-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v3' into course-deletion
- Loading branch information
Showing
20 changed files
with
637 additions
and
51 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
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
148 changes: 148 additions & 0 deletions
148
apps/frontend-manage/src/components/courses/modals/ExtensionModal.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,148 @@ | ||
import { useMutation } from '@apollo/client' | ||
import { | ||
ExtendGroupActivityDocument, | ||
ExtendMicroLearningDocument, | ||
GetSingleCourseDocument, | ||
} from '@klicker-uzh/graphql/dist/ops' | ||
import { Button, FormikDateField, Modal } from '@uzh-bf/design-system' | ||
import dayjs from 'dayjs' | ||
import { Form, Formik } from 'formik' | ||
import { useTranslations } from 'next-intl' | ||
import { twMerge } from 'tailwind-merge' | ||
import * as Yup from 'yup' | ||
|
||
interface ExtensionModalProps { | ||
type: 'microLearning' | 'groupActivity' | ||
id: string | ||
currentEndDate: Date | ||
courseId: string | ||
title: string | ||
description: string | ||
open: boolean | ||
setOpen: (value: boolean) => void | ||
} | ||
|
||
function ExtensionModal({ | ||
type, | ||
id, | ||
currentEndDate, | ||
courseId, | ||
title, | ||
description, | ||
open, | ||
setOpen, | ||
}: ExtensionModalProps) { | ||
const t = useTranslations() | ||
const [extendMicroLearning] = useMutation(ExtendMicroLearningDocument, { | ||
refetchQueries: [ | ||
{ query: GetSingleCourseDocument, variables: { courseId: courseId } }, | ||
], | ||
}) | ||
const [extendGroupActivity] = useMutation(ExtendGroupActivityDocument, { | ||
refetchQueries: [ | ||
{ query: GetSingleCourseDocument, variables: { courseId: courseId } }, | ||
], | ||
}) | ||
|
||
return ( | ||
<Modal | ||
onClose={(): void => setOpen(false)} | ||
open={open} | ||
hideCloseButton={true} | ||
title={title} | ||
className={{ | ||
content: 'h-max min-h-max w-[40rem] self-center !pb-0', | ||
title: 'text-xl', | ||
}} | ||
> | ||
<div className="space-y-3" data-cy="activity-extension-modal"> | ||
<div>{description}</div> | ||
<Formik | ||
initialValues={{ | ||
endDate: dayjs(currentEndDate).local().format('YYYY-MM-DDTHH:mm'), | ||
}} | ||
validationSchema={Yup.object().shape({ | ||
endDate: Yup.date() | ||
.required() | ||
.min(new Date(), t('manage.course.futureEndDateRequired')), | ||
})} | ||
onSubmit={async (values, { setSubmitting }) => { | ||
const utcEndDate = dayjs(values.endDate).utc().format() | ||
setSubmitting(true) | ||
|
||
if (type === 'microLearning') { | ||
await extendMicroLearning({ | ||
variables: { | ||
id, | ||
endDate: utcEndDate, | ||
}, | ||
optimisticResponse: { | ||
__typename: 'Mutation', | ||
extendMicroLearning: { | ||
__typename: 'MicroLearning', | ||
id, | ||
scheduledEndAt: utcEndDate, | ||
}, | ||
}, | ||
}) | ||
} else if (type === 'groupActivity') { | ||
await extendGroupActivity({ | ||
variables: { | ||
id, | ||
endDate: utcEndDate, | ||
}, | ||
optimisticResponse: { | ||
__typename: 'Mutation', | ||
extendGroupActivity: { | ||
__typename: 'GroupActivity', | ||
id, | ||
scheduledEndAt: utcEndDate, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
setSubmitting(false) | ||
setOpen(false) | ||
}} | ||
> | ||
{({ isValid, isSubmitting }) => ( | ||
<Form> | ||
<FormikDateField | ||
required | ||
name="endDate" | ||
label={t('manage.course.newEndDate')} | ||
labelType="large" | ||
data={{ cy: 'extend-activity-date' }} | ||
/> | ||
<div className="mt-3 flex flex-row justify-between"> | ||
<Button | ||
onClick={(): void => setOpen(false)} | ||
data={{ cy: 'extend-activity-cancel' }} | ||
> | ||
{t('shared.generic.cancel')} | ||
</Button> | ||
<Button | ||
type="submit" | ||
loading={isSubmitting} | ||
disabled={!isValid} | ||
className={{ | ||
root: twMerge( | ||
'bg-primary-100 font-bold text-white', | ||
!isValid && 'bg-primary-40 cursor-not-allowed' | ||
), | ||
}} | ||
data={{ cy: 'extend-activity-confirm' }} | ||
> | ||
{t('shared.generic.confirm')} | ||
</Button> | ||
</div> | ||
</Form> | ||
)} | ||
</Formik> | ||
</div> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default ExtensionModal |
Oops, something went wrong.