|
| 1 | +import { useCallback } from 'react'; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Modal, |
| 5 | + TextInput, |
| 6 | +} from '@ifrc-go/ui'; |
| 7 | +import { |
| 8 | + useBooleanState, |
| 9 | + useTranslation, |
| 10 | +} from '@ifrc-go/ui/hooks'; |
| 11 | +import { |
| 12 | + resolveToComponent, |
| 13 | + resolveToString, |
| 14 | +} from '@ifrc-go/ui/utils'; |
| 15 | +import { |
| 16 | + isDefined, |
| 17 | + isNotDefined, |
| 18 | +} from '@togglecorp/fujs'; |
| 19 | + |
| 20 | +import Link from '#components/Link'; |
| 21 | +import useInputState from '#hooks/useInputState'; |
| 22 | +import { useLazyRequest } from '#utils/restRequest'; |
| 23 | + |
| 24 | +import TokenDetails from '../TokenDetails'; |
| 25 | + |
| 26 | +import i18n from './i18n.json'; |
| 27 | +import styles from './styles.module.css'; |
| 28 | + |
| 29 | +const MIN_TITLE_LENGTH = 3; |
| 30 | + |
| 31 | +interface Props { |
| 32 | + onClose: () => void; |
| 33 | +} |
| 34 | + |
| 35 | +function GenerateMontandonTokenModal(props: Props) { |
| 36 | + const { onClose } = props; |
| 37 | + const [accepted, { setTrue: setAcceptedTrue }] = useBooleanState(false); |
| 38 | + const strings = useTranslation(i18n); |
| 39 | + |
| 40 | + const [tokenTitle, setTokenTitle] = useInputState<string | undefined>(undefined); |
| 41 | + |
| 42 | + const { |
| 43 | + pending: tokenPending, |
| 44 | + response: tokenResponse, |
| 45 | + error: tokenError, |
| 46 | + trigger: triggerGenerateTokenRequest, |
| 47 | + } = useLazyRequest({ |
| 48 | + method: 'POST', |
| 49 | + url: '/api/v2/external-token/', |
| 50 | + body: (context: string) => ({ |
| 51 | + title: context, |
| 52 | + expire_timestamp: undefined, |
| 53 | + }), |
| 54 | + }); |
| 55 | + |
| 56 | + const handleAcceptAndGenerateClick = useCallback( |
| 57 | + (title: string | undefined) => { |
| 58 | + if (isDefined(title) && title.length >= MIN_TITLE_LENGTH) { |
| 59 | + setAcceptedTrue(); |
| 60 | + triggerGenerateTokenRequest(title); |
| 61 | + } |
| 62 | + }, |
| 63 | + [setAcceptedTrue, triggerGenerateTokenRequest], |
| 64 | + ); |
| 65 | + |
| 66 | + return ( |
| 67 | + <Modal |
| 68 | + className={styles.generateMontandonTokenModal} |
| 69 | + heading={strings.title} |
| 70 | + onClose={onClose} |
| 71 | + headerDescription={!accepted && resolveToComponent( |
| 72 | + strings.description, |
| 73 | + { |
| 74 | + termsLink: ( |
| 75 | + <Link |
| 76 | + external |
| 77 | + // TODO: use actual link |
| 78 | + href="https://github.com/IFRCGo" |
| 79 | + withLinkIcon |
| 80 | + withUnderline |
| 81 | + > |
| 82 | + {strings.termsAndConditionLabel} |
| 83 | + </Link> |
| 84 | + ), |
| 85 | + }, |
| 86 | + )} |
| 87 | + size="sm" |
| 88 | + contentViewType="vertical" |
| 89 | + spacing="comfortable" |
| 90 | + footerActions={( |
| 91 | + <> |
| 92 | + {!accepted && ( |
| 93 | + <> |
| 94 | + <Button |
| 95 | + name={undefined} |
| 96 | + onClick={onClose} |
| 97 | + variant="secondary" |
| 98 | + > |
| 99 | + {strings.cancelButtonLabel} |
| 100 | + </Button> |
| 101 | + <Button |
| 102 | + name={tokenTitle} |
| 103 | + onClick={handleAcceptAndGenerateClick} |
| 104 | + disabled={isNotDefined(tokenTitle) |
| 105 | + || tokenTitle.trim().length < MIN_TITLE_LENGTH} |
| 106 | + > |
| 107 | + {strings.acceptButtonLabel} |
| 108 | + </Button> |
| 109 | + </> |
| 110 | + )} |
| 111 | + {accepted && ( |
| 112 | + <Button |
| 113 | + name={undefined} |
| 114 | + onClick={onClose} |
| 115 | + > |
| 116 | + {strings.doneButtonLabel} |
| 117 | + </Button> |
| 118 | + )} |
| 119 | + </> |
| 120 | + )} |
| 121 | + pending={tokenPending} |
| 122 | + errored={isDefined(tokenError)} |
| 123 | + errorMessage={tokenError?.value.messageForNotification} |
| 124 | + > |
| 125 | + {!accepted && ( |
| 126 | + <TextInput |
| 127 | + name="tokenTitle" |
| 128 | + label={strings.titleInputLabel} |
| 129 | + value={tokenTitle} |
| 130 | + onChange={setTokenTitle} |
| 131 | + hint={ |
| 132 | + resolveToString( |
| 133 | + strings.titleInputHint, |
| 134 | + { minCharacters: MIN_TITLE_LENGTH }, |
| 135 | + ) |
| 136 | + } |
| 137 | + placeholder={strings.titleInputPlaceholder} |
| 138 | + /> |
| 139 | + )} |
| 140 | + {accepted && ( |
| 141 | + <> |
| 142 | + <TokenDetails |
| 143 | + data={tokenResponse} |
| 144 | + /> |
| 145 | + <div className={styles.note}> |
| 146 | + {strings.tokenNote} |
| 147 | + </div> |
| 148 | + </> |
| 149 | + )} |
| 150 | + </Modal> |
| 151 | + ); |
| 152 | +} |
| 153 | + |
| 154 | +export default GenerateMontandonTokenModal; |
0 commit comments