-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user creation to users list page
- Loading branch information
Showing
7 changed files
with
309 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
framework/core/js/src/admin/components/CreateUserModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
import app from '../../admin/app'; | ||
import Modal, { IInternalModalAttrs } from '../../common/components/Modal'; | ||
import Button from '../../common/components/Button'; | ||
import extractText from '../../common/utils/extractText'; | ||
import ItemList from '../../common/utils/ItemList'; | ||
import Stream from '../../common/utils/Stream'; | ||
import type Mithril from 'mithril'; | ||
import Switch from '../../common/components/Switch'; | ||
import Checkbox from '../../common/components/Checkbox'; | ||
|
||
export interface ICreateUserModalAttrs extends IInternalModalAttrs { | ||
username?: string; | ||
email?: string; | ||
password?: string; | ||
token?: string; | ||
provided?: string[]; | ||
} | ||
|
||
export type SignupBody = { | ||
username: string; | ||
email: string; | ||
isEmailConfirmed: boolean; | ||
password: string; | ||
}; | ||
|
||
export default class CreateUserModal<CustomAttrs extends ICreateUserModalAttrs = ICreateUserModalAttrs> extends Modal<CustomAttrs> { | ||
/** | ||
* The value of the username input. | ||
*/ | ||
username!: Stream<string>; | ||
|
||
/** | ||
* The value of the email input. | ||
*/ | ||
email!: Stream<string>; | ||
|
||
/** | ||
* The value of the password input. | ||
*/ | ||
password!: Stream<string>; | ||
|
||
/** | ||
* Whether email confirmation is required after signing in. | ||
*/ | ||
requireEmailConfirmation!: Stream<boolean>; | ||
|
||
/** | ||
* Keeps the modal open after the user is created to facilitate creating | ||
* multiple users at once. | ||
*/ | ||
bulkAdd!: Stream<boolean>; | ||
|
||
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) { | ||
super.oninit(vnode); | ||
|
||
this.username = Stream(''); | ||
this.email = Stream(''); | ||
this.password = Stream(''); | ||
this.requireEmailConfirmation = Stream(false); | ||
this.bulkAdd = Stream(false); | ||
} | ||
|
||
className() { | ||
return 'Modal--small CreateUserModal'; | ||
} | ||
|
||
title() { | ||
return app.translator.trans('core.admin.create_user.title'); | ||
} | ||
|
||
content() { | ||
return ( | ||
<> | ||
<div className="Modal-body">{this.body()}</div> | ||
</> | ||
); | ||
} | ||
|
||
body() { | ||
return ( | ||
<> | ||
<div className="Form Form--centered">{this.fields().toArray()}</div> | ||
</> | ||
); | ||
} | ||
|
||
fields() { | ||
const items = new ItemList(); | ||
|
||
const usernameLabel = extractText(app.translator.trans('core.admin.create_user.username_placeholder')); | ||
const emailLabel = extractText(app.translator.trans('core.admin.create_user.email_placeholder')); | ||
const emailConfirmationLabel = extractText(app.translator.trans('core.admin.create_user.email_confirmed_label')); | ||
const passwordLabel = extractText(app.translator.trans('core.admin.create_user.password_placeholder')); | ||
|
||
items.add( | ||
'username', | ||
<div className="Form-group"> | ||
<input | ||
className="FormControl" | ||
name="username" | ||
type="text" | ||
placeholder={usernameLabel} | ||
aria-label={usernameLabel} | ||
bidi={this.username} | ||
disabled={this.loading} | ||
/> | ||
</div>, | ||
100 | ||
); | ||
|
||
items.add( | ||
'email', | ||
<div className="Form-group"> | ||
<input | ||
className="FormControl" | ||
name="email" | ||
type="email" | ||
placeholder={emailLabel} | ||
aria-label={emailLabel} | ||
bidi={this.email} | ||
disabled={this.loading} | ||
/> | ||
</div>, | ||
80 | ||
); | ||
|
||
items.add( | ||
'emailConfirmation', | ||
<div className="Form-group"> | ||
<Switch | ||
name="emailConfirmed" | ||
state={this.requireEmailConfirmation()} | ||
onchange={(checked: boolean) => this.requireEmailConfirmation(checked)} | ||
disabled={this.loading} | ||
> | ||
{emailConfirmationLabel} | ||
</Switch> | ||
</div>, | ||
60 | ||
); | ||
|
||
items.add( | ||
'password', | ||
<div className="Form-group"> | ||
<input | ||
className="FormControl" | ||
name="password" | ||
type="password" | ||
autocomplete="new-password" | ||
placeholder={passwordLabel} | ||
aria-label={passwordLabel} | ||
bidi={this.password} | ||
disabled={this.loading} | ||
/> | ||
</div>, | ||
40 | ||
); | ||
|
||
items.add( | ||
'bulkAdd', | ||
<div className="Form-group CreateUserModal-bulkAdd"> | ||
<Switch name="bulkAdd" disabled={this.loading} state={this.bulkAdd()} onchange={(checked: boolean) => this.bulkAdd(checked)}> | ||
{app.translator.trans('core.admin.create_user.bulk_add_label')} | ||
</Switch> | ||
</div>, | ||
0 | ||
); | ||
|
||
items.add( | ||
'submit', | ||
<div className="Form-group"> | ||
<Button className="Button Button--primary Button--block" type="submit" loading={this.loading}> | ||
{app.translator.trans('core.admin.create_user.submit_button')} | ||
</Button> | ||
</div>, | ||
-10 | ||
); | ||
|
||
return items; | ||
} | ||
|
||
onready() { | ||
this.$('[name=username]').trigger('select'); | ||
} | ||
|
||
onsubmit(e: SubmitEvent) { | ||
e.preventDefault(); | ||
|
||
this.loading = true; | ||
|
||
app | ||
.request({ | ||
url: app.forum.attribute('apiUrl') + '/users', | ||
method: 'POST', | ||
body: { data: { attributes: this.submitData() } }, | ||
errorHandler: this.onerror.bind(this), | ||
}) | ||
.then(() => { | ||
this.loaded(); | ||
|
||
if (this.bulkAdd()) { | ||
this.resetData(); | ||
} else { | ||
this.hide(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Get the data that should be submitted in the sign-up request. | ||
*/ | ||
submitData(): SignupBody { | ||
const data = { | ||
username: this.username(), | ||
email: this.email(), | ||
isEmailConfirmed: !this.requireEmailConfirmation(), | ||
password: this.password(), | ||
}; | ||
|
||
return data; | ||
} | ||
|
||
resetData() { | ||
this.username(''); | ||
this.email(''); | ||
this.password(''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.CreateUserModal { | ||
&-bulkAdd { | ||
margin-top: 32px; | ||
margin-bottom: 24px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters