Created
November 3, 2017 12:43
-
-
Save unlabeled/149b2c6a37a0b3fb4f9984b7934ba24d to your computer and use it in GitHub Desktop.
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
/* @flow */ | |
import { action, computed, observable } from "mobx" | |
import { setUserContext } from "utils/errors" | |
import { myJetStorage, routingStore } from "stores" | |
import Api from "utils/api" | |
import { Aircraft, User } from "types" | |
/* eslint-disable */ | |
declare var __MORDA__: boolean | |
const morda = Boolean(__MORDA__) | |
/* eslint-enable */ | |
class HeadStore { | |
@observable loggedIn = void 0 | |
@observable _user: ?{ id: string, myjet_model: string } | |
@observable _aircraft: ?{ id: string, myjet_model: string } | |
constructor() { | |
Api.getItem("me") | |
.then(result => { | |
if (result && result.id) { | |
this.setUser(result) | |
} else { | |
throw "Fake" | |
} | |
}) | |
.catch(() => { | |
// TODO: Fix routing | |
this.clearUser() | |
if (window.location.pathname === "/login") { | |
return void 0 | |
} | |
const location = encodeURIComponent(window.location.pathname) | |
routingStore.history.push(`/login?next=${location}`) | |
window.location.reload() | |
}) | |
} | |
@action | |
clearUser = () => { | |
setUserContext() | |
this.loggedIn = false | |
this._user = void 0 | |
} | |
@action | |
setUser = (user: User) => { | |
myJetStorage.normalizeResult(user) | |
this._user = { id: user.id, myjet_model: "User" } | |
// Set user for Sentry | |
const userEntity = myJetStorage.getEntity(this._user) | |
setUserContext({ | |
id: userEntity.id, | |
email: userEntity.email, | |
username: userEntity.displayName, | |
}) | |
this.loggedIn = true | |
} | |
@computed | |
get locale(): ?string { | |
if (morda && this.aircraft) { | |
return this.aircraft.locale || "en-GB" | |
} | |
if (this.user) { | |
return this.user.locale || "en-GB" | |
} | |
return "en-GB" | |
} | |
@computed | |
get user(): ?User { | |
if (this.loggedIn === false) { | |
return void 0 | |
} | |
if (this._user) { | |
return myJetStorage.getEntityFromStorage(this._user) | |
} | |
} | |
@computed | |
get aircraftId(): ?string { | |
return this.aircraft ? this.aircraft.id : undefined | |
} | |
@computed | |
get referenceMonth(): ?string { | |
return (this.aircraft && this.aircraft.preferences.reference_month) || "all" | |
} | |
@computed | |
get selectedMonth(): ?string { | |
return (this.aircraft && this.aircraft.preferences.selected_month) || "all" | |
} | |
@computed | |
get selectedPeriod(): ?string { | |
return (this.aircraft && this.aircraft.preferences.selected_period) || "all" | |
} | |
@computed | |
get forPeriod(): ?string { | |
return (this.aircraft && this.aircraft.preferences.for_period) || "all" | |
} | |
@computed | |
get aircraft(): ?Aircraft { | |
if (!morda) { | |
return void 0 | |
} | |
if (this._aircraft) { | |
return myJetStorage.getEntity(this._aircraft) | |
} | |
if (this.user) { | |
const preferences = myJetStorage.getEntity(this.user.preferences) | |
if (preferences && preferences.aircraft) { | |
// TODO .getEntity(pref.aircraft) | |
return myJetStorage.getEntity({ | |
myjet_model: "Aircraft", | |
id: preferences.aircraft, | |
}) | |
} else { | |
myJetStorage.getList("Aircraft", {}, true).then(() => { | |
const aircraftList = myJetStorage.getList("Aircraft") | |
if (aircraftList && aircraftList.objects.length) { | |
this._aircraft = aircraftList.objects[0] | |
if (!this._user || !this._aircraft) { | |
return null | |
} | |
myJetStorage.updateEntity( | |
{ myjet_model: "UserPreferences", id: this._user.id }, | |
{ ["aircraft"]: this._aircraft.id }, | |
true | |
) | |
} | |
}) | |
} | |
} | |
} | |
} | |
const store = new HeadStore() | |
export default store |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment