All files / src/pages/TagCreator Organization.js

83.33% Statements 45/54
65.63% Branches 21/32
83.33% Functions 15/18
82.69% Lines 43/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                          61x                                       61x 66x 66x 66x   66x   66x             66x                 9x     9x   51467x 115128x         9x   390x                                                   61x 956x 956x 3x   956x                                   61x 62x 62x 14x     14x     14x       14x       62x 14x 14x     14x 11x 11x 11x   8x 8x 8x   8x 16x   8x               11x   3x 3x     62x              
 
import React,{ useState } from 'react';
import axios from 'axios';
import Button from '@material-ui/core/Button';
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
import Link from '@material-ui/core/Link';
import CircularProgress from '@material-ui/core/CircularProgress';
import { makeStyles } from '@material-ui/core/styles'
import AddOrgModal from '../../components/AddOrgModal';
 
const useStyles = makeStyles((theme) => ({
  gridStyle:{
    paddingRight: '8px',
    [theme.breakpoints.down('xs')]: {
      paddingBottom: '8px',
    },
  },
  typoStyle: {
    [theme.breakpoints.down('xs')]: {
      fontSize: '1.5rem',
    },
  },
  tStyle: {
    fontWeight:'500',
    [theme.breakpoints.down('xs')]: {
      fontSize: '0.875rem',
    },
  },
}))
 
export const OrganizationSelectorSection = ({ orgName, setOrgName, options }) => {
  const [modalOpen, setModalOpen] = useState(false);
  const [open, setOpen] = useState(false);
  const [currentOptions, setCurrentOptions] = useState(options);
 
  const loading = open && options.length === 0;
 
  const handleModalClose = (newOrg) => {
    if (newOrg) {
      setCurrentOptions([newOrg.name, ...currentOptions]);
    }
    setModalOpen(false);
  };
 
  return (
    <>
      <Grid item xs={12} sm={12}>
        <p>Which Organization?</p>
        <Autocomplete
          id="organization"
          style={{ width: '100%' }}
          open={open}
          onOpen={() => {
            setOpen(true);
          }}
          onClose={() => {
            setOpen(false);
          }}
          getOptionSelected={(option, value) => option === value }
          getOptionLabel={(option) => option}
          options={currentOptions}
          autoComplete
          loading={loading}
          value={orgName}
          onChange={(e, v) => setOrgName(v)}
          renderInput={(params) =>(
            <TextField {...params}
              required
              variant="outlined"
              InputProps={{
                ...params.InputProps,
                endAdornment: (
                  <React.Fragment>
                    {loading ? <CircularProgress color="inherit" size={20} /> : null}
                    {params.InputProps.endAdornment}
                  </React.Fragment>
                ),
              }}
            />
          )}
        />
      </Grid>
      <Grid item>
        <Typography variant='body1'>
          Don’t see your organization? Click <Link onClick={() => setModalOpen(true)}><b>here</b></Link> to add it.
        </Typography>
      </Grid>
      <AddOrgModal open={modalOpen} onClose={handleModalClose} />
    </>
  )
}
 
export const OrgNameSection = ({ setDisplayState,orgName,linkStyles }) => {
  const classes = useStyles()
  const handleChangeOrg = () => {
    setDisplayState('')
  }
  return (
    <Grid container direction="row" alignItems="center" style={{ padding: '48px 0px 32px 0px' }}>
      <Grid item xs={12} sm={3} className={classes.gridStyle} >
        <Typography variant='h6' className={classes.tStyle} >Affliated Organization:</Typography>
      </Grid>
      {orgName ?
        <Grid item xs={10} sm={7}>
          <Typography variant='h3' className={classes.typoStyle}>{orgName}</Typography>
        </Grid> : <Grid item xs={7} style={{ paddingRight: '50px' }}>
          <Typography variant='h3'>Unaffliated</Typography>
        </Grid>}
      <Grid item xs={2} >
        <Link id="change-org" component="button" variant='body1' onClick={handleChangeOrg} underline='always' style={linkStyles} >change</Link>
      </Grid>
    </Grid>
  )
}
 
export const OrgChange = ({ value,orgName,setOrgName, setOrgTags, changeValue, setDisplayState }) => {
  const [orgNameError, setOrgNameError] = useState('');
  const handleChangeOrg = () => {
    Iif (changeValue === 'TopicTag') {
      setDisplayState('TopicTag')
    }
    else Iif (changeValue === 'GenerateTags') {
      setDisplayState('GenerateTags')
    }
    else Iif (changeValue === 'CopyPasteTags') {
      setDisplayState('CopyPasteTags')
    }
    else {
      setDisplayState('ProjectUrl')
    }
  }
  // eslint-disable-next-line complexity
  const handleSubmitOrg = () => {
    const topics = []
    Iif (value === 'yes' && orgName === ""){
      setOrgNameError(<p style={{ color: 'red' }}> Please select org name</p>)
    }
    else if (value === 'yes' && orgName !== "") {
      let og = orgName.normalize('NFD').replace(/[\u0300-\u036f]/g, "")
      og = og.replace(/ /g,"-").toLowerCase()
      axios.get(`${process.env.REACT_APP_API_URL}/api/organizations/${og}`,)
        .then(res => {
          const po = res.data.parents
          Eif (res.data.org_tag !== "") {
            topics.push(res.data.org_tag)
          }
          Eif (po.length !== 0){
            po.map(p =>(p.org_tag !== "") ? topics.push(p.org_tag) : null)
          }
          setOrgTags(topics)
        }).catch(e => {
          /*
           * This should store the error state.
           * Component should check for error state and resolve the correct response.
           */
          console.log(e);
        })
      handleChangeOrg()
    }
    else Eif (value === 'no' && orgName === ''){
      handleChangeOrg()
    }
  }
  return (
    <Grid item xs={12} sm={12}>
      {orgNameError}
      <Grid align='center' style={{ padding: '20px' }}><Button onClick={handleSubmitOrg} id='submitButton'>Submit Organization</Button></Grid>
    </Grid>
  )
}