-
-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert some common classes/utils to TS (#2929)
* Convert common/Session * Update common/states/AlertManagerState * Convert common/utils/extractText Co-authored-by: David Wheatley <hi@davwheat.dev>
- Loading branch information
1 parent
1832fb0
commit aa0b68b
Showing
4 changed files
with
61 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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}`; | ||
} | ||
} |
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
11 changes: 5 additions & 6 deletions
11
js/src/common/utils/extractText.js → js/src/common/utils/extractText.ts
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 |
---|---|---|
@@ -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); | ||
} | ||
} |