-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start using suspense in EventFormWizard and add Exit modal
- Loading branch information
Showing
4 changed files
with
152 additions
and
59 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
41 changes: 0 additions & 41 deletions
41
packages/client/src/v2-events/features/events/useEventForm.ts
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
packages/client/src/v2-events/features/events/useEventFormNavigation.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,92 @@ | ||
/* | ||
* 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 from 'react' | ||
import { Button, ResponsiveModal, Stack, Text } from '@opencrvs/components' | ||
import { defineMessages, useIntl } from 'react-intl' | ||
import { useModal } from '@client/v2-events/hooks/useModal' | ||
import { V2_ROOT_ROUTE } from '@client/v2-events/routes' | ||
import { useHistory } from 'react-router-dom' | ||
|
||
const modalMessages = defineMessages({ | ||
cancel: { | ||
id: 'exitModal.cancel', | ||
defaultMessage: 'Cancel' | ||
}, | ||
confirm: { | ||
id: 'buttons.confirm', | ||
defaultMessage: 'Confirm' | ||
}, | ||
exitWithoutSavingTitle: { | ||
id: 'exitModal.exitWithoutSaving', | ||
defaultMessage: 'Exit without saving changes?' | ||
}, | ||
exitWithoutSavingDescription: { | ||
id: 'exitModal.exitWithoutSavingDescription', | ||
defaultMessage: | ||
'You have unsaved changes on your declaration form. Are you sure you want to exit without saving?' | ||
} | ||
}) | ||
|
||
export const useEventFormNavigation = () => { | ||
const intl = useIntl() | ||
const [modal, openModal] = useModal() | ||
const history = useHistory() | ||
|
||
const goToHome = () => { | ||
history.push(V2_ROOT_ROUTE) | ||
} | ||
|
||
const exit = async () => { | ||
const exitConfirm = await openModal<boolean | null>((close) => ( | ||
<ResponsiveModal | ||
autoHeight | ||
responsive={false} | ||
title={intl.formatMessage(modalMessages.exitWithoutSavingTitle)} | ||
actions={[ | ||
<Button | ||
type="tertiary" | ||
id="cancel_save_without_exit" | ||
key="cancel_save_without_exit" | ||
onClick={() => { | ||
close(null) | ||
}} | ||
> | ||
{intl.formatMessage(modalMessages.cancel)} | ||
</Button>, | ||
<Button | ||
type="primary" | ||
key="confirm_save_without_exit" | ||
id="confirm_save_without_exit" | ||
onClick={() => { | ||
close(true) | ||
}} | ||
> | ||
{intl.formatMessage(modalMessages.confirm)} | ||
</Button> | ||
]} | ||
show={true} | ||
handleClose={() => close(null)} | ||
> | ||
<Stack> | ||
<Text variant="reg16" element="p" color="grey500"> | ||
{intl.formatMessage(modalMessages.exitWithoutSavingDescription)} | ||
</Text> | ||
</Stack> | ||
</ResponsiveModal> | ||
)) | ||
|
||
if (exitConfirm) { | ||
goToHome() | ||
} | ||
} | ||
|
||
return { exit, modal } | ||
} |
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,32 @@ | ||
/* | ||
* 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 { ReactNode, useState } from 'react' | ||
|
||
type CloseModal<ResultType> = (result: ResultType) => void | ||
|
||
type ModalFactory<ResultType> = (close: CloseModal<ResultType>) => ReactNode | ||
|
||
export function useModal() { | ||
const [modalNode, setModalNode] = useState<ReactNode>(null) | ||
|
||
function openModal<ModalResult>(modalFactory: ModalFactory<ModalResult>) { | ||
return new Promise<ModalResult>((resolve) => { | ||
function close(value: ModalResult) { | ||
resolve(value) | ||
setModalNode(null) | ||
} | ||
|
||
setModalNode(modalFactory(close)) | ||
}) | ||
} | ||
|
||
return [modalNode, openModal] as const | ||
} |