misuzu/assets/typescript/User.ts

42 lines
1 KiB
TypeScript
Raw Normal View History

2019-04-30 00:55:10 +00:00
/// <reference path="Permissions.ts" />
interface CurrentUserInfo {
user_id: number;
username: string;
user_background_settings: number;
user_colour: number;
2019-04-30 00:55:10 +00:00
perms: Permissions;
}
let userInfo: CurrentUserInfo;
2019-06-10 17:04:53 +00:00
function getRawCurrentUserInfo(): CurrentUserInfo {
const userInfoElement: HTMLDivElement = document.getElementById('js-user-info') as HTMLDivElement;
2019-06-10 17:04:53 +00:00
if(!userInfoElement)
return null;
return JSON.parse(userInfoElement.textContent) as CurrentUserInfo;
}
2019-06-10 17:04:53 +00:00
function refreshCurrentUserInfo(): void {
userInfo = getRawCurrentUserInfo();
}
2019-06-10 17:04:53 +00:00
function getCurrentUser(attribute: string = null) {
if(attribute) {
if(!userInfo) {
return '';
}
return userInfo[attribute] || '';
}
return userInfo || null;
}
2019-06-10 17:04:53 +00:00
function userInit(): void {
refreshCurrentUserInfo();
console.log(`You are ${getCurrentUser('username')} with user id ${getCurrentUser('user_id')} and colour ${colourGetCSS(getCurrentUser('user_colour'))}.`);
}