|
| 1 | +import { |
| 2 | + Alert, |
| 3 | + Button, |
| 4 | + Modal, |
| 5 | + ModalSize, |
| 6 | + Switch, |
| 7 | + VariantType, |
| 8 | + useToastProvider, |
| 9 | +} from '@openfun/cunningham-react'; |
| 10 | +import { UseMutationResult } from '@tanstack/react-query'; |
| 11 | +import { useRouter } from 'next/navigation'; |
| 12 | +import { useState } from 'react'; |
| 13 | +import { useTranslation } from 'react-i18next'; |
| 14 | + |
| 15 | +import { APIError } from '@/api'; |
| 16 | +import { Box, Text } from '@/components'; |
| 17 | +import useCunninghamTheme from '@/cunningham/useCunninghamTheme'; |
| 18 | + |
| 19 | +import { KEY_DOC, KEY_LIST_DOC } from '../api'; |
| 20 | +import { useCreateDoc } from '../api/useCreateDoc'; |
| 21 | +import { useUpdateDoc } from '../api/useUpdateDoc'; |
| 22 | +import IconEdit from '../assets/icon-edit.svg'; |
| 23 | +import { Doc } from '../types'; |
| 24 | + |
| 25 | +import { InputDocName } from './InputDocName'; |
| 26 | + |
| 27 | +interface ModalCreateDocProps { |
| 28 | + onClose: () => void; |
| 29 | +} |
| 30 | + |
| 31 | +export const ModalCreateDoc = ({ onClose }: ModalCreateDocProps) => { |
| 32 | + const router = useRouter(); |
| 33 | + const api = useCreateDoc({ |
| 34 | + onSuccess: (doc) => { |
| 35 | + router.push(`/docs/${doc.id}`); |
| 36 | + }, |
| 37 | + }); |
| 38 | + const { t } = useTranslation(); |
| 39 | + |
| 40 | + return ( |
| 41 | + <ModalDoc |
| 42 | + {...{ |
| 43 | + buttonText: t('Create the document'), |
| 44 | + onClose, |
| 45 | + isPublic: false, |
| 46 | + titleModal: t('Create a new document'), |
| 47 | + validate: (title, is_public) => |
| 48 | + api.mutate({ |
| 49 | + is_public, |
| 50 | + title, |
| 51 | + }), |
| 52 | + ...api, |
| 53 | + }} |
| 54 | + /> |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +interface ModalUpdateDocProps { |
| 59 | + onClose: () => void; |
| 60 | + doc: Doc; |
| 61 | +} |
| 62 | + |
| 63 | +export const ModalUpdateDoc = ({ onClose, doc }: ModalUpdateDocProps) => { |
| 64 | + const { toast } = useToastProvider(); |
| 65 | + const { t } = useTranslation(); |
| 66 | + |
| 67 | + const api = useUpdateDoc({ |
| 68 | + onSuccess: () => { |
| 69 | + toast(t('The document has been updated.'), VariantType.SUCCESS, { |
| 70 | + duration: 4000, |
| 71 | + }); |
| 72 | + onClose(); |
| 73 | + }, |
| 74 | + listInvalideQueries: [KEY_DOC, KEY_LIST_DOC], |
| 75 | + }); |
| 76 | + |
| 77 | + return ( |
| 78 | + <ModalDoc |
| 79 | + {...{ |
| 80 | + buttonText: t('Validate the modification'), |
| 81 | + onClose, |
| 82 | + initialTitle: doc.title, |
| 83 | + isPublic: doc.is_public, |
| 84 | + infoText: t('Enter the new name of the selected document.'), |
| 85 | + titleModal: t('Update document "{{documentTitle}}"', { |
| 86 | + documentTitle: doc.title, |
| 87 | + }), |
| 88 | + validate: (title, is_public) => |
| 89 | + api.mutate({ |
| 90 | + is_public, |
| 91 | + title, |
| 92 | + id: doc.id, |
| 93 | + }), |
| 94 | + ...api, |
| 95 | + }} |
| 96 | + /> |
| 97 | + ); |
| 98 | +}; |
| 99 | + |
| 100 | +type ModalDoc<T> = { |
| 101 | + buttonText: string; |
| 102 | + isPublic: boolean; |
| 103 | + onClose: () => void; |
| 104 | + titleModal: string; |
| 105 | + validate: (title: string, is_public: boolean) => void; |
| 106 | + initialTitle?: string; |
| 107 | + infoText?: string; |
| 108 | +} & UseMutationResult<Doc, APIError<unknown>, T, unknown>; |
| 109 | + |
| 110 | +const ModalDoc = <T,>({ |
| 111 | + buttonText, |
| 112 | + infoText, |
| 113 | + initialTitle, |
| 114 | + isPublic, |
| 115 | + onClose, |
| 116 | + titleModal, |
| 117 | + validate, |
| 118 | + ...api |
| 119 | +}: ModalDoc<T>) => { |
| 120 | + const { colorsTokens } = useCunninghamTheme(); |
| 121 | + const { t } = useTranslation(); |
| 122 | + const [title, setTitle] = useState(initialTitle || ''); |
| 123 | + |
| 124 | + const [docPublic, setDocPublic] = useState(isPublic); |
| 125 | + |
| 126 | + return ( |
| 127 | + <Modal |
| 128 | + isOpen |
| 129 | + closeOnClickOutside |
| 130 | + hideCloseButton |
| 131 | + leftActions={ |
| 132 | + <Button |
| 133 | + aria-label={t('Close the modal')} |
| 134 | + color="secondary" |
| 135 | + fullWidth |
| 136 | + onClick={() => onClose()} |
| 137 | + > |
| 138 | + {t('Cancel')} |
| 139 | + </Button> |
| 140 | + } |
| 141 | + onClose={() => onClose()} |
| 142 | + rightActions={ |
| 143 | + <Button |
| 144 | + aria-label={buttonText} |
| 145 | + color="primary" |
| 146 | + fullWidth |
| 147 | + onClick={() => validate(title, docPublic)} |
| 148 | + > |
| 149 | + {buttonText} |
| 150 | + </Button> |
| 151 | + } |
| 152 | + size={ModalSize.MEDIUM} |
| 153 | + title={ |
| 154 | + <Box $align="center" $gap="1rem" $margin={{ bottom: '2.5rem' }}> |
| 155 | + <IconEdit width={48} color={colorsTokens()['primary-text']} /> |
| 156 | + <Text as="h2" $size="h3" $margin="none"> |
| 157 | + {titleModal} |
| 158 | + </Text> |
| 159 | + </Box> |
| 160 | + } |
| 161 | + > |
| 162 | + <Box $margin={{ bottom: 'xl' }} $gap="1rem"> |
| 163 | + {infoText && ( |
| 164 | + <Alert canClose={false} type={VariantType.INFO}> |
| 165 | + <Text>{infoText}</Text> |
| 166 | + </Alert> |
| 167 | + )} |
| 168 | + |
| 169 | + <Box $gap="1rem"> |
| 170 | + <InputDocName |
| 171 | + label={t('Document name')} |
| 172 | + defaultValue={title} |
| 173 | + {...{ |
| 174 | + error: api.error, |
| 175 | + isError: api.isError, |
| 176 | + isPending: api.isPending, |
| 177 | + setDocName: setTitle, |
| 178 | + }} |
| 179 | + /> |
| 180 | + <Switch |
| 181 | + label={t('Is it public ?')} |
| 182 | + labelSide="right" |
| 183 | + defaultChecked={docPublic} |
| 184 | + onChange={() => setDocPublic(!docPublic)} |
| 185 | + /> |
| 186 | + </Box> |
| 187 | + </Box> |
| 188 | + </Modal> |
| 189 | + ); |
| 190 | +}; |
0 commit comments