Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some common classes/utils to TS #2929

Merged
merged 8 commits into from
Nov 23, 2021
56 changes: 0 additions & 56 deletions js/src/common/Session.js

This file was deleted.

55 changes: 55 additions & 0 deletions js/src/common/Session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import app from '../common/app';
import User from './models/User';
import { FlarumRequestOptions } from './Application';

export type LoginParams = {
/**
* The username/email
*/
identification: string;

/**
* Password
*/
password: string;
};

/**
* The `Session` class defines the current user session. It stores a reference
* to the current authenticated user, and provides methods to log in/out.
*/
export default class Session {
/**
* The current authenticated user.
*/
user: User | null;

/**
* The CSRF token.
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
*/
csrfToken: string;

constructor(user: User | null, csrfToken: string) {
this.user = user;
this.csrfToken = csrfToken;
}

/**
* Attempt to log in a user.
*/
login(body: LoginParams, options: Omit<FlarumRequestOptions<any>, 'url' | 'body' | 'method'> = {}) {
return app.request({
method: 'POST',
url: `${app.forum.attribute('baseUrl')}/login`,
body,
...options,
});
}

/**
* Log the user out.
*/
logout() {
window.location.href = `${app.forum.attribute('baseUrl')}/logout?token=${this.csrfToken}`;
}
}
2 changes: 1 addition & 1 deletion js/src/common/states/AlertManagerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class AlertManagerState {
/**
* Dismiss an alert.
*/
dismiss(key: AlertIdentifier): void {
dismiss(key: AlertIdentifier | null): void {
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
if (!key || !(key in this.activeAlerts)) return;

delete this.activeAlerts[key];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type Mithril from 'mithril';

/**
* Extract the text nodes from a virtual element.
*
* @param {VirtualElement} vdom
* @return {String}
*/
export default function extractText(vdom) {
export default function extractText(vdom: Mithril.Children): string {
if (vdom instanceof Array) {
return vdom.map((element) => extractText(element)).join('');
} else if (typeof vdom === 'object' && vdom !== null) {
return vdom.children ? extractText(vdom.children) : vdom.text;
return vdom.children ? extractText(vdom.children) : String(vdom.text);
dsevillamartin marked this conversation as resolved.
Show resolved Hide resolved
} else {
return vdom;
return String(vdom);
dsevillamartin marked this conversation as resolved.
Show resolved Hide resolved
}
}