|
1 | 1 | import { DialogAppSDK } from '@contentful/app-sdk'; |
2 | | -import { Paragraph } from '@contentful/f36-components'; |
3 | | -import { /* useCMA, */ useSDK } from '@contentful/react-apps-toolkit'; |
| 2 | +import { Button, Flex, Form, FormControl, Select } from '@contentful/f36-components'; |
| 3 | +import { useAutoResizer, useSDK } from '@contentful/react-apps-toolkit'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import LocaleMultiSelect from '../components/LocaleMultiSelect'; |
| 6 | +import { |
| 7 | + SimplifiedLocale, |
| 8 | + mapLocaleNamesToSimplifiedLocales, |
| 9 | + normalizeLocaleCode, |
| 10 | +} from '../utils/locales'; |
| 11 | +import { styles } from './Dialog.styles'; |
4 | 12 |
|
5 | 13 | const Dialog = () => { |
6 | 14 | const sdk = useSDK<DialogAppSDK>(); |
| 15 | + const [selectedSourceLocale, setSelectedSourceLocale] = useState<string | null>(null); |
| 16 | + const [selectedTargetLocales, setSelectedTargetLocales] = useState<SimplifiedLocale[]>([]); |
| 17 | + const [missingSourceLocale, setMissingSourceLocale] = useState(false); |
| 18 | + const [missingTargetLocales, setMissingTargetLocales] = useState(false); |
7 | 19 |
|
8 | | - return <Paragraph>Hello Dialog Component (AppId: {sdk.ids.app})</Paragraph>; |
| 20 | + const mappedLocales = mapLocaleNamesToSimplifiedLocales(sdk.locales.names); |
| 21 | + |
| 22 | + useAutoResizer(); |
| 23 | + |
| 24 | + const handlePopulateFields = () => { |
| 25 | + if (!selectedSourceLocale || selectedTargetLocales.length === 0) { |
| 26 | + setMissingSourceLocale(!selectedSourceLocale); |
| 27 | + setMissingTargetLocales(selectedTargetLocales.length === 0); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // todo : copy and paste logic to populate fields |
| 32 | + |
| 33 | + sdk.close(); |
| 34 | + }; |
| 35 | + |
| 36 | + return ( |
| 37 | + <Form> |
| 38 | + <Flex |
| 39 | + flexDirection="column" |
| 40 | + justifyContent="space-between" |
| 41 | + marginTop="spacingM" |
| 42 | + marginRight="spacingL" |
| 43 | + marginLeft="spacingL" |
| 44 | + marginBottom="spacingM" |
| 45 | + className={styles.container}> |
| 46 | + <Flex flexDirection="column"> |
| 47 | + <FormControl isRequired isInvalid={missingSourceLocale}> |
| 48 | + <FormControl.Label>Select source locale</FormControl.Label> |
| 49 | + <Select |
| 50 | + id="source-locale" |
| 51 | + name="source-locale" |
| 52 | + testId="source-locale-select" |
| 53 | + onChange={(event) => setSelectedSourceLocale(event.target.value)}> |
| 54 | + {!selectedSourceLocale && ( |
| 55 | + <Select.Option key={`select-locale-empty`} value={undefined}> |
| 56 | + Select one |
| 57 | + </Select.Option> |
| 58 | + )} |
| 59 | + {mappedLocales.map((locale) => ( |
| 60 | + <Select.Option |
| 61 | + key={`select-locale-${normalizeLocaleCode(locale.code)}`} |
| 62 | + data-test-id={`select-locale-${normalizeLocaleCode(locale.code)}`} |
| 63 | + value={locale.code}> |
| 64 | + {locale.name} |
| 65 | + </Select.Option> |
| 66 | + ))} |
| 67 | + </Select> |
| 68 | + {missingSourceLocale && ( |
| 69 | + <FormControl.ValidationMessage>Select source locale</FormControl.ValidationMessage> |
| 70 | + )} |
| 71 | + </FormControl> |
| 72 | + <FormControl isRequired isInvalid={missingTargetLocales}> |
| 73 | + <FormControl.Label>Select target locales to populate</FormControl.Label> |
| 74 | + <LocaleMultiSelect |
| 75 | + availableLocales={mappedLocales} |
| 76 | + selectedLocales={selectedTargetLocales} |
| 77 | + onSelectionChange={setSelectedTargetLocales} |
| 78 | + isInvalid={missingTargetLocales} |
| 79 | + /> |
| 80 | + {missingTargetLocales && ( |
| 81 | + <FormControl.ValidationMessage>Select target locales</FormControl.ValidationMessage> |
| 82 | + )} |
| 83 | + </FormControl> |
| 84 | + </Flex> |
| 85 | + <Flex justifyContent="flex-end" gap="spacingM"> |
| 86 | + <Button |
| 87 | + onClick={() => { |
| 88 | + sdk.close(); |
| 89 | + }}> |
| 90 | + Cancel |
| 91 | + </Button> |
| 92 | + <Button variant="primary" onClick={() => handlePopulateFields()}> |
| 93 | + Populate fields |
| 94 | + </Button> |
| 95 | + </Flex> |
| 96 | + </Flex> |
| 97 | + </Form> |
| 98 | + ); |
9 | 99 | }; |
10 | 100 |
|
11 | 101 | export default Dialog; |
0 commit comments