-
Notifications
You must be signed in to change notification settings - Fork 3
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
2 changed files
with
137 additions
and
0 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
133 changes: 133 additions & 0 deletions
133
src/scenes/Engagement/LanguageEngagement/Milestone/Milestone.tsx
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,133 @@ | ||
import { useMutation } from '@apollo/client'; | ||
import { Check, Clear, Edit } from '@mui/icons-material'; | ||
import { CircularProgress, Stack, Tooltip, Typography } from '@mui/material'; | ||
import { useMemo, useState } from 'react'; | ||
import { Form } from 'react-final-form'; | ||
import { | ||
LanguageMilestone, | ||
LanguageMilestoneLabels, | ||
LanguageMilestoneList, | ||
} from '~/api/schema.graphql'; | ||
import { SelectField } from '~/components/form'; | ||
import { IconButton } from '../../../../components/IconButton'; | ||
import { UpdateLanguageEngagementDocument } from '../../EditEngagement/EditEngagementDialog.graphql'; | ||
import { EngagementMilestoneReachedFragment } from './Milestone.graphql'; | ||
|
||
interface Props { | ||
engagement: EngagementMilestoneReachedFragment; | ||
} | ||
|
||
interface FormShape { | ||
milestoneReached: LanguageMilestone; | ||
} | ||
|
||
export const LanguageEngagementMilestone = ({ engagement }: Props) => { | ||
const { milestoneReached } = engagement; | ||
|
||
const [isEditing, setIsEditing] = useState(false); | ||
const [updateLanguageEngagement] = useMutation( | ||
UpdateLanguageEngagementDocument | ||
); | ||
|
||
const initialValues = useMemo( | ||
(): FormShape => ({ | ||
milestoneReached: milestoneReached.value || 'Unknown', | ||
}), | ||
[milestoneReached] | ||
); | ||
|
||
if (!milestoneReached.canRead) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Form<FormShape> | ||
initialValues={initialValues} | ||
onSubmit={async (values, form) => { | ||
if (form.getState().dirty) { | ||
await updateLanguageEngagement({ | ||
variables: { | ||
input: { | ||
engagement: { | ||
id: engagement.id, | ||
milestoneReached: values.milestoneReached, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
setIsEditing(false); | ||
}} | ||
subscription={{ submitting: true }} | ||
> | ||
{({ handleSubmit, form, submitting }) => ( | ||
<Stack | ||
spacing={1} | ||
sx={{ mb: 4 }} | ||
component={isEditing ? 'form' : 'div'} | ||
onSubmit={handleSubmit} | ||
> | ||
<Stack direction="row" alignItems="center" spacing={1}> | ||
<Typography variant="h3">Milestone</Typography> | ||
{!isEditing ? ( | ||
milestoneReached.canEdit ? ( | ||
<Tooltip title="Edit"> | ||
<IconButton | ||
onClick={() => setIsEditing(true)} | ||
aria-label="edit milestone" | ||
> | ||
<Edit /> | ||
</IconButton> | ||
</Tooltip> | ||
) : null | ||
) : ( | ||
<> | ||
<Tooltip title="Save"> | ||
<IconButton | ||
color="success" | ||
disabled={submitting} | ||
type="submit" | ||
> | ||
{submitting ? <CircularProgress size={24} /> : <Check />} | ||
</IconButton> | ||
</Tooltip> | ||
<Tooltip title="Cancel"> | ||
<IconButton | ||
color="error" | ||
disabled={submitting} | ||
onClick={() => { | ||
setIsEditing(false); | ||
form.reset(); | ||
}} | ||
> | ||
<Clear /> | ||
</IconButton> | ||
</Tooltip> | ||
</> | ||
)} | ||
</Stack> | ||
{isEditing ? ( | ||
<SelectField | ||
name="milestoneReached" | ||
label="Milestone" | ||
options={LanguageMilestoneList} | ||
getOptionLabel={(option) => LanguageMilestoneLabels[option]} | ||
defaultValue={milestoneReached.value} | ||
required | ||
/> | ||
) : milestoneReached.value ? ( | ||
<Typography color="textSecondary"> | ||
{LanguageMilestoneLabels[milestoneReached.value]} | ||
</Typography> | ||
) : ( | ||
<Typography color="textSecondary"> | ||
Unknown{' '} | ||
{milestoneReached.canEdit && '— click the edit pencil to add'} | ||
</Typography> | ||
)} | ||
</Stack> | ||
)} | ||
</Form> | ||
); | ||
}; |