|
| 1 | +import i18n from '@dhis2/d2-i18n' |
| 2 | +import { Button, Modal, ModalTitle, ModalContent } from '@dhis2/ui' |
| 3 | +import { getInstance as getD2 } from 'd2' |
| 4 | +import FormBuilder from 'd2-ui/lib/forms/FormBuilder.component.js' |
| 5 | +import { isUrlArray, isRequired } from 'd2-ui/lib/forms/Validators.js' |
| 6 | +import PropTypes from 'prop-types' |
| 7 | +import React from 'react' |
| 8 | +import MultiToggle from '../form-fields/multi-toggle.jsx' |
| 9 | +import TextField from '../form-fields/text-field.jsx' |
| 10 | +import styles from './ClientForm.module.css' |
| 11 | + |
| 12 | +const formFieldStyle = { |
| 13 | + width: '100%', |
| 14 | +} |
| 15 | + |
| 16 | +const validateClientID = async (v) => { |
| 17 | + const d2 = await getD2() |
| 18 | + const list = await d2.models.oAuth2Clients.list({ |
| 19 | + paging: false, |
| 20 | + filter: [`cid:eq:${v}`], |
| 21 | + }) |
| 22 | + if (list.size > 0) { |
| 23 | + throw i18n.t('This client ID is already taken') |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const ClientForm = ({ clientModel, onUpdate, onSave, onCancel }) => { |
| 28 | + const grantTypes = ((clientModel && clientModel.grantTypes) || []).reduce( |
| 29 | + (curr, prev) => { |
| 30 | + curr[prev] = true |
| 31 | + return curr |
| 32 | + }, |
| 33 | + {} |
| 34 | + ) |
| 35 | + |
| 36 | + const fields = [ |
| 37 | + { |
| 38 | + name: 'name', |
| 39 | + value: clientModel.name, |
| 40 | + component: TextField, |
| 41 | + props: { |
| 42 | + floatingLabelText: i18n.t('Name'), |
| 43 | + style: formFieldStyle, |
| 44 | + changeEvent: 'onBlur', |
| 45 | + }, |
| 46 | + validators: [ |
| 47 | + { |
| 48 | + validator: isRequired, |
| 49 | + message: i18n.t('Required'), |
| 50 | + }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + { |
| 54 | + name: 'cid', |
| 55 | + value: clientModel.cid, |
| 56 | + component: TextField, |
| 57 | + props: { |
| 58 | + floatingLabelText: i18n.t('Client ID'), |
| 59 | + style: formFieldStyle, |
| 60 | + changeEvent: 'onBlur', |
| 61 | + }, |
| 62 | + validators: [ |
| 63 | + { |
| 64 | + validator: isRequired, |
| 65 | + message: i18n.t('Required'), |
| 66 | + }, |
| 67 | + { |
| 68 | + validator: (v) => v.toString().trim().length > 0, |
| 69 | + message: i18n.t('Required'), |
| 70 | + }, |
| 71 | + ], |
| 72 | + asyncValidators: [validateClientID], |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'secret', |
| 76 | + value: clientModel && clientModel.secret, |
| 77 | + component: TextField, |
| 78 | + props: { |
| 79 | + floatingLabelText: i18n.t('Client Secret'), |
| 80 | + disabled: true, |
| 81 | + style: formFieldStyle, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'grantTypes', |
| 86 | + component: MultiToggle, |
| 87 | + style: formFieldStyle, |
| 88 | + props: { |
| 89 | + label: i18n.t('Grant Types'), |
| 90 | + items: [ |
| 91 | + { |
| 92 | + name: 'password', |
| 93 | + text: i18n.t('Password'), |
| 94 | + value: grantTypes.password, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: 'refresh_token', |
| 98 | + text: i18n.t('Refresh token'), |
| 99 | + value: grantTypes.refresh_token, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: 'authorization_code', |
| 103 | + text: i18n.t('Authorization code'), |
| 104 | + value: grantTypes.authorization_code, |
| 105 | + }, |
| 106 | + ], |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: 'redirectUris', |
| 111 | + value: (clientModel.redirectUris || []).join('\n'), |
| 112 | + component: TextField, |
| 113 | + props: { |
| 114 | + hintText: i18n.t('One URL per line'), |
| 115 | + floatingLabelText: i18n.t('Redirect URIs'), |
| 116 | + multiLine: true, |
| 117 | + style: formFieldStyle, |
| 118 | + changeEvent: 'onBlur', |
| 119 | + }, |
| 120 | + validators: [ |
| 121 | + { |
| 122 | + validator: isUrlArray, |
| 123 | + message: i18n.t('This field should contain a list of URLs'), |
| 124 | + }, |
| 125 | + ], |
| 126 | + }, |
| 127 | + ] |
| 128 | + |
| 129 | + const headerText = |
| 130 | + clientModel.id === undefined |
| 131 | + ? i18n.t('Create new OAuth2 Client') |
| 132 | + : i18n.t('Edit OAuth2 Client') |
| 133 | + return ( |
| 134 | + <Modal onClose={onCancel}> |
| 135 | + <ModalTitle>{headerText}</ModalTitle> |
| 136 | + <ModalContent> |
| 137 | + <FormBuilder fields={fields} onUpdateField={onUpdate} /> |
| 138 | + <div style={{ marginTop: '1rem' }}> |
| 139 | + <Button primary onClick={onSave}> |
| 140 | + {i18n.t('Save')} |
| 141 | + </Button> |
| 142 | + <Button |
| 143 | + secondary |
| 144 | + onClick={onCancel} |
| 145 | + className={styles.cancelBtn} |
| 146 | + > |
| 147 | + {i18n.t('Cancel')} |
| 148 | + </Button> |
| 149 | + </div> |
| 150 | + </ModalContent> |
| 151 | + </Modal> |
| 152 | + ) |
| 153 | +} |
| 154 | + |
| 155 | +ClientForm.propTypes = { |
| 156 | + clientModel: PropTypes.object.isRequired, |
| 157 | + onCancel: PropTypes.func.isRequired, |
| 158 | + onSave: PropTypes.func.isRequired, |
| 159 | + onUpdate: PropTypes.func.isRequired, |
| 160 | +} |
| 161 | + |
| 162 | +export default ClientForm |
0 commit comments