|
| 1 | +import app from '../../admin/app'; |
| 2 | +import Modal, { IInternalModalAttrs } from '../../common/components/Modal'; |
| 3 | +import Button from '../../common/components/Button'; |
| 4 | +import extractText from '../../common/utils/extractText'; |
| 5 | +import ItemList from '../../common/utils/ItemList'; |
| 6 | +import Stream from '../../common/utils/Stream'; |
| 7 | +import type Mithril from 'mithril'; |
| 8 | +import Switch from '../../common/components/Switch'; |
| 9 | +import { generateRandomString } from '../../common/utils/string'; |
| 10 | + |
| 11 | +export interface ICreateUserModalAttrs extends IInternalModalAttrs { |
| 12 | + username?: string; |
| 13 | + email?: string; |
| 14 | + password?: string; |
| 15 | + token?: string; |
| 16 | + provided?: string[]; |
| 17 | +} |
| 18 | + |
| 19 | +export type SignupBody = { |
| 20 | + username: string; |
| 21 | + email: string; |
| 22 | + isEmailConfirmed: boolean; |
| 23 | + password: string; |
| 24 | +}; |
| 25 | + |
| 26 | +export default class CreateUserModal<CustomAttrs extends ICreateUserModalAttrs = ICreateUserModalAttrs> extends Modal<CustomAttrs> { |
| 27 | + /** |
| 28 | + * The value of the username input. |
| 29 | + */ |
| 30 | + username!: Stream<string>; |
| 31 | + |
| 32 | + /** |
| 33 | + * The value of the email input. |
| 34 | + */ |
| 35 | + email!: Stream<string>; |
| 36 | + |
| 37 | + /** |
| 38 | + * The value of the password input. |
| 39 | + */ |
| 40 | + password!: Stream<string | null>; |
| 41 | + |
| 42 | + /** |
| 43 | + * Whether email confirmation is required after signing in. |
| 44 | + */ |
| 45 | + requireEmailConfirmation!: Stream<boolean>; |
| 46 | + |
| 47 | + /** |
| 48 | + * Keeps the modal open after the user is created to facilitate creating |
| 49 | + * multiple users at once. |
| 50 | + */ |
| 51 | + bulkAdd!: Stream<boolean>; |
| 52 | + |
| 53 | + oninit(vnode: Mithril.Vnode<CustomAttrs, this>) { |
| 54 | + super.oninit(vnode); |
| 55 | + |
| 56 | + this.username = Stream(''); |
| 57 | + this.email = Stream(''); |
| 58 | + this.password = Stream<string | null>(''); |
| 59 | + this.requireEmailConfirmation = Stream(false); |
| 60 | + this.bulkAdd = Stream(false); |
| 61 | + } |
| 62 | + |
| 63 | + className() { |
| 64 | + return 'Modal--small CreateUserModal'; |
| 65 | + } |
| 66 | + |
| 67 | + title() { |
| 68 | + return app.translator.trans('core.admin.create_user.title'); |
| 69 | + } |
| 70 | + |
| 71 | + content() { |
| 72 | + return ( |
| 73 | + <> |
| 74 | + <div className="Modal-body">{this.body()}</div> |
| 75 | + </> |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + body() { |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <div className="Form Form--centered">{this.fields().toArray()}</div> |
| 83 | + </> |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + fields() { |
| 88 | + const items = new ItemList(); |
| 89 | + |
| 90 | + const usernameLabel = extractText(app.translator.trans('core.admin.create_user.username_placeholder')); |
| 91 | + const emailLabel = extractText(app.translator.trans('core.admin.create_user.email_placeholder')); |
| 92 | + const emailConfirmationLabel = extractText(app.translator.trans('core.admin.create_user.email_confirmed_label')); |
| 93 | + const useRandomPasswordLabel = extractText(app.translator.trans('core.admin.create_user.use_random_password')); |
| 94 | + const passwordLabel = extractText(app.translator.trans('core.admin.create_user.password_placeholder')); |
| 95 | + |
| 96 | + items.add( |
| 97 | + 'username', |
| 98 | + <div className="Form-group"> |
| 99 | + <input |
| 100 | + className="FormControl" |
| 101 | + name="username" |
| 102 | + type="text" |
| 103 | + placeholder={usernameLabel} |
| 104 | + aria-label={usernameLabel} |
| 105 | + bidi={this.username} |
| 106 | + disabled={this.loading} |
| 107 | + /> |
| 108 | + </div>, |
| 109 | + 100 |
| 110 | + ); |
| 111 | + |
| 112 | + items.add( |
| 113 | + 'email', |
| 114 | + <div className="Form-group"> |
| 115 | + <input |
| 116 | + className="FormControl" |
| 117 | + name="email" |
| 118 | + type="email" |
| 119 | + placeholder={emailLabel} |
| 120 | + aria-label={emailLabel} |
| 121 | + bidi={this.email} |
| 122 | + disabled={this.loading} |
| 123 | + /> |
| 124 | + </div>, |
| 125 | + 80 |
| 126 | + ); |
| 127 | + |
| 128 | + items.add( |
| 129 | + 'password', |
| 130 | + <div className="Form-group"> |
| 131 | + <input |
| 132 | + className="FormControl" |
| 133 | + name="password" |
| 134 | + type="password" |
| 135 | + autocomplete="new-password" |
| 136 | + placeholder={passwordLabel} |
| 137 | + aria-label={passwordLabel} |
| 138 | + bidi={this.password} |
| 139 | + disabled={this.loading || this.password() === null} |
| 140 | + /> |
| 141 | + </div>, |
| 142 | + 60 |
| 143 | + ); |
| 144 | + |
| 145 | + items.add( |
| 146 | + 'emailConfirmation', |
| 147 | + <div className="Form-group"> |
| 148 | + <Switch |
| 149 | + name="emailConfirmed" |
| 150 | + state={this.requireEmailConfirmation()} |
| 151 | + onchange={(checked: boolean) => this.requireEmailConfirmation(checked)} |
| 152 | + disabled={this.loading} |
| 153 | + > |
| 154 | + {emailConfirmationLabel} |
| 155 | + </Switch> |
| 156 | + </div>, |
| 157 | + 40 |
| 158 | + ); |
| 159 | + |
| 160 | + items.add( |
| 161 | + 'useRandomPassword', |
| 162 | + <div className="Form-group"> |
| 163 | + <Switch |
| 164 | + name="useRandomPassword" |
| 165 | + state={this.password() === null} |
| 166 | + onchange={(enabled: boolean) => { |
| 167 | + this.password(enabled ? null : ''); |
| 168 | + }} |
| 169 | + disabled={this.loading} |
| 170 | + > |
| 171 | + {useRandomPasswordLabel} |
| 172 | + </Switch> |
| 173 | + </div>, |
| 174 | + 20 |
| 175 | + ); |
| 176 | + |
| 177 | + items.add( |
| 178 | + 'submit', |
| 179 | + <div className="Form-group"> |
| 180 | + <Button className="Button Button--primary Button--block" type="submit" loading={this.loading}> |
| 181 | + {app.translator.trans('core.admin.create_user.submit_button')} |
| 182 | + </Button> |
| 183 | + </div>, |
| 184 | + 0 |
| 185 | + ); |
| 186 | + |
| 187 | + items.add( |
| 188 | + 'submitAndAdd', |
| 189 | + <div className="Form-group"> |
| 190 | + <Button className="Button Button--block" onclick={() => this.bulkAdd(true) && this.onsubmit()} disabled={this.loading}> |
| 191 | + {app.translator.trans('core.admin.create_user.submit_and_create_another_button')} |
| 192 | + </Button> |
| 193 | + </div>, |
| 194 | + -20 |
| 195 | + ); |
| 196 | + |
| 197 | + return items; |
| 198 | + } |
| 199 | + |
| 200 | + onready() { |
| 201 | + this.$('[name=username]').trigger('select'); |
| 202 | + } |
| 203 | + |
| 204 | + onsubmit(e: SubmitEvent | null = null) { |
| 205 | + e?.preventDefault(); |
| 206 | + |
| 207 | + this.loading = true; |
| 208 | + |
| 209 | + app |
| 210 | + .request({ |
| 211 | + url: app.forum.attribute('apiUrl') + '/users', |
| 212 | + method: 'POST', |
| 213 | + body: { data: { attributes: this.submitData() } }, |
| 214 | + errorHandler: this.onerror.bind(this), |
| 215 | + }) |
| 216 | + .then(() => { |
| 217 | + if (this.bulkAdd()) { |
| 218 | + this.resetData(); |
| 219 | + } else { |
| 220 | + this.hide(); |
| 221 | + } |
| 222 | + }) |
| 223 | + .finally(() => { |
| 224 | + this.bulkAdd(false); |
| 225 | + this.loaded(); |
| 226 | + }); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Get the data that should be submitted in the sign-up request. |
| 231 | + */ |
| 232 | + submitData(): SignupBody { |
| 233 | + const data = { |
| 234 | + username: this.username(), |
| 235 | + email: this.email(), |
| 236 | + isEmailConfirmed: !this.requireEmailConfirmation(), |
| 237 | + password: this.password() ?? generateRandomString(32), |
| 238 | + }; |
| 239 | + |
| 240 | + return data; |
| 241 | + } |
| 242 | + |
| 243 | + resetData() { |
| 244 | + this.username(''); |
| 245 | + this.email(''); |
| 246 | + this.password(''); |
| 247 | + } |
| 248 | +} |
0 commit comments