Skip to content

Commit

Permalink
Convert some common classes/utils to TS (#2929)
Browse files Browse the repository at this point in the history
* Convert common/Session

* Update common/states/AlertManagerState

* Convert common/utils/extractText

Co-authored-by: David Wheatley <hi@davwheat.dev>
  • Loading branch information
dsevillamartin and davwheat authored Nov 23, 2021
1 parent 1832fb0 commit aa0b68b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 63 deletions.
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.
*/
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 {
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);
} else {
return vdom;
return String(vdom);
}
}

0 comments on commit aa0b68b

Please sign in to comment.