Skip to content

Commit

Permalink
feat: unify action form and review structures (#8253)
Browse files Browse the repository at this point in the history
* feat: unify action form and review structures

* fix: add missing license header

* fix: use register routes in register preview

* fix: typo in readme
  • Loading branch information
makelicious authored Dec 23, 2024
1 parent 85887d6 commit e290682
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 201 deletions.
11 changes: 11 additions & 0 deletions packages/client/src/v2-events/GLOSSARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
| Entity | Description |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Event | A life event (e.g., dog adoption). An entry in the database describing a past life event and all steps (actions) involved in the process. |
| EventConfig | Description of event features defined by the country. Includes configuration for process steps (`Action`) and forms (`ActionConfig`) involved. |
| EventInput | A subset of an event. Describes fields that can be sent to the system with the intention of either creating or mutating a an event |
| EventIndex | A subset of an event. Describes how the event is stored in the search index. Contains static fields shared by all event types and custom fields defined by event configuration |
| User | User in the system. Might be a practitioner or an admin or something else. |
| Location | Describes a physical location. Can be a admin structure, an office or something else. Cannot be anyone's personal home address |
| Action | Event contains one or more actions. Action is a system event which triggers a status change. See `ActionConfig` |
| ActionConfig | Each action defines a form, which needs to be filled in order for the status to change. Configuration can have multiple forms, out of which **only one can be active**. |
| FormConfig |  Form config defines separate read (`review`) and write (`pages`) configurations for itself. |
10 changes: 9 additions & 1 deletion packages/client/src/v2-events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ Client for managing custom events.
/components
# Features used by routes
/features
/events
# Each action has 'review' and 'pages' definitions to match configuration
/actions
[Action]/
Pages.tsx
Review.tsx
# exports Pages and Review
index.ts
# Reusable layouts used between features
/layouts
# Route definitions and application structure
Expand All @@ -24,6 +32,6 @@ We will be iterating over the structure during the project. Treat it as a starti
## Development practices

- Do not import components outside v2-events. If you need a component, copy it in and refactor it.
- When building new features, aim to have a separate component that handles interaction with router and data fetching when it makes sense. Features should be route independent, and necessary information (ids and such) should be passed in as props or similar. See (`features/events/actions/register/Register.tsx`)
- When building new features, aim to have a separate component that handles interaction with router and data fetching when it makes sense. Features should be route independent, and necessary information (ids and such) should be passed in as props or similar. See (`features/events/actions/register` or `features/events/actions/declare`)
- Use constants through object pattern. e.g.`ActionType.CREATE` over `'CREATE'`. In most situations, it does not matter. However, changing the names will get much easier.
- When building new features, prefer to import them through `index.ts`. Managing imports will be cleaner and easier that way. See (`/layouts`)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 { ActionType } from '@opencrvs/commons/client'
import { useEventConfiguration } from '@client/v2-events/features/events/useEventConfiguration'
import { useEventFormNavigation } from '@client/v2-events/features/events/useEventFormNavigation'
import { useEvents } from '@client/v2-events//features/events/useEvents/useEvents'
import { ROUTES } from '@client/v2-events/routes'
import { Pages as PagesComponent } from '@client/v2-events/features/events/components/Pages'

export function Pages() {
const { eventId, pageId } = useTypedParams(ROUTES.V2.EVENTS.DECLARE.PAGES)
const [searchParams] = useTypedSearchParams(ROUTES.V2.EVENTS.DECLARE.PAGES)
const navigate = useNavigate()
const events = useEvents()
const { modal } = useEventFormNavigation()

const [event] = events.getEvent(eventId)

const { eventConfiguration: configuration } = useEventConfiguration(
event.type
)
const formPages = configuration?.actions
.find((action) => action.type === ActionType.DECLARE)
?.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.DECLARE.PAGES.buildPath({
eventId,
pageId: currentPageId
}),
{ replace: true }
)
}
}, [pageId, currentPageId, navigate, eventId])

return (
<>
{modal}
<PagesComponent
eventId={eventId}
formPages={formPages}
pageId={currentPageId}
showReviewButton={searchParams.from === 'review'}
onFormPageChange={(nextPageId: string) =>
navigate(
ROUTES.V2.EVENTS.DECLARE.PAGES.buildPath({
eventId,
pageId: nextPageId
})
)
}
onSubmit={() =>
navigate(ROUTES.V2.EVENTS.DECLARE.REVIEW.buildPath({ eventId }))
}
/>
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { useEventFormNavigation } from '@client/v2-events/features/events/useEve
import { useEvents } from '@client/v2-events/features/events/useEvents/useEvents'
import { useModal } from '@client/v2-events/hooks/useModal'
import { ROUTES } from '@client/v2-events/routes'
import { Preview } from '@client/v2-events/features/events/components/Preview'
import { Review as ReviewComponent } from '@client/v2-events/features/events/components/Review'

const messages = defineMessages({
reviewActionTitle: {
Expand Down Expand Up @@ -97,7 +97,7 @@ interface RejectionState {
isDuplicate: boolean
}

export function ReviewSection() {
export function Review() {
const { eventId } = useTypedParams(ROUTES.V2.EVENTS.DECLARE.REVIEW)
const events = useEvents()
const navigate = useNavigate()
Expand Down Expand Up @@ -129,12 +129,12 @@ export function ReviewSection() {
fieldId?: string
}) {
const confirmedEdit = await openModal<boolean | null>((close) => (
<Preview.EditModal close={close}></Preview.EditModal>
<ReviewComponent.EditModal close={close}></ReviewComponent.EditModal>
))

if (confirmedEdit) {
navigate(
ROUTES.V2.EVENTS.DECLARE.PAGE.buildPath(
ROUTES.V2.EVENTS.DECLARE.PAGES.buildPath(
{ pageId, eventId },
{
from: 'review'
Expand All @@ -149,7 +149,7 @@ export function ReviewSection() {

async function handleDeclaration() {
const confirmedDeclaration = await openModal<boolean | null>((close) => (
<Preview.ActionModal action="Declare" close={close} />
<ReviewComponent.ActionModal action="Declare" close={close} />
))
if (confirmedDeclaration) {
declareMutation.mutate({
Expand Down Expand Up @@ -184,7 +184,7 @@ export function ReviewSection() {

return (
<>
<Preview.Body
<ReviewComponent.Body
eventConfig={config}
formConfig={formConfigs[0]}
// eslint-disable-next-line
Expand All @@ -196,7 +196,7 @@ export function ReviewSection() {
surname: form['applicant.surname']
})}
>
<Preview.Actions
<ReviewComponent.Actions
messages={{
title: messages.reviewActionTitle,
description: messages.reviewActionDescription,
Expand All @@ -205,7 +205,7 @@ export function ReviewSection() {
onConfirm={handleDeclaration}
onReject={handleReject}
/>
</Preview.Body>
</ReviewComponent.Body>
{modal}
</>
)
Expand Down
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 { Pages } from './Pages'
import { Review } from './Review'

export { Pages, Review }
Loading

0 comments on commit e290682

Please sign in to comment.