Skip to content

Commit

Permalink
reload latest session label
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Nov 1, 2023
1 parent 6784a95 commit c9bbd28
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 26 deletions.
4 changes: 4 additions & 0 deletions app/server/src/db/sessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class SessionStore {
await this._redis.hset(this.sessionKey("label"), id, label);
}

async getSessionLabel(id: string) {
await this._redis.hget(this.sessionKey("label"), id);
}

async getSession(id: string) {
return this._redis.hget(this.sessionKey("data"), id);
}
Expand Down
27 changes: 18 additions & 9 deletions app/static/src/app/components/WodinSession.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<session-initialise-modal :open="appInitialised && !sessionInitialised"
<session-initialise-modal :open="appInitialised && !sessionInitialised && !isSessionsRoute"
@new-session="newSession"
@reload-session="reloadSession"></session-initialise-modal>
<router-view v-if="appInitialised"></router-view>
Expand All @@ -17,6 +17,7 @@ import { AppStateAction } from "../store/appState/actions";
import { ErrorsMutation } from "../store/errors/mutations";
import { localStorageManager } from "../localStorageManager";
import {AppStateGetter} from "../store/appState/getters";
import {SessionMetadata} from "../types/responseTypes";
export default defineComponent({
name: "WodinSession",
Expand All @@ -39,7 +40,7 @@ export default defineComponent({
const path = new URL(window.location.href).pathname;
const isSessionsRoute = !!path.match(/\/sessions\/?$/);
const sessionInitialised = ref(false);
const appInitialised = computed(() => !!store.state.config);
const appInitialised = computed(() => !!store.state.config && !!store.state.sessions.sessionsMetadata);
const latestSessionId: Ref<null|string> = ref(null);
const {
Expand All @@ -66,17 +67,24 @@ export default defineComponent({
}
});
const initialise = (sessionId: string) => {
store.dispatch(AppStateAction.InitialiseSession, sessionId);
const initialise = (sessionId: string, copySession = true) => {
console.log(`Initialising with sessionId: ${sessionId}, copySession: ${copySession}`)
store.dispatch(AppStateAction.InitialiseSession, { loadSessionId: sessionId, copySession });
sessionInitialised.value = true;
};
watch(appInitialised, (newValue) => {
// We don't need to show session initialise modal if showing the sessions page, or if we have a
// loadSessionId or if there are no previous sessions
// We don't need to show session initialise modal if we have a
// loadSessionId (loading from share) or if there are no previous sessions
const sessions = localStorageManager.getSessionIds(store.state.appName, store.getters[AppStateGetter.baseUrlPath]);
latestSessionId.value = sessions.length ? sessions[0] : null;
if (newValue && (loadSessionId || isSessionsRoute || !latestSessionId.value)) {
const sessionId = sessions.length ? sessions[0] : null;
// check latest session id is actually available from the back end
const sessionAvailable = sessionId && !!store.state.sessions.sessionsMetadata.find((s: SessionMetadata) => s.id === sessionId);
if (sessionAvailable) {
latestSessionId.value = sessionId;
}
if (newValue && (loadSessionId || !latestSessionId.value)) {
initialise(loadSessionId || "");
}
});
Expand All @@ -86,12 +94,13 @@ export default defineComponent({
};
const reloadSession = () => {
initialise(latestSessionId.value!);
initialise(latestSessionId.value!, false);
};
return {
sessionInitialised,
appInitialised,
isSessionsRoute,
newSession,
reloadSession
};
Expand Down
2 changes: 1 addition & 1 deletion app/static/src/app/components/sessions/SessionsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export default defineComponent({
};
onMounted(() => {
store.dispatch(`${namespace}/${SessionsAction.GetSessions}`);
store.dispatch(`${namespace}/${SessionsAction.GetSessions}`); // get latest sessions
store.dispatch(AppStateAction.LoadUserPreferences);
});
Expand Down
36 changes: 25 additions & 11 deletions app/static/src/app/store/appState/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FitState } from "../fit/state";
import { SessionsAction } from "../sessions/actions";
import { localStorageManager } from "../../localStorageManager";
import { AppStateGetter } from "./getters";
import { SetAppPayload } from "../../types/payloadTypes";
import {InitialiseAppPayload, InitialiseSessionPayload} from "../../types/payloadTypes";

export enum AppStateAction {
InitialiseApp = "InitialiseApp",
Expand All @@ -35,8 +35,8 @@ async function immediateUploadState(context: ActionContext<AppState, AppState>)
}

export const appStateActions: ActionTree<AppState, AppState> = {
async [AppStateAction.InitialiseApp](context, payload: SetAppPayload) {
const { commit} = context;
async [AppStateAction.InitialiseApp](context, payload: InitialiseAppPayload) {
const { commit, dispatch} = context;
const {
appName,
baseUrl,
Expand All @@ -60,20 +60,34 @@ export const appStateActions: ActionTree<AppState, AppState> = {
if (response?.data.endTime) {
commit(`run/${RunMutation.SetEndTime}`, response.data.endTime, { root: true });
}

// get sessions metadata so we can check latest session to reload
dispatch(`sessions/${SessionsAction.GetSessions}`);
},

async [AppStateAction.InitialiseSession](context, loadSessionId: string) {
async [AppStateAction.InitialiseSession](context, payload: InitialiseSessionPayload) {
const {
commit, state, dispatch, getters
} = context;
const {
loadSessionId,
copySession
} = payload;

// If we're reloading a session on page refresh we want to use the same session id rather than making a copy
// as we do on share - otherwise we save the new session id to local storage
if (loadSessionId && !copySession) {
commit(AppStateMutation.SetSessionId, loadSessionId);

// TODO: If user has selected to reload the previous session, presumably that meant *not* a copy
// - we need to set loadSessionId as the session in the state. But this has already been set - we should
// ensure it doesn't get saved as we don't really want to spawn another session... ALTHOUGH if we just don't
// add it to the local storage here, it will be ignored in future session list fetches
// So, todo - set state.sessionId to loadSessionId and *don't* add session id here (But make sure this doesn't
// impact load from share)
localStorageManager.addSessionId(state.appName!, getters[AppStateGetter.baseUrlPath], state.sessionId);
// set the session's label too, if any
const label = state.sessions
.sessionsMetadata?.find((s) => s.id === loadSessionId)?.label;
if (label) {
commit(AppStateMutation.SetSessionLabel, label);
}
} else {
localStorageManager.addSessionId(state.appName!, getters[AppStateGetter.baseUrlPath], state.sessionId);
}

if (loadSessionId) {
// Fetch and rehydrate session data
Expand Down
9 changes: 7 additions & 2 deletions app/static/src/app/store/appState/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MutationTree } from "vuex";
import { AppState, UserPreferences, VisualisationTab } from "./state";
import { AppConfig } from "../../types/responseTypes";
import { SetAppPayload } from "../../types/payloadTypes";
import { InitialiseAppPayload } from "../../types/payloadTypes";
import registerTranslations from "../../../../translationPackage/registerTranslations";
import { collectedTranslations } from "../translations/collectedTranslations";

Expand All @@ -12,6 +12,7 @@ export enum AppStateMutation {
ClearQueuedStateUpload = "ClearQueuedStateUpload",
SetQueuedStateUpload = "SetQueuedStateUpload",
SetStateUploadInProgress = "SetStateUploadInProgress",
SetSessionId = "SetSessionId",
SetSessionLabel = "SetSessionLabel",
SetConfigured = "SetConfigured",
SetUserPreferences = "SetUserPreferences",
Expand All @@ -26,7 +27,7 @@ export const StateUploadMutations = [
] as string[];

export const appStateMutations: MutationTree<AppState> = {
[AppStateMutation.SetApp](state: AppState, payload: SetAppPayload) {
[AppStateMutation.SetApp](state: AppState, payload: InitialiseAppPayload) {
state.appName = payload.appName;
state.baseUrl = payload.baseUrl;
state.appsPath = payload.appsPath;
Expand Down Expand Up @@ -56,6 +57,10 @@ export const appStateMutations: MutationTree<AppState> = {
state.stateUploadInProgress = payload;
},

[AppStateMutation.SetSessionId](state: AppState, payload: string) {
state.sessionId = payload;
},

[AppStateMutation.SetSessionLabel](state: AppState, payload: null | string) {
state.sessionLabel = payload;
},
Expand Down
2 changes: 2 additions & 0 deletions app/static/src/app/store/appState/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { VersionsState } from "../versions/state";
import { GraphSettingsState } from "../graphSettings/state";
import { LanguageState } from "../../../../translationPackage/store/state";
import { MultiSensitivityState } from "../multiSensitivity/state";
import {SessionsState} from "../sessions/state";

export enum AppType {
Basic = "basic",
Expand Down Expand Up @@ -42,6 +43,7 @@ export interface AppState {
sensitivity: SensitivityState,
multiSensitivity: MultiSensitivityState,
graphSettings: GraphSettingsState,
sessions: SessionsState,
versions: VersionsState,
language: LanguageState,
configured: boolean, // true if configuration has been loaded or rehydrated and defaults set
Expand Down
7 changes: 6 additions & 1 deletion app/static/src/app/types/payloadTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { Language } from "./languageTypes";
import { AdvancedOptions } from "./responseTypes";
import { Tag } from "../store/run/state";

export interface SetAppPayload {
export interface InitialiseAppPayload {
appName: string
baseUrl: string
appsPath: string,
enableI18n: boolean,
defaultLanguage: Language
}

export interface InitialiseSessionPayload {
loadSessionId: string,
copySession: boolean
}

export interface SetParameterSetResultPayload {
name: string,
result: OdinRunResultOde
Expand Down
2 changes: 0 additions & 2 deletions app/static/src/app/wodin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,4 @@ app.directive("translate", translate<AppState>(store));
app.mount("#app");

const router = initialiseRouter(getComponent(), store.state.appName!, store.state.baseUrl!, store.state.appsPath!);
console.log("initialising router")
app.use(router);
console.log("initialed store from wodin")

0 comments on commit c9bbd28

Please sign in to comment.