-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
376 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useMutation, useQuery, useQueryClient } from 'react-query'; | ||
|
||
import { useAxios } from './axios'; | ||
|
||
const usePutVersion = () => { | ||
const axios = useAxios(); | ||
const queryClient = useQueryClient(); | ||
return useMutation(async (data) => axios.put('versions', data) | ||
.then((response) => response), { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('developers'); | ||
queryClient.invalidateQueries('developers/hierarchy'); | ||
}, | ||
}); | ||
}; | ||
|
||
export { | ||
usePutVersion, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import React, { useContext, useEffect, useState } from 'react'; | ||
import { | ||
Card, | ||
CardHeader, | ||
CardContent, | ||
Container, | ||
makeStyles, | ||
} from '@material-ui/core'; | ||
import { | ||
arrayOf, | ||
bool, | ||
func, | ||
object, | ||
string, | ||
} from 'prop-types'; | ||
import { useFormik } from 'formik'; | ||
import * as yup from 'yup'; | ||
|
||
import { ChplActionBar } from 'components/action-bar'; | ||
import { ChplTextField } from 'components/util'; | ||
import { eventTrack } from 'services/analytics.service'; | ||
import { DeveloperContext, UserContext, useAnalyticsContext } from 'shared/contexts'; | ||
import { utilStyles } from 'themes'; | ||
|
||
const useStyles = makeStyles({ | ||
...utilStyles, | ||
content: { | ||
display: 'grid', | ||
gridTemplateColumns: '1fr 1fr', | ||
gap: '16px', | ||
alignItems: 'start', | ||
}, | ||
developerHeader: { | ||
margin: '0', | ||
fontSize: '1.25em', | ||
}, | ||
}); | ||
|
||
const validationSchema = yup.object({ | ||
version: yup.string() | ||
.required('Version is required'), | ||
}); | ||
|
||
function ChplVersionEdit(props) { | ||
const { | ||
dispatch, | ||
errorMessages: initialErrorMessages, | ||
isInvalid: initialIsInvalid, | ||
isProcessing, | ||
isSplitting, | ||
version, | ||
} = props; | ||
const { analytics } = useAnalyticsContext(); | ||
const { developer } = useContext(DeveloperContext); | ||
const [errorMessages, setErrorMessages] = useState([]); | ||
const [warnings, setWarnings] = useState([]); | ||
const [isInvalid, setIsInvalid] = useState(false); | ||
const classes = useStyles(); | ||
let formik; | ||
|
||
useEffect(() => { | ||
setIsInvalid(initialIsInvalid); | ||
}, [initialIsInvalid]); | ||
|
||
useEffect(() => { | ||
setErrorMessages(initialErrorMessages); | ||
}, [initialErrorMessages]); | ||
|
||
const cancel = () => { | ||
eventTrack({ | ||
...analytics, | ||
event: 'Cancel Version Edit', | ||
}); | ||
dispatch('cancel'); | ||
}; | ||
|
||
const save = () => { | ||
const updatedVersion = { | ||
...version, | ||
version: formik.values.version, | ||
}; | ||
dispatch('save', updatedVersion); | ||
}; | ||
|
||
const handleDispatch = (action) => { | ||
switch (action) { | ||
case 'cancel': | ||
cancel(); | ||
break; | ||
case 'save': | ||
formik.submitForm(); | ||
break; | ||
// no default | ||
} | ||
}; | ||
|
||
const isActionDisabled = () => isInvalid || !formik.isValid; | ||
|
||
formik = useFormik({ | ||
initialValues: { | ||
version: version.version || '', | ||
}, | ||
onSubmit: () => { | ||
save(); | ||
}, | ||
validationSchema, | ||
}); | ||
|
||
return ( | ||
<Container disableGutters maxWidth="lg"> | ||
<Card> | ||
{ isSplitting | ||
&& ( | ||
<CardHeader | ||
title="New Developer" | ||
component="h5" | ||
className={classes.developerHeader} | ||
/> | ||
)} | ||
{ !isSplitting | ||
&& ( | ||
<CardHeader | ||
title={developer.name} | ||
className={classes.developerHeader} | ||
component="h2" | ||
/> | ||
)} | ||
<CardContent className={classes.content}> | ||
<ChplTextField | ||
id="version" | ||
name="version" | ||
label="Version" | ||
required | ||
value={formik.values.version} | ||
onChange={formik.handleChange} | ||
onBlur={formik.handleBlur} | ||
error={formik.touched.version && !!formik.errors.version} | ||
helperText={formik.touched.version && formik.errors.version} | ||
/> | ||
</CardContent> | ||
</Card> | ||
<ChplActionBar | ||
dispatch={handleDispatch} | ||
isDisabled={isActionDisabled()} | ||
isProcessing={isProcessing} | ||
errors={errorMessages} | ||
warnings={warnings} | ||
/> | ||
</Container> | ||
); | ||
} | ||
|
||
export default ChplVersionEdit; | ||
|
||
ChplVersionEdit.propTypes = { | ||
dispatch: func.isRequired, | ||
errorMessages: arrayOf(string).isRequired, | ||
isInvalid: bool.isRequired, | ||
isProcessing: bool, | ||
isSplitting: bool.isRequired, | ||
version: object.isRequired, | ||
}; | ||
|
||
ChplVersionEdit.defaultProps = { | ||
isProcessing: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { | ||
arrayOf, | ||
bool, | ||
func, | ||
object, | ||
string, | ||
} from 'prop-types'; | ||
|
||
import ChplVersionEdit from './version-edit'; | ||
|
||
function ChplVersion({ | ||
dispatch, | ||
errorMessages, | ||
isInvalid: initialIsInvalid, | ||
isProcessing, | ||
isSplitting, | ||
version, | ||
}) { | ||
const [isInvalid, setIsInvalid] = useState(false); | ||
|
||
useEffect(() => { | ||
setIsInvalid(initialIsInvalid); | ||
}, [initialIsInvalid]); | ||
|
||
return ( | ||
<ChplVersionEdit | ||
dispatch={dispatch} | ||
isInvalid={isInvalid} | ||
isProcessing={isProcessing} | ||
isSplitting={isSplitting} | ||
errorMessages={errorMessages} | ||
version={version} | ||
/> | ||
); | ||
} | ||
|
||
export default ChplVersion; | ||
|
||
ChplVersion.propTypes = { | ||
dispatch: func, | ||
errorMessages: arrayOf(string), | ||
isInvalid: bool, | ||
isProcessing: bool, | ||
isSplitting: bool, | ||
version: object.isRequired, | ||
}; | ||
|
||
ChplVersion.defaultProps = { | ||
dispatch: () => {}, | ||
errorMessages: [], | ||
isInvalid: false, | ||
isProcessing: false, | ||
isSplitting: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.