mirror of
https://github.com/flarum/core.git
synced 2025-08-04 23:47:32 +02:00
Modal typescript cleanup and conversions
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
import app from '../../admin/app';
|
import app from '../../admin/app';
|
||||||
import Modal, { IInternalModalAttrs } from '../../common/components/Modal';
|
import Modal, { IInternalModalAttrs } from '../../common/components/Modal';
|
||||||
|
|
||||||
export default class LoadingModal<ModalAttrs extends IInternalModalAttrs = IInternalModalAttrs> extends Modal<ModalAttrs> {
|
export interface ILoadingModalAttrs extends IInternalModalAttrs {}
|
||||||
|
|
||||||
|
export default class LoadingModal<ModalAttrs extends ILoadingModalAttrs = ILoadingModalAttrs> extends Modal<ModalAttrs> {
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
|
@@ -1,11 +1,21 @@
|
|||||||
import app from '../../admin/app';
|
import app from '../../admin/app';
|
||||||
import Modal from '../../common/components/Modal';
|
import Modal, { IInternalModalAttrs } from '../../common/components/Modal';
|
||||||
import LoadingIndicator from '../../common/components/LoadingIndicator';
|
import LoadingIndicator from '../../common/components/LoadingIndicator';
|
||||||
import Placeholder from '../../common/components/Placeholder';
|
import Placeholder from '../../common/components/Placeholder';
|
||||||
import ExtensionReadme from '../models/ExtensionReadme';
|
import ExtensionReadme from '../models/ExtensionReadme';
|
||||||
|
import type Mithril from 'mithril';
|
||||||
|
import { Extension } from '../AdminApplication';
|
||||||
|
|
||||||
export default class ReadmeModal extends Modal {
|
export interface IReadmeModalAttrs extends IInternalModalAttrs {
|
||||||
oninit(vnode) {
|
extension: Extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class ReadmeModal<CustomAttrs extends IReadmeModalAttrs = IReadmeModalAttrs> extends Modal<CustomAttrs> {
|
||||||
|
protected name!: string;
|
||||||
|
protected extName!: string;
|
||||||
|
protected readme!: ExtensionReadme;
|
||||||
|
|
||||||
|
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
|
||||||
super.oninit(vnode);
|
super.oninit(vnode);
|
||||||
|
|
||||||
app.store.models['extension-readmes'] = ExtensionReadme;
|
app.store.models['extension-readmes'] = ExtensionReadme;
|
@@ -32,11 +32,11 @@ export interface SavedModelData {
|
|||||||
|
|
||||||
export type ModelData = UnsavedModelData | SavedModelData;
|
export type ModelData = UnsavedModelData | SavedModelData;
|
||||||
|
|
||||||
interface SaveRelationships {
|
export interface SaveRelationships {
|
||||||
[relationship: string]: Model | Model[];
|
[relationship: string]: Model | Model[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SaveAttributes {
|
export interface SaveAttributes {
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
relationships?: SaveRelationships;
|
relationships?: SaveRelationships;
|
||||||
}
|
}
|
||||||
|
@@ -7,11 +7,8 @@ export type LoginParams = {
|
|||||||
* The username/email
|
* The username/email
|
||||||
*/
|
*/
|
||||||
identification: string;
|
identification: string;
|
||||||
|
|
||||||
/**
|
|
||||||
* Password
|
|
||||||
*/
|
|
||||||
password: string;
|
password: string;
|
||||||
|
remember: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,17 +1,28 @@
|
|||||||
import app from '../../common/app';
|
import app from '../../common/app';
|
||||||
import Modal from './Modal';
|
import Modal, { IInternalModalAttrs } from './Modal';
|
||||||
import Button from './Button';
|
import Button from './Button';
|
||||||
import GroupBadge from './GroupBadge';
|
import GroupBadge from './GroupBadge';
|
||||||
import Group from '../models/Group';
|
import Group from '../models/Group';
|
||||||
import extractText from '../utils/extractText';
|
import extractText from '../utils/extractText';
|
||||||
import ItemList from '../utils/ItemList';
|
import ItemList from '../utils/ItemList';
|
||||||
import Stream from '../utils/Stream';
|
import Stream from '../utils/Stream';
|
||||||
|
import Mithril from 'mithril';
|
||||||
|
import User from '../models/User';
|
||||||
|
import { SaveAttributes, SaveRelationships } from '../Model';
|
||||||
|
|
||||||
/**
|
export interface IEditUserModalAttrs extends IInternalModalAttrs {
|
||||||
* The `EditUserModal` component displays a modal dialog with a login form.
|
user: User;
|
||||||
*/
|
}
|
||||||
export default class EditUserModal extends Modal {
|
|
||||||
oninit(vnode) {
|
export default class EditUserModal<CustomAttrs extends IEditUserModalAttrs = IEditUserModalAttrs> extends Modal<CustomAttrs> {
|
||||||
|
protected username!: Stream<string>;
|
||||||
|
protected email!: Stream<string>;
|
||||||
|
protected isEmailConfirmed!: Stream<boolean>;
|
||||||
|
protected setPassword!: Stream<boolean>;
|
||||||
|
protected password!: Stream<string>;
|
||||||
|
protected groups: Record<string, Stream<boolean>> = {};
|
||||||
|
|
||||||
|
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
|
||||||
super.oninit(vnode);
|
super.oninit(vnode);
|
||||||
|
|
||||||
const user = this.attrs.user;
|
const user = this.attrs.user;
|
||||||
@@ -19,14 +30,15 @@ export default class EditUserModal extends Modal {
|
|||||||
this.username = Stream(user.username() || '');
|
this.username = Stream(user.username() || '');
|
||||||
this.email = Stream(user.email() || '');
|
this.email = Stream(user.email() || '');
|
||||||
this.isEmailConfirmed = Stream(user.isEmailConfirmed() || false);
|
this.isEmailConfirmed = Stream(user.isEmailConfirmed() || false);
|
||||||
this.setPassword = Stream(false);
|
this.setPassword = Stream(false as boolean);
|
||||||
this.password = Stream(user.password() || '');
|
this.password = Stream(user.password() || '');
|
||||||
this.groups = {};
|
|
||||||
|
const userGroups = user.groups() || [];
|
||||||
|
|
||||||
app.store
|
app.store
|
||||||
.all('groups')
|
.all<Group>('groups')
|
||||||
.filter((group) => [Group.GUEST_ID, Group.MEMBER_ID].indexOf(group.id()) === -1)
|
.filter((group) => ![Group.GUEST_ID, Group.MEMBER_ID].includes(group.id()!))
|
||||||
.forEach((group) => (this.groups[group.id()] = Stream(user.groups().indexOf(group) !== -1)));
|
.forEach((group) => (this.groups[group.id()!] = Stream(userGroups.includes(group))));
|
||||||
}
|
}
|
||||||
|
|
||||||
className() {
|
className() {
|
||||||
@@ -49,7 +61,7 @@ export default class EditUserModal extends Modal {
|
|||||||
fields() {
|
fields() {
|
||||||
const items = new ItemList();
|
const items = new ItemList();
|
||||||
|
|
||||||
if (app.session.user.canEditCredentials()) {
|
if (app.session.user?.canEditCredentials()) {
|
||||||
items.add(
|
items.add(
|
||||||
'username',
|
'username',
|
||||||
<div className="Form-group">
|
<div className="Form-group">
|
||||||
@@ -103,10 +115,11 @@ export default class EditUserModal extends Modal {
|
|||||||
<label className="checkbox">
|
<label className="checkbox">
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
onchange={(e) => {
|
onchange={(e: KeyboardEvent) => {
|
||||||
this.setPassword(e.target.checked);
|
const target = e.target as HTMLInputElement;
|
||||||
|
this.setPassword(target.checked);
|
||||||
m.redraw.sync();
|
m.redraw.sync();
|
||||||
if (e.target.checked) this.$('[name=password]').select();
|
if (target.checked) this.$('[name=password]').select();
|
||||||
e.redraw = false;
|
e.redraw = false;
|
||||||
}}
|
}}
|
||||||
disabled={this.nonAdminEditingAdmin()}
|
disabled={this.nonAdminEditingAdmin()}
|
||||||
@@ -132,24 +145,31 @@ export default class EditUserModal extends Modal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (app.session.user.canEditGroups()) {
|
if (app.session.user?.canEditGroups()) {
|
||||||
items.add(
|
items.add(
|
||||||
'groups',
|
'groups',
|
||||||
<div className="Form-group EditUserModal-groups">
|
<div className="Form-group EditUserModal-groups">
|
||||||
<label>{app.translator.trans('core.lib.edit_user.groups_heading')}</label>
|
<label>{app.translator.trans('core.lib.edit_user.groups_heading')}</label>
|
||||||
<div>
|
<div>
|
||||||
{Object.keys(this.groups)
|
{Object.keys(this.groups)
|
||||||
.map((id) => app.store.getById('groups', id))
|
.map((id) => app.store.getById<Group>('groups', id))
|
||||||
.map((group) => (
|
.filter(Boolean)
|
||||||
<label className="checkbox">
|
.map(
|
||||||
<input
|
(group) =>
|
||||||
type="checkbox"
|
// Necessary because filter(Boolean) doesn't narrow out falsy values.
|
||||||
bidi={this.groups[group.id()]}
|
group && (
|
||||||
disabled={group.id() === Group.ADMINISTRATOR_ID && (this.attrs.user === app.session.user || !this.userIsAdmin(app.session.user))}
|
<label className="checkbox">
|
||||||
/>
|
<input
|
||||||
{GroupBadge.component({ group, label: '' })} {group.nameSingular()}
|
type="checkbox"
|
||||||
</label>
|
bidi={this.groups[group.id()!]}
|
||||||
))}
|
disabled={
|
||||||
|
group.id() === Group.ADMINISTRATOR_ID && (this.attrs.user === app.session.user || !this.userIsAdmin(app.session.user))
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
{GroupBadge.component({ group, label: '' })} {group.nameSingular()}
|
||||||
|
</label>
|
||||||
|
)
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>,
|
</div>,
|
||||||
10
|
10
|
||||||
@@ -194,9 +214,8 @@ export default class EditUserModal extends Modal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
const data = {
|
const data: SaveAttributes = {};
|
||||||
relationships: {},
|
const relationships: SaveRelationships = {};
|
||||||
};
|
|
||||||
|
|
||||||
if (this.attrs.user.canEditCredentials() && !this.nonAdminEditingAdmin()) {
|
if (this.attrs.user.canEditCredentials() && !this.nonAdminEditingAdmin()) {
|
||||||
data.username = this.username();
|
data.username = this.username();
|
||||||
@@ -211,15 +230,18 @@ export default class EditUserModal extends Modal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.attrs.user.canEditGroups()) {
|
if (this.attrs.user.canEditGroups()) {
|
||||||
data.relationships.groups = Object.keys(this.groups)
|
relationships.groups = Object.keys(this.groups)
|
||||||
.filter((id) => this.groups[id]())
|
.filter((id) => this.groups[id]())
|
||||||
.map((id) => app.store.getById('groups', id));
|
.map((id) => app.store.getById<Group>('groups', id))
|
||||||
|
.filter((g): g is Group => g instanceof Group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data.relationships = relationships;
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
onsubmit(e) {
|
onsubmit(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
@@ -239,9 +261,8 @@ export default class EditUserModal extends Modal {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
* @protected
|
|
||||||
*/
|
*/
|
||||||
userIsAdmin(user) {
|
protected userIsAdmin(user: User | null) {
|
||||||
return user.groups().some((g) => g.id() === Group.ADMINISTRATOR_ID);
|
return user && (user.groups() || []).some((g) => g?.id() === Group.ADMINISTRATOR_ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -32,7 +32,7 @@ export default abstract class Modal<ModalAttrs extends IInternalModalAttrs = IIn
|
|||||||
*/
|
*/
|
||||||
alertAttrs: AlertAttrs | null = null;
|
alertAttrs: AlertAttrs | null = null;
|
||||||
|
|
||||||
oninit(vnode: Mithril.VnodeDOM<ModalAttrs, this>) {
|
oninit(vnode: Mithril.Vnode<ModalAttrs, this>) {
|
||||||
super.oninit(vnode);
|
super.oninit(vnode);
|
||||||
|
|
||||||
// TODO: [Flarum 2.0] Remove the code below.
|
// TODO: [Flarum 2.0] Remove the code below.
|
||||||
|
@@ -1,6 +1,12 @@
|
|||||||
import Modal from './Modal';
|
import RequestError from '../utils/RequestError';
|
||||||
|
import Modal, { IInternalModalAttrs } from './Modal';
|
||||||
|
|
||||||
export default class RequestErrorModal extends Modal {
|
export interface IRequestErrorModalAttrs extends IInternalModalAttrs {
|
||||||
|
error: RequestError;
|
||||||
|
formattedError: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export default class RequestErrorModal<CustomAttrs extends IRequestErrorModalAttrs = IRequestErrorModalAttrs> extends Modal<CustomAttrs> {
|
||||||
className() {
|
className() {
|
||||||
return 'RequestErrorModal Modal--large';
|
return 'RequestErrorModal Modal--large';
|
||||||
}
|
}
|
||||||
@@ -18,14 +24,10 @@ export default class RequestErrorModal extends Modal {
|
|||||||
// else try to parse it as JSON and stringify it with indentation
|
// else try to parse it as JSON and stringify it with indentation
|
||||||
if (formattedError) {
|
if (formattedError) {
|
||||||
responseText = formattedError.join('\n\n');
|
responseText = formattedError.join('\n\n');
|
||||||
|
} else if (error.response) {
|
||||||
|
responseText = JSON.stringify(error.response, null, 2);
|
||||||
} else {
|
} else {
|
||||||
try {
|
responseText = error.responseText;
|
||||||
const json = error.response || JSON.parse(error.responseText);
|
|
||||||
|
|
||||||
responseText = JSON.stringify(json, null, 2);
|
|
||||||
} catch (e) {
|
|
||||||
responseText = error.responseText;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
@@ -125,7 +125,7 @@ export default class ForumApplication extends Application {
|
|||||||
app.history.home();
|
app.history.home();
|
||||||
|
|
||||||
// Reload the current user so that their unread notification count is refreshed.
|
// Reload the current user so that their unread notification count is refreshed.
|
||||||
const userId = app.session.user?.id()
|
const userId = app.session.user?.id();
|
||||||
if (userId) {
|
if (userId) {
|
||||||
app.store.find('users', userId);
|
app.store.find('users', userId);
|
||||||
m.redraw();
|
m.redraw();
|
||||||
|
@@ -1,34 +1,31 @@
|
|||||||
import app from '../../forum/app';
|
import app from '../../forum/app';
|
||||||
import Modal from '../../common/components/Modal';
|
import Modal, { IInternalModalAttrs } from '../../common/components/Modal';
|
||||||
import Button from '../../common/components/Button';
|
import Button from '../../common/components/Button';
|
||||||
import extractText from '../../common/utils/extractText';
|
import extractText from '../../common/utils/extractText';
|
||||||
import Stream from '../../common/utils/Stream';
|
import Stream from '../../common/utils/Stream';
|
||||||
|
import Mithril from 'mithril';
|
||||||
|
import RequestError from '../../common/utils/RequestError';
|
||||||
|
|
||||||
|
export interface IForgotPasswordModalAttrs extends IInternalModalAttrs {
|
||||||
|
email?: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The `ForgotPasswordModal` component displays a modal which allows the user to
|
* The `ForgotPasswordModal` component displays a modal which allows the user to
|
||||||
* enter their email address and request a link to reset their password.
|
* enter their email address and request a link to reset their password.
|
||||||
*
|
|
||||||
* ### Attrs
|
|
||||||
*
|
|
||||||
* - `email`
|
|
||||||
*/
|
*/
|
||||||
export default class ForgotPasswordModal extends Modal {
|
export default class ForgotPasswordModal<CustomAttrs extends IForgotPasswordModalAttrs = IForgotPasswordModalAttrs> extends Modal<CustomAttrs> {
|
||||||
oninit(vnode) {
|
/**
|
||||||
|
* The value of the email input.
|
||||||
|
*/
|
||||||
|
email!: Stream<string>;
|
||||||
|
|
||||||
|
success: boolean = false;
|
||||||
|
|
||||||
|
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
|
||||||
super.oninit(vnode);
|
super.oninit(vnode);
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the email input.
|
|
||||||
*
|
|
||||||
* @type {Function}
|
|
||||||
*/
|
|
||||||
this.email = Stream(this.attrs.email || '');
|
this.email = Stream(this.attrs.email || '');
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether or not the password reset email was sent successfully.
|
|
||||||
*
|
|
||||||
* @type {Boolean}
|
|
||||||
*/
|
|
||||||
this.success = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
className() {
|
className() {
|
||||||
@@ -84,7 +81,7 @@ export default class ForgotPasswordModal extends Modal {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
onsubmit(e) {
|
onsubmit(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
@@ -98,14 +95,14 @@ export default class ForgotPasswordModal extends Modal {
|
|||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.success = true;
|
this.success = true;
|
||||||
this.alert = null;
|
this.alertAttrs = null;
|
||||||
})
|
})
|
||||||
.catch(() => {})
|
.catch(() => {})
|
||||||
.then(this.loaded.bind(this));
|
.then(this.loaded.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
onerror(error) {
|
onerror(error: RequestError) {
|
||||||
if (error.status === 404) {
|
if (error.status === 404 && error.alert) {
|
||||||
error.alert.content = app.translator.trans('core.forum.forgot_password.not_found_message');
|
error.alert.content = app.translator.trans('core.forum.forgot_password.not_found_message');
|
||||||
}
|
}
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
import app from '../../forum/app';
|
import app from '../../forum/app';
|
||||||
import Modal from '../../common/components/Modal';
|
import Modal, { IInternalModalAttrs } from '../../common/components/Modal';
|
||||||
import ForgotPasswordModal from './ForgotPasswordModal';
|
import ForgotPasswordModal from './ForgotPasswordModal';
|
||||||
import SignUpModal from './SignUpModal';
|
import SignUpModal from './SignUpModal';
|
||||||
import Button from '../../common/components/Button';
|
import Button from '../../common/components/Button';
|
||||||
@@ -7,38 +7,34 @@ import LogInButtons from './LogInButtons';
|
|||||||
import extractText from '../../common/utils/extractText';
|
import extractText from '../../common/utils/extractText';
|
||||||
import ItemList from '../../common/utils/ItemList';
|
import ItemList from '../../common/utils/ItemList';
|
||||||
import Stream from '../../common/utils/Stream';
|
import Stream from '../../common/utils/Stream';
|
||||||
|
import type Mithril from 'mithril';
|
||||||
|
import RequestError from '../../common/utils/RequestError';
|
||||||
|
|
||||||
/**
|
export interface ILoginModalAttrs extends IInternalModalAttrs {
|
||||||
* The `LogInModal` component displays a modal dialog with a login form.
|
identification?: string;
|
||||||
*
|
password?: string;
|
||||||
* ### Attrs
|
remember?: boolean;
|
||||||
*
|
}
|
||||||
* - `identification`
|
|
||||||
* - `password`
|
export default class LogInModal<CustomAttrs extends ILoginModalAttrs = ILoginModalAttrs> extends Modal<CustomAttrs> {
|
||||||
*/
|
/**
|
||||||
export default class LogInModal extends Modal {
|
* The value of the identification input.
|
||||||
oninit(vnode) {
|
*/
|
||||||
|
identification!: Stream<string>;
|
||||||
|
/**
|
||||||
|
* The value of the password input.
|
||||||
|
*/
|
||||||
|
password!: Stream<string>;
|
||||||
|
/**
|
||||||
|
* The value of the remember me input.
|
||||||
|
*/
|
||||||
|
remember!: Stream<boolean>;
|
||||||
|
|
||||||
|
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
|
||||||
super.oninit(vnode);
|
super.oninit(vnode);
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the identification input.
|
|
||||||
*
|
|
||||||
* @type {Function}
|
|
||||||
*/
|
|
||||||
this.identification = Stream(this.attrs.identification || '');
|
this.identification = Stream(this.attrs.identification || '');
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the password input.
|
|
||||||
*
|
|
||||||
* @type {Function}
|
|
||||||
*/
|
|
||||||
this.password = Stream(this.attrs.password || '');
|
this.password = Stream(this.attrs.password || '');
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the remember me input.
|
|
||||||
*
|
|
||||||
* @type {Function}
|
|
||||||
*/
|
|
||||||
this.remember = Stream(!!this.attrs.remember);
|
this.remember = Stream(!!this.attrs.remember);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,12 +136,10 @@ export default class LogInModal extends Modal {
|
|||||||
/**
|
/**
|
||||||
* Open the forgot password modal, prefilling it with an email if the user has
|
* Open the forgot password modal, prefilling it with an email if the user has
|
||||||
* entered one.
|
* entered one.
|
||||||
*
|
|
||||||
* @public
|
|
||||||
*/
|
*/
|
||||||
forgotPassword() {
|
forgotPassword() {
|
||||||
const email = this.identification();
|
const email = this.identification();
|
||||||
const attrs = email.indexOf('@') !== -1 ? { email } : undefined;
|
const attrs = email.includes('@') ? { email } : undefined;
|
||||||
|
|
||||||
app.modal.show(ForgotPasswordModal, attrs);
|
app.modal.show(ForgotPasswordModal, attrs);
|
||||||
}
|
}
|
||||||
@@ -153,13 +147,14 @@ export default class LogInModal extends Modal {
|
|||||||
/**
|
/**
|
||||||
* Open the sign up modal, prefilling it with an email/username/password if
|
* Open the sign up modal, prefilling it with an email/username/password if
|
||||||
* the user has entered one.
|
* the user has entered one.
|
||||||
*
|
|
||||||
* @public
|
|
||||||
*/
|
*/
|
||||||
signUp() {
|
signUp() {
|
||||||
const attrs = { password: this.password() };
|
|
||||||
const identification = this.identification();
|
const identification = this.identification();
|
||||||
attrs[identification.indexOf('@') !== -1 ? 'email' : 'username'] = identification;
|
|
||||||
|
const attrs = {
|
||||||
|
[identification.includes('@') ? 'email' : 'username']: identification,
|
||||||
|
password: this.password(),
|
||||||
|
};
|
||||||
|
|
||||||
app.modal.show(SignUpModal, attrs);
|
app.modal.show(SignUpModal, attrs);
|
||||||
}
|
}
|
||||||
@@ -168,7 +163,7 @@ export default class LogInModal extends Modal {
|
|||||||
this.$('[name=' + (this.identification() ? 'password' : 'identification') + ']').select();
|
this.$('[name=' + (this.identification() ? 'password' : 'identification') + ']').select();
|
||||||
}
|
}
|
||||||
|
|
||||||
onsubmit(e) {
|
onsubmit(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
@@ -182,8 +177,8 @@ export default class LogInModal extends Modal {
|
|||||||
.then(() => window.location.reload(), this.loaded.bind(this));
|
.then(() => window.location.reload(), this.loaded.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
onerror(error) {
|
onerror(error: RequestError) {
|
||||||
if (error.status === 401) {
|
if (error.status === 401 && error.alert) {
|
||||||
error.alert.content = app.translator.trans('core.forum.log_in.invalid_login_message');
|
error.alert.content = app.translator.trans('core.forum.log_in.invalid_login_message');
|
||||||
}
|
}
|
||||||
|
|
@@ -1,45 +1,47 @@
|
|||||||
import app from '../../forum/app';
|
import app from '../../forum/app';
|
||||||
import Modal from '../../common/components/Modal';
|
import Modal, { IInternalModalAttrs } from '../../common/components/Modal';
|
||||||
import LogInModal from './LogInModal';
|
import LogInModal from './LogInModal';
|
||||||
import Button from '../../common/components/Button';
|
import Button from '../../common/components/Button';
|
||||||
import LogInButtons from './LogInButtons';
|
import LogInButtons from './LogInButtons';
|
||||||
import extractText from '../../common/utils/extractText';
|
import extractText from '../../common/utils/extractText';
|
||||||
import ItemList from '../../common/utils/ItemList';
|
import ItemList from '../../common/utils/ItemList';
|
||||||
import Stream from '../../common/utils/Stream';
|
import Stream from '../../common/utils/Stream';
|
||||||
|
import type Mithril from 'mithril';
|
||||||
|
|
||||||
/**
|
export interface ISignupModalAttrs extends IInternalModalAttrs {
|
||||||
* The `SignUpModal` component displays a modal dialog with a singup form.
|
username?: string;
|
||||||
*
|
email?: string;
|
||||||
* ### Attrs
|
password?: string;
|
||||||
*
|
token?: string;
|
||||||
* - `username`
|
provided?: string[];
|
||||||
* - `email`
|
}
|
||||||
* - `password`
|
|
||||||
* - `token` An email token to sign up with.
|
export type SignupBody = {
|
||||||
*/
|
username: string;
|
||||||
export default class SignUpModal extends Modal {
|
email: string;
|
||||||
oninit(vnode) {
|
} & ({ token: string } | { password: string });
|
||||||
|
|
||||||
|
export default class SignUpModal<CustomAttrs extends ISignupModalAttrs = ISignupModalAttrs> 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>;
|
||||||
|
|
||||||
|
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
|
||||||
super.oninit(vnode);
|
super.oninit(vnode);
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the username input.
|
|
||||||
*
|
|
||||||
* @type {Function}
|
|
||||||
*/
|
|
||||||
this.username = Stream(this.attrs.username || '');
|
this.username = Stream(this.attrs.username || '');
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the email input.
|
|
||||||
*
|
|
||||||
* @type {Function}
|
|
||||||
*/
|
|
||||||
this.email = Stream(this.attrs.email || '');
|
this.email = Stream(this.attrs.email || '');
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the password input.
|
|
||||||
*
|
|
||||||
* @type {Function}
|
|
||||||
*/
|
|
||||||
this.password = Stream(this.attrs.password || '');
|
this.password = Stream(this.attrs.password || '');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,12 +57,12 @@ export default class SignUpModal extends Modal {
|
|||||||
return [<div className="Modal-body">{this.body()}</div>, <div className="Modal-footer">{this.footer()}</div>];
|
return [<div className="Modal-body">{this.body()}</div>, <div className="Modal-footer">{this.footer()}</div>];
|
||||||
}
|
}
|
||||||
|
|
||||||
isProvided(field) {
|
isProvided(field: string): boolean {
|
||||||
return this.attrs.provided && this.attrs.provided.indexOf(field) !== -1;
|
return this.attrs.provided?.includes(field) ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
body() {
|
body() {
|
||||||
return [this.attrs.token ? '' : <LogInButtons />, <div className="Form Form--centered">{this.fields().toArray()}</div>];
|
return [!this.attrs.token && <LogInButtons />, <div className="Form Form--centered">{this.fields().toArray()}</div>];
|
||||||
}
|
}
|
||||||
|
|
||||||
fields() {
|
fields() {
|
||||||
@@ -156,7 +158,7 @@ export default class SignUpModal extends Modal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onsubmit(e) {
|
onsubmit(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
@@ -175,22 +177,16 @@ export default class SignUpModal extends Modal {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the data that should be submitted in the sign-up request.
|
* Get the data that should be submitted in the sign-up request.
|
||||||
*
|
|
||||||
* @return {Object}
|
|
||||||
* @protected
|
|
||||||
*/
|
*/
|
||||||
submitData() {
|
submitData(): SignupBody {
|
||||||
|
const authData = this.attrs.token ? { token: this.attrs.token } : { password: this.password() };
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
username: this.username(),
|
username: this.username(),
|
||||||
email: this.email(),
|
email: this.email(),
|
||||||
|
...authData,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.attrs.token) {
|
|
||||||
data.token = this.attrs.token;
|
|
||||||
} else {
|
|
||||||
data.password = this.password();
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user