Skip to content

Commit

Permalink
feat(i18n): extract text from modals
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle committed Nov 20, 2024
1 parent 663a74a commit 338517b
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useTranslation } from 'react-i18next'

import { UnsavedChangesModal } from '~templates/NavigationPrompt'

import { usePaymentStore } from '~features/admin-form/create/builder-and-design/BuilderAndDesignDrawer/FieldListDrawer/field-panels/usePaymentStore'
Expand Down Expand Up @@ -93,12 +95,13 @@ export const useDirtyModal = () => {
export const DirtyModal = (): JSX.Element => {
const { isOpen, handleCancelNavigate, handleConfirmNavigate } =
useDirtyModal()
const { t } = useTranslation()

return (
<UnsavedChangesModal
isOpen={isOpen}
onClose={handleCancelNavigate}
cancelButtonText="No, return to editing"
cancelButtonText={t('features.adminForm.modals.dirty.cancelButtonText')}
onConfirm={handleConfirmNavigate}
onCancel={handleCancelNavigate}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ const FieldButtonGroup = ({
<IconButton
variant="clear"
colorScheme="secondary"
aria-label="Edit field"
aria-label={t('features.common.tooltip.editField')}
icon={<BiCog fontSize="1.25rem" />}
onClick={handleEditFieldClick}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useRef } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { BiCog, BiTrash } from 'react-icons/bi'
import { Box, ButtonGroup, Collapse, Flex } from '@chakra-ui/react'

Expand Down Expand Up @@ -147,11 +148,16 @@ const PaymentButtonGroup = ({
handleBuilderClick(false)
}
}, [handleBuilderClick, isMobile])
const { t } = useTranslation()

const {
deletePaymentModalDisclosure: { onOpen: onDeleteModalOpen },
} = useBuilderAndDesignContext()

const { deleteField, editField } = t('features.common.tooltip', {
returnObjects: true,
})

return (
<Flex
px={{ base: '0.75rem', md: '1.5rem' }}
Expand All @@ -164,15 +170,15 @@ const PaymentButtonGroup = ({
<IconButton
variant="clear"
colorScheme="secondary"
aria-label="Edit field"
aria-label={editField}
icon={<BiCog fontSize="1.25rem" />}
onClick={handleEditFieldClick}
/>
)}
<Tooltip label="Delete field">
<Tooltip label={deleteField}>
<IconButton
colorScheme="danger"
aria-label="Delete field"
aria-label={deleteField}
icon={<BiTrash fontSize="1.25rem" />}
onClick={onDeleteModalOpen}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { BiCog } from 'react-icons/bi'
import { Box, ButtonGroup, Collapse, Flex, IconButton } from '@chakra-ui/react'

Expand Down Expand Up @@ -37,6 +38,7 @@ import {
} from '../useFieldBuilderStore'

export const StartPageView = () => {
const { t } = useTranslation()
const isMobile = useIsMobile()
const { data: form, isLoading } = useCreateTabForm()
const setFieldBuilderToInactive = useFieldBuilderStore(
Expand Down Expand Up @@ -259,7 +261,7 @@ export const StartPageView = () => {
<IconButton
variant="clear"
colorScheme="secondary"
aria-label="Edit field"
aria-label={t('features.common.tooltip.editField')}
icon={<BiCog fontSize="1.25rem" />}
onClick={handleEditInstructionsClick}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,20 @@ export const DeleteFieldModal = (): JSX.Element => {
}
}, [deleteFieldMutation, onClose, stateData])

const {
title,
description: { field, logic },
confirmButtonText,
} = t('features.adminForm.modals.deleteField', { returnObjects: true })

return (
<Modal isOpen onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalCloseButton />
<ModalHeader>Delete field</ModalHeader>
<ModalHeader>{title}</ModalHeader>
<ModalBody>
<Text color="secondary.500">
{fieldIsInLogic
? `This field is used in your form logic, so deleting it may cause
your logic to stop working correctly. Are you sure you want to
delete this field?`
: `Are you sure you want to delete this field? This action
cannot be undone.`}
</Text>
<Text color="secondary.500">{fieldIsInLogic ? logic : field}</Text>
<UnorderedList
spacing="0.5rem"
listStyleType="none"
Expand Down Expand Up @@ -105,7 +104,7 @@ export const DeleteFieldModal = (): JSX.Element => {
onClick={handleDeleteConfirmation}
isLoading={deleteFieldMutation.isLoading}
>
Yes, delete field
{confirmButtonText}
</Button>
</ButtonGroup>
</ModalFooter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import {
ButtonGroup,
Modal,
Expand Down Expand Up @@ -28,6 +29,7 @@ export const DeletePaymentModal = (): JSX.Element => {
const {
deletePaymentModalDisclosure: { onClose },
} = useBuilderAndDesignContext()
const { t } = useTranslation()

const { deletePaymentFieldMutation } = useDeleteFormField()

Expand All @@ -44,29 +46,32 @@ export const DeletePaymentModal = (): JSX.Element => {
}
}, [deletePaymentFieldMutation, onClose, stateData, setFieldListTabIndex])

const {
title,
description: { payment: description },
confirmButtonText,
} = t('features.adminForm.modals.deleteField', { returnObjects: true })

return (
<Modal isOpen onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalCloseButton />
<ModalHeader>Delete field</ModalHeader>
<ModalHeader>{title}</ModalHeader>
<ModalBody>
<Text color="secondary.500">
Are you sure you want to delete payment field? This action can't be
undone.
</Text>
<Text color="secondary.500">{description}</Text>
</ModalBody>
<ModalFooter>
<ButtonGroup>
<Button variant="clear" colorScheme="secondary" onClick={onClose}>
Cancel
{t('features.common.cancel')}
</Button>
<Button
colorScheme="danger"
onClick={handleDeleteConfirmation}
isLoading={deletePaymentFieldMutation.isLoading}
>
Yes, delete field
{confirmButtonText}
</Button>
</ButtonGroup>
</ModalFooter>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/features/admin-form/en-sg.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { enSG as meta } from './meta'
import { enSG as modals } from './modals'
import { enSG as navbar } from './navbar'
import { enSG as sidebar } from './sidebar'

export const enSG = {
navbar,
sidebar,
meta,
modals,
}
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/features/admin-form/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './en-sg'
export { type Meta } from './meta'
export { type Modals } from './modals'
export { type Navbar } from './navbar'
export { type Fields } from './sidebar'
export { type HeaderAndInstructions } from './sidebar'
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/i18n/locales/features/admin-form/modals/en-sg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const enSG = {
deleteField: {
title: 'Delete field',
description: {
field:
'Are you sure you want to delete this field? This action cannot be undone.',
logic:
'This field is used in your form logic, so deleting it may cause your logic to stop working correctly. Are you sure you want to delete this field?',
payment:
"Are you sure you want to delete payment field? This action can't be undone.",
},
confirmButtonText: 'Yes, delete field',
},
unsavedChanges: {
title: 'You have unsaved changes',
description: 'Are you sure you want to leave? Your changes will be lost.',
confirmButtonText: 'Yes, discard changes',
cancelButtonText: 'No, stay on page',
},
dirty: {
cancelButtonText: 'No, return to editing',
},
}
22 changes: 22 additions & 0 deletions frontend/src/i18n/locales/features/admin-form/modals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export * from './en-sg'

export interface Modals {
deleteField: {
title: string
description: {
field: string
logic: string
payment: string
}
confirmButtonText: string
}
unsavedChanges: {
title: string
description: string
confirmButtonText: string
cancelButtonText: string
}
dirty: {
cancelButtonText: string
}
}
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/features/common/en-sg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const enSG: Common = {
tooltip: {
deleteField: 'Delete field',
duplicateField: 'Duplicate field',
editField: 'Edit field',
},
dropdown: {
placeholder: 'Select an option',
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/features/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface Common {
tooltip: {
deleteField: string
duplicateField: string
editField: string
}
dropdown: {
placeholder: string
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
type HeaderAndInstructions,
type Logic,
type Meta,
type Modals,
type Navbar,
type ThankYou,
} from './admin-form'
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Logic,
Login,
Meta,
Modals,
Navbar,
PublicForm,
ThankYou,
Expand All @@ -24,6 +25,7 @@ interface Translation {
}
navbar?: Navbar
meta?: Meta
modals?: Modals
}
common?: Common
publicForm?: PublicForm
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/templates/NavigationPrompt/NavigationPrompt.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { memo } from 'react'
import { useTranslation } from 'react-i18next'

import { UnsavedChangesModal } from './UnsavedChangesModal'
import { useNavigationPrompt } from './useNavigationPrompt'
Expand Down Expand Up @@ -26,8 +27,13 @@ export const NavigationPrompt = memo(
confirmButtonText = 'Yes, discard changes',
cancelButtonText = 'No, stay on page',
}: NavigationPromptProps) => {
const { t } = useTranslation()
const { isPromptShown, onCancel, onConfirm } = useNavigationPrompt(when)

const defaultText = t('features.adminForm.modals.unsavedChanges', {
returnObjects: true,
})

return (
<UnsavedChangesModal
onCancel={onCancel}
Expand Down

0 comments on commit 338517b

Please sign in to comment.