import { Component, linkEvent, createRef, RefObject } from 'inferno';
import { Link } from 'inferno-router';
import { Subscription } from 'rxjs';
-import { retryWhen, delay, take } from 'rxjs/operators';
import { WebSocketService, UserService } from '../services';
import {
UserOperation,
notifyComment,
notifyPrivateMessage,
isBrowser,
+ wsSubscribe,
} from '../utils';
import { i18n } from '../i18next';
searchParam: '',
toggleSearch: false,
};
+ subscription: any;
constructor(props: any, context: any) {
super(props, context);
this.state = this.emptyState;
- if (isBrowser()) {
- this.wsSub = WebSocketService.Instance.subject
- .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
- .subscribe(
- msg => this.parseMessage(msg),
- err => console.error(err),
- () => console.log('complete')
- );
-
- this.searchTextField = createRef();
- }
+ this.parseMessage = this.parseMessage.bind(this);
+ this.subscription = wsSubscribe(this.parseMessage);
// The login
// TODO this needs some work
- if (this.props.site.my_user) {
- UserService.Instance.user = this.props.site.my_user;
- // i18n.changeLanguage(getLanguage());
-
- if (isBrowser()) {
- WebSocketService.Instance.userJoin();
- // On the first load, check the unreads
- if (this.state.isLoggedIn == false) {
- this.requestNotificationPermission();
- this.fetchUnreads();
- // setTheme(data.my_user.theme, true);
- // i18n.changeLanguage(getLanguage());
- // i18n.changeLanguage('de');
- }
- }
+ UserService.Instance.user = this.props.site.my_user;
+ i18n.changeLanguage(getLanguage());
+ setTheme();
+ if (UserService.Instance.user) {
}
+
+ // if (!!this.props.site.my_user) {
+ // UserService.Instance.this.props.site.my_user);
+ // // UserService.Instance.user = this.props.site.my_user;
+ // } else {
+ // UserService.Instance.setUser(undefined);
+ // }
}
componentDidMount() {
// Subscribe to jwt changes
if (isBrowser()) {
+ this.searchTextField = createRef();
+ console.log(`isLoggedIn = ${this.state.isLoggedIn}`);
+
+ // On the first load, check the unreads
+ if (this.state.isLoggedIn == false) {
+ // setTheme(data.my_user.theme, true);
+ // i18n.changeLanguage(getLanguage());
+ // i18n.changeLanguage('de');
+ } else {
+ this.requestNotificationPermission();
+ WebSocketService.Instance.userJoin();
+ this.fetchUnreads();
+ }
+
this.userSub = UserService.Instance.jwtSub.subscribe(res => {
// A login
if (res !== undefined) {
this.requestNotificationPermission();
+ WebSocketService.Instance.getSite();
} else {
- this.state.isLoggedIn = false;
+ this.setState({ isLoggedIn: false });
}
- console.log('a new login');
- // WebSocketService.Instance.getSite();
- this.setState(this.state);
});
// Subscribe to unread count changes
this.state.unreadCount = this.calculateUnreadCount();
this.setState(this.state);
this.sendUnreadCount();
+ } else if (res.op == UserOperation.GetSite) {
+ // This is only called on a successful login
+ let data = res.data as GetSiteResponse;
+ UserService.Instance.user = data.my_user;
+ this.state.isLoggedIn = true;
+ this.setState(this.state);
} else if (res.op == UserOperation.CreateComment) {
let data = res.data as CommentResponse;
);
private constructor() {
- let jwt = IsomorphicCookie.load('jwt');
- if (jwt) {
- this.setClaims(jwt);
+ if (this.auth) {
+ this.setClaims(this.auth);
} else {
// setTheme();
console.log('No JWT cookie found.');
}
public login(res: LoginResponse) {
- this.setClaims(res.jwt);
let expires = new Date();
expires.setDate(expires.getDate() + 365);
- IsomorphicCookie.save('jwt', res.jwt, { expires });
+ IsomorphicCookie.save('jwt', res.jwt, { expires, secure: false });
console.log('jwt cookie set');
+ this.setClaims(res.jwt);
}
public logout() {
+ IsomorphicCookie.remove('jwt', { secure: false });
this.claims = undefined;
this.user = undefined;
- IsomorphicCookie.remove('jwt');
// setTheme();
this.jwtSub.next();
console.log('Logged out.');
// TODO
export function getLanguage(override?: string): string {
- return 'en';
- // let user = UserService.Instance.user;
- // let lang = override || (user && user.lang ? user.lang : 'browser');
+ let user = UserService.Instance.user;
+ let lang = override || (user && user.lang ? user.lang : 'browser');
- // if (lang == 'browser') {
- // return getBrowserLanguage();
- // } else {
- // return lang;
- // }
+ if (lang == 'browser' && isBrowser()) {
+ return getBrowserLanguage();
+ } else {
+ return lang;
+ }
}
// TODO
// TODO
export function setTheme(theme: string = 'darkly', loggedIn: boolean = false) {
+ // require('../assets/css/themes/_variables.darkly.scss');
+ // require('../../node_modules/bootstrap/scss/bootstrap.scss');
+ // require('../assets/css/themes/_variables.darkly.scss');
+ // require('../assets/css/themes/_variables.darkly.scss');
+ // require('../assets/css/themes/_variables.darkly.scss');
// unload all the other themes
// for (var i = 0; i < themes.length; i++) {
// let styleSheet = document.getElementById(themes[i]);
}
export function setIsoData(context: any): IsoData {
- if (isBrowser()) {
- return window.isoData;
- } else {
- return context.router.staticContext;
- }
+ let isoData: IsoData = isBrowser()
+ ? window.isoData
+ : context.router.staticContext;
+ return isoData;
}
export function wsSubscribe(parseMessage: any): Subscription {