2017-06-29 12:54:02 -06:00
|
|
|
import { AuthState } from "./auth/interfaces";
|
|
|
|
import { box } from "boxed_value";
|
2017-08-25 09:11:11 -06:00
|
|
|
import { get, isNumber } from "lodash";
|
|
|
|
|
|
|
|
export enum BooleanSetting {
|
|
|
|
X_AXIS_INVERTED = "x_axis_inverted",
|
|
|
|
Y_AXIS_INVERTED = "y_axis_inverted",
|
|
|
|
Z_AXIS_INVERTED = "z_axis_inverted",
|
|
|
|
RAW_ENCODERS = "raw_encoders",
|
|
|
|
SCALED_ENCODERS = "scaled_encoders"
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum NumericSetting {
|
|
|
|
BOT_ORIGIN_QUADRANT = "bot_origin_quadrant",
|
|
|
|
ZOOM_LEVEL = "zoom_level",
|
|
|
|
}
|
2017-06-29 12:54:02 -06:00
|
|
|
|
|
|
|
export namespace Session {
|
|
|
|
const KEY = "session";
|
|
|
|
|
|
|
|
/** Replace the contents of session storage. */
|
2017-08-25 09:11:11 -06:00
|
|
|
export function replace(nextState: AuthState) {
|
2017-06-29 12:54:02 -06:00
|
|
|
localStorage[KEY] = JSON.stringify(nextState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Fetch the previous session. */
|
2017-08-25 09:11:11 -06:00
|
|
|
export function getAll(): AuthState | undefined {
|
2017-06-29 12:54:02 -06:00
|
|
|
try {
|
|
|
|
let v: AuthState = JSON.parse(localStorage[KEY]);
|
|
|
|
if (box(v).kind === "object") {
|
|
|
|
return v;
|
|
|
|
} else {
|
|
|
|
throw new Error("Expected object or undefined");
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return undefined;
|
2017-08-02 09:14:08 -06:00
|
|
|
}
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear localstorage and sessionstorage. */
|
2017-08-25 09:11:11 -06:00
|
|
|
export function clear() {
|
2017-06-29 12:54:02 -06:00
|
|
|
localStorage.clear();
|
|
|
|
sessionStorage.clear();
|
|
|
|
window.location.href = window.location.origin;
|
|
|
|
}
|
2017-08-25 09:11:11 -06:00
|
|
|
|
|
|
|
export function getBool(key: BooleanSetting): boolean {
|
|
|
|
let output = JSON.parse(get(localStorage, key, "false"));
|
|
|
|
return !output;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setBool(key: BooleanSetting, val: boolean): void {
|
|
|
|
localStorage.setItem(key, JSON.stringify(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNum(key: NumericSetting): number | undefined {
|
|
|
|
let output = JSON.parse(get(localStorage, key, "null"));
|
|
|
|
return (isNumber(output)) ? output : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setNum(key: NumericSetting, val: number): void {
|
|
|
|
localStorage.setItem(key, JSON.stringify(val));
|
|
|
|
}
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|