import * as React from "react"; import { BrowserRouter, Route, Redirect } from "react-router-dom"; import AuthRoute from "../security/AuthRoute"; import Dashboard from "../pages/dashboard"; import LoginPage from "../pages/login"; import LevelListPage from "../pages/levelList"; import LevelPage from "../pages/level"; import ReviewPage from "../pages/review"; import SummaryPage from "../pages/summary"; import WelcomePage from "../pages/intro"; import Drawer from "../containers/Drawer"; import { BACKEND_URL } from "../config"; import { ILevel } from "../models/level"; import { ILearner } from "../models/learner"; import { IVocab, VocabType } from "../models/vocab"; import { IReviewMetadata, ReviewType } from "../models/review"; import { IUser } from "../models/user"; // TODO: Replace the sessionStorage with localStorage? // TODO: Cache API-Calls // TODO: When mounting without a login, check if the sessionToken is still valid export default class Application extends React.Component<{}> { constructor(props: any) { super(props); // Load a key from the SessionStorage const authKey = window.sessionStorage.getItem("sessionToken") || null; // TODO const loggedIn = authKey !== null; this.login = this.login.bind(this); this.isAuthenticated = this.isAuthenticated.bind(this); } getLevels(): ILevel[] { console.log("STUB: Application::getLevels"); // TODO: Actually fetch them from somewhere const levels = [{ name: "Der Bauer auf dem Feld", desc: "So fängt alles an: Du bist ein einfacher Bauer und musst dich die Karriereleiter mit deinen freshen Latein-Skills hinaufarbeiten", level: 1, done: true, }, { name: "???", desc: "Warum schreibe ich überhaupt was?dsd dddddddddddddddddddddd", level: 2, done: false, }]; return levels; } getLastReview = (): IReviewMetadata => { console.log("STUB: Application::getLastReview"); // TODO: Actually fetch this // TODO: Stub return {} as IReviewMetadata; } setLastReview = (meta: IReviewMetadata) => { console.log("STUB: Application::setLastReview"); // TODO: Send this to the server this.setState({ lastReview: meta, }); } getReviewQueue = (): IVocab[] => { console.log("STUB: Application::getReviewQueue"); // TODO: Implement return [{ german: ["Wein"], hint: "Worte auf '-um' sind meistens NeutrUM", type: VocabType.NOMEN, latin: { grundform: "Vinum", genitiv: "Vini", genus: "Neutrum" }, id: 0 }/* , { * latin: "Vici", * german: "", * hint: "Wird \"Viki\" und nicht \"Vichi\" ausgesprochen", * mnemonic: "Merk dir das Wort mit Caesars berühmten Worten: \"Veni Vidi Vici\"; Er kam, sah und siegte", * type: VocabType.NOMEN, * id: 2 }, { * latin: "fuga", * german: "Flucht", * hint: "Worte auf \"-a\" sind FeminA", * type: VocabType.NOMEN, * id: 3 } */] as IVocab[]; } getLearners(): ILearner[] { console.log("STUB: Application::getLearners"); // TODO: Implement return [{ username: "Polynomdivision", level: 5, score: 400, }, { username: "Polynomdivision2", level: 3, score: 500, }, { username: "Der eine Typ", level: 7, score: 100, }]; } getTopTenLearners(): ILearner[] { console.log("STUB: Application::getTopTenLearners"); // TODO: Implement return [{ username: "Polynomdivision", level: 5, score: 400, }, { username: "Polynomdivision2", level: 3, score: 500, }, { username: "Der eine Typ", level: 7, score: 100, }]; } getNextLevel(): ILevel { console.log("STUB: Application::getNextLevel"); // TODO: Actually fetch data return { name: "???", desc: "Warum schreibe ich überhaupt was?dsd dddddddddddddddddddddd", level: 2, done: false, }; } getLevelVocab(id: number): IVocab[] { console.log("STUB: Application::getLevelVocab"); // TODO: Actually implement this // TODO: Don't fetch this when it was already fetched once. return [{ german: ["Wein"], hint: "Worte auf '-um' sind meistens NeutrUM", type: VocabType.NOMEN, latin: { grundform: "Vinum", genitiv: "Vini", genus: "Neutrum" }, id: 0 }/* , { * latin: "Vici", * german: "", * hint: "Wird \"Viki\" und nicht \"Vichi\" ausgesprochen", * mnemonic: "Merk dir das Wort mit Caesars berühmten Worten: \"Veni Vidi Vici\"; Er kam, sah und siegte", * type: VocabType.NOMEN, * id: 2 }, { * latin: "fuga", * german: "Flucht", * hint: "Worte auf \"-a\" sind FeminA", * type: VocabType.NOMEN, * id: 3 } */] as IVocab[]; } login(username: string, password: string): Promise { return new Promise((res, rej) => { // TODO: First login? Redirect to /welcome fetch(`${BACKEND_URL}/login`, { method: "POST", headers: new Headers({ 'Content-Type': "application/json", }), body: JSON.stringify({ // NOTE: We will force HTTPS and hash using pbkdf2 on the // server username: username, hash: password, }), }).then(data => data.json()) .then((resp) => { if (resp.error === "0") { this.setState({ loggedIn: true, user: resp.data, }); res(resp.data); } else { rej({}); } }); }); } // Checks whether the user is logged in isAuthenticated() { // TODO: Security? // TODO: Implement return true; } closeDrawer = () => { this.setState({ drawerOpen: false, }); } drawerButtonState = (show: boolean) => { // This is required as we would otherwise try to continously update // the state, resulting in an infinte loop if (show !== this.state.showDrawerButton) { this.setState({ showDrawerButton: show, }); } } render() { return
} /> { return }} /> { return }} /> { return }} /> } /> {/*We cannot use AuthRoute here, because match is undefined otherwise*/} { if (this.isAuthenticated()) { return ; } else { return ; } }} /> { if (this.isAuthenticated()) { return ; } else { return ; } }} /> { return ; }} /> { return }} />
; } };