-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events v2 collect certificate workflow boilerplate code
- Loading branch information
Showing
12 changed files
with
371 additions
and
3 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
packages/client/src/v2-events/features/events/actions/collect-certificate/Pages.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,116 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
|
||
import React, { useEffect } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
import { | ||
useTypedParams, | ||
useTypedSearchParams | ||
} from 'react-router-typesafe-routes/dom' | ||
import { v4 as uuid } from 'uuid' | ||
import { ActionType, getCurrentEventState } from '@opencrvs/commons/client' | ||
import { useEvents } from '@client/v2-events//features/events/useEvents/useEvents' | ||
import { Pages as PagesComponent } from '@client/v2-events/features/events/components/Pages' | ||
import { useEventConfiguration } from '@client/v2-events/features/events/useEventConfiguration' | ||
import { useEventFormNavigation } from '@client/v2-events/features/events/useEventFormNavigation' | ||
import { ROUTES } from '@client/v2-events/routes' | ||
import { useEventFormData } from '@client/v2-events/features/events/useEventFormData' | ||
import { FormLayout } from '@client/v2-events/layouts/form' | ||
|
||
export function Pages() { | ||
const { eventId, pageId } = useTypedParams( | ||
ROUTES.V2.EVENTS.COLLECT_CERTIFICATE.PAGES | ||
) | ||
const [searchParams] = useTypedSearchParams( | ||
ROUTES.V2.EVENTS.COLLECT_CERTIFICATE.PAGES | ||
) | ||
const setFormValues = useEventFormData((state) => state.setFormValues) | ||
const formEventId = useEventFormData((state) => state.eventId) | ||
const form = useEventFormData((state) => state.formValues) | ||
const navigate = useNavigate() | ||
const events = useEvents() | ||
const { modal, goToHome } = useEventFormNavigation() | ||
|
||
const [event] = events.getEvent.useSuspenseQuery(eventId) | ||
const currentState = getCurrentEventState(event) | ||
|
||
useEffect(() => { | ||
if (formEventId !== event.id) { | ||
setFormValues(event.id, currentState.data) | ||
} | ||
}, [currentState.data, event.id, formEventId, setFormValues]) | ||
|
||
const { eventConfiguration: configuration } = useEventConfiguration( | ||
event.type | ||
) | ||
const formPages = configuration.actions | ||
.find((action) => action.type === ActionType.COLLECT_CERTIFICATE) | ||
?.forms.find((form) => form.active)?.pages | ||
|
||
if (!formPages) { | ||
throw new Error('Form configuration not found for type: ' + event.type) | ||
} | ||
|
||
const currentPageId = | ||
formPages.find((p) => p.id === pageId)?.id || formPages[0]?.id | ||
|
||
if (!currentPageId) { | ||
throw new Error('Form does not have any pages') | ||
} | ||
|
||
useEffect(() => { | ||
if (pageId !== currentPageId) { | ||
navigate( | ||
ROUTES.V2.EVENTS.COLLECT_CERTIFICATE.PAGES.buildPath({ | ||
eventId, | ||
pageId: currentPageId | ||
}), | ||
{ replace: true } | ||
) | ||
} | ||
}, [pageId, currentPageId, navigate, eventId]) | ||
|
||
return ( | ||
<FormLayout | ||
route={ROUTES.V2.EVENTS.COLLECT_CERTIFICATE} | ||
onSaveAndExit={() => { | ||
events.actions.collectCertificate.mutate({ | ||
eventId: event.id, | ||
data: form, | ||
transactionId: uuid(), | ||
draft: true | ||
}) | ||
goToHome() | ||
}} | ||
> | ||
{modal} | ||
<PagesComponent | ||
eventId={eventId} | ||
formPages={formPages} | ||
pageId={currentPageId} | ||
showReviewButton={searchParams.from === 'review'} | ||
onFormPageChange={(nextPageId: string) => | ||
navigate( | ||
ROUTES.V2.EVENTS.COLLECT_CERTIFICATE.PAGES.buildPath({ | ||
eventId, | ||
pageId: nextPageId | ||
}) | ||
) | ||
} | ||
onSubmit={() => | ||
navigate( | ||
ROUTES.V2.EVENTS.COLLECT_CERTIFICATE.REVIEW.buildPath({ eventId }) | ||
) | ||
} | ||
/> | ||
</FormLayout> | ||
) | ||
} |
151 changes: 151 additions & 0 deletions
151
packages/client/src/v2-events/features/events/actions/collect-certificate/Review.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,151 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
|
||
import React, { useEffect } from 'react' | ||
import { defineMessages } from 'react-intl' | ||
import { useNavigate } from 'react-router-dom' | ||
import { v4 as uuid } from 'uuid' | ||
import { useTypedParams } from 'react-router-typesafe-routes/dom' | ||
import { getCurrentEventState, ActionType } from '@opencrvs/commons/client' | ||
import { ROUTES } from '@client/v2-events/routes' | ||
import { useEvents } from '@client/v2-events/features/events/useEvents/useEvents' | ||
import { Review as ReviewComponent } from '@client/v2-events/features/events/components/Review' | ||
import { useModal } from '@client/v2-events/hooks/useModal' | ||
import { useEventFormNavigation } from '@client/v2-events/features/events/useEventFormNavigation' | ||
import { useEventConfiguration } from '@client/v2-events/features/events/useEventConfiguration' | ||
import { useEventFormData } from '@client/v2-events/features/events/useEventFormData' | ||
import { FormLayout } from '@client/v2-events/layouts/form' | ||
|
||
const messages = defineMessages({ | ||
registerActionTitle: { | ||
id: 'registerAction.title', | ||
defaultMessage: 'Register member', | ||
description: 'The title for register action' | ||
}, | ||
registerActionDescription: { | ||
id: 'registerAction.description', | ||
defaultMessage: | ||
'By clicking register, you confirm that the information entered is correct and the member can be registered.', | ||
description: 'The description for register action' | ||
}, | ||
registerActionDeclare: { | ||
id: 'registerAction.Declare', | ||
defaultMessage: 'Register', | ||
description: 'The label for declare button of register action' | ||
} | ||
}) | ||
|
||
/** | ||
* | ||
* Preview of event to be registered. | ||
*/ | ||
export function Review() { | ||
const { eventId } = useTypedParams(ROUTES.V2.EVENTS.COLLECT_CERTIFICATE) | ||
const events = useEvents() | ||
const [modal, openModal] = useModal() | ||
const navigate = useNavigate() | ||
const { goToHome } = useEventFormNavigation() | ||
const collectCertificateMutation = events.actions.collectCertificate | ||
|
||
const [event] = events.getEvent.useSuspenseQuery(eventId) | ||
|
||
const { eventConfiguration: config } = useEventConfiguration(event.type) | ||
|
||
if (!config) { | ||
throw new Error('Event configuration not found with type: ' + event.type) | ||
} | ||
|
||
const { forms: formConfigs } = config.actions.filter( | ||
(action) => action.type === ActionType.COLLECT_CERTIFICATE | ||
)[0] | ||
|
||
const setFormValues = useEventFormData((state) => state.setFormValues) | ||
const getFormValues = useEventFormData((state) => state.getFormValues) | ||
|
||
useEffect(() => { | ||
setFormValues(eventId, getCurrentEventState(event).data) | ||
}, [event, eventId, setFormValues]) | ||
|
||
const form = getFormValues(eventId) | ||
|
||
async function handleEdit({ | ||
pageId, | ||
fieldId | ||
}: { | ||
pageId: string | ||
fieldId?: string | ||
}) { | ||
const confirmedEdit = await openModal<boolean | null>((close) => ( | ||
<ReviewComponent.EditModal close={close} /> | ||
)) | ||
|
||
if (confirmedEdit) { | ||
navigate( | ||
ROUTES.V2.EVENTS.COLLECT_CERTIFICATE.PAGES.buildPath( | ||
{ pageId, eventId }, | ||
{ | ||
from: 'review' | ||
}, | ||
fieldId | ||
) | ||
) | ||
} | ||
return | ||
} | ||
|
||
async function handleRegistration() { | ||
const confirmedRegistration = await openModal<boolean | null>((close) => ( | ||
<ReviewComponent.ActionModal action="Collect Certificate" close={close} /> | ||
)) | ||
if (confirmedRegistration) { | ||
collectCertificateMutation.mutate({ | ||
eventId: event.id, | ||
data: form, | ||
transactionId: uuid() | ||
}) | ||
|
||
goToHome() | ||
} | ||
} | ||
|
||
return ( | ||
<FormLayout | ||
route={ROUTES.V2.EVENTS.COLLECT_CERTIFICATE} | ||
onSaveAndExit={() => { | ||
events.actions.collectCertificate.mutate({ | ||
eventId: event.id, | ||
data: form, | ||
transactionId: uuid(), | ||
draft: true | ||
}) | ||
goToHome() | ||
}} | ||
> | ||
<ReviewComponent.Body | ||
eventConfig={config} | ||
form={form} | ||
formConfig={formConfigs[0]} | ||
title="" | ||
onEdit={handleEdit} | ||
> | ||
<ReviewComponent.Actions | ||
messages={{ | ||
title: messages.registerActionTitle, | ||
description: messages.registerActionDescription, | ||
onConfirm: messages.registerActionDeclare | ||
}} | ||
onConfirm={handleRegistration} | ||
/> | ||
{modal} | ||
</ReviewComponent.Body> | ||
</FormLayout> | ||
) | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/client/src/v2-events/features/events/actions/collect-certificate/index.ts
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,15 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
|
||
import { Review } from './Review' | ||
import { Pages } from './Pages' | ||
|
||
export { Review, Pages } |
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
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
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.