Skip to content

Commit

Permalink
split Initialise action into app and session components
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Oct 31, 2023
1 parent bf55199 commit 1ff6e26
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 66 deletions.
1 change: 1 addition & 0 deletions app/static/src/app/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AppHeader from "./components/header/AppHeader.vue";
import { BasicState } from "./store/basic/state";
import { initialiseRouter } from "./router";

console.log("initialising store from basic")
export const store = new Vuex.Store<BasicState>(storeOptions);

const app = createApp({ components: { WodinSession, AppHeader } });
Expand Down
64 changes: 29 additions & 35 deletions app/static/src/app/components/WodinSession.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<template>
<session-initialise-modal :open="!sessionInitialised"
<session-initialise-modal :open="appInitialised && !sessionInitialised"
@new-session="newSession"
@reload-session="reloadSession"></session-initialise-modal>
<router-view v-if="sessionInitialised"></router-view>
<router-view v-if="appInitialised"></router-view>
</template>

<script lang="ts">
import {
defineComponent, onMounted, ref
computed,
defineComponent, onMounted, ref, watch
} from "vue";
import { RouterView, useRoute, useRouter } from "vue-router";
import { RouterView } from "vue-router";
import { useStore } from "vuex";
import { AppStateMutation } from "../store/appState/mutations";
import SessionInitialiseModal from "./SessionInitialiseModal.vue";
import { AppStateAction } from "../store/appState/actions";
import { ErrorsMutation } from "../store/errors/mutations";
Expand All @@ -34,10 +34,13 @@ export default defineComponent({
setup(props) {
const store = useStore();
// TODO: These are undefined ,but they shouldn't be... Get router undefined error when load session from Sessions page
// (loadSessionId is set)
const route = useRoute();
const router = useRouter();
const path = new URL(window.location.href).pathname;
const isSessionsRoute = !!path.match(/\/sessions\/?$/);
// We don't need to show session initialise modal if showing the sessions page, or..
// TODO: if no previous sessions...
const sessionInitialised = ref(isSessionsRoute);
const appInitialised = computed(() => !!store.state.config);
const {
appName,
Expand All @@ -48,44 +51,33 @@ export default defineComponent({
defaultLanguage
} = props;
store.commit(AppStateMutation.SetApp, {
store.dispatch(AppStateAction.InitialiseApp, {
appName,
baseUrl,
appsPath,
enableI18n,
defaultLanguage
});
// We don't need to show session initialise modal if showing the sessions page, or..
// TODO: if no previous sessions...
// Can't use the router here to check route as it isn't set up
// const sessionInitialised = ref(route.fullPath.endsWith("sessions"));
const sessionInitialised = ref(false);
onMounted(() => {
if (props.shareNotFound) {
store.commit(`errors/${ErrorsMutation.AddError}`,
{ detail: `Share id not found: ${props.shareNotFound}` });
}
});
const initialise = (sessionId: string) => {
store.dispatch(AppStateAction.Initialise,
{
appName,
baseUrl,
sessionId,
appsPath,
enableI18n,
defaultLanguage
});
store.dispatch(AppStateAction.InitialiseSession, sessionId);
sessionInitialised.value = true;
};
// If we have a loadSessionId we can initialise the session right away with the requested id
if (loadSessionId) {
initialise(loadSessionId);
}
// const initialised = computed(() => !!(store.state.appName && store.state.baseUrl && store.state.appsPath));
onMounted(() => {
if (props.shareNotFound) {
store.commit(`errors/${ErrorsMutation.AddError}`,
{ detail: `Share id not found: ${props.shareNotFound}` });
watch(appInitialised, (newValue) => {
if (newValue) {
console.log("app initialised")
}
// If we have a loadSessionId we can initialise the session as soon as app config is loaded, with id
if (newValue && loadSessionId) {
initialise(loadSessionId);
}
});
Expand All @@ -99,8 +91,10 @@ export default defineComponent({
initialise("");
};
console.log("completed setup")
return {
sessionInitialised,
appInitialised,
newSession,
reloadSession
};
Expand Down
62 changes: 35 additions & 27 deletions app/static/src/app/store/appState/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { FitState } from "../fit/state";
import { SessionsAction } from "../sessions/actions";
import { localStorageManager } from "../../localStorageManager";
import { AppStateGetter } from "./getters";
import { InitialisePayload } from "../../types/payloadTypes";
import { SetAppPayload } from "../../types/payloadTypes";

export enum AppStateAction {
Initialise = "Initialise",
InitialiseApp = "InitialiseApp",
InitialiseSession = "InitialiseSession",
QueueStateUpload = "QueueStateUpload",
LoadUserPreferences = "LoadUserPreferences",
SaveUserPreferences = "SaveUserPreferences"
Expand All @@ -34,50 +35,57 @@ async function immediateUploadState(context: ActionContext<AppState, AppState>)
}

export const appStateActions: ActionTree<AppState, AppState> = {
async [AppStateAction.Initialise](context, payload: InitialisePayload) {
// TODO: just accept loadSessionId here, everything else should already be set in the state
const {
commit, state, dispatch, getters
} = context;
async [AppStateAction.InitialiseApp](context, payload: SetAppPayload) {
const { commit} = context;
const {
appName,
baseUrl,
loadSessionId,
appsPath,
enableI18n,
defaultLanguage
} = payload;
/*commit(AppStateMutation.SetApp, {
commit(AppStateMutation.SetApp, {
appName,
baseUrl,
appsPath,
enableI18n,
defaultLanguage
});*/
localStorageManager.addSessionId(appName, getters[AppStateGetter.baseUrlPath], state.sessionId);
});

const response = await api(context)
.freezeResponse()
.withSuccess(AppStateMutation.SetConfig)
.withError(`errors/${ErrorsMutation.AddError}` as ErrorsMutation, true)
.get<AppConfig>(`/config/${appName}`);
if (response?.data.endTime) {
commit(`run/${RunMutation.SetEndTime}`, response.data.endTime, { root: true });
}
},

if (response) {
if (loadSessionId) {
// Fetch and rehydrate session data
await dispatch(`sessions/${SessionsAction.Rehydrate}`, loadSessionId);
} else {
await dispatch(`model/${ModelAction.FetchOdinRunner}`, null, { root: true });
// If not loading a session, set code and end time from default in config
commit(`code/${CodeMutation.SetCurrentCode}`, state.config!.defaultCode, { root: true });
if (response.data.endTime) {
commit(`run/${RunMutation.SetEndTime}`, response.data.endTime, { root: true });
}
commit(AppStateMutation.SetConfigured);
if (state.code.currentCode.length) {
// Fetch and run model for default code
await dispatch(`model/${ModelAction.DefaultModel}`);
}
async [AppStateAction.InitialiseSession](context, loadSessionId: string) {
const {
commit, state, dispatch, getters
} = context;

// 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);

if (loadSessionId) {
// Fetch and rehydrate session data
await dispatch(`sessions/${SessionsAction.Rehydrate}`, loadSessionId);
} else {
await dispatch(`model/${ModelAction.FetchOdinRunner}`, null, { root: true });
// If not loading a session, set code and end time from default in config
commit(`code/${CodeMutation.SetCurrentCode}`, state.config!.defaultCode, { root: true });
commit(AppStateMutation.SetConfigured);
if (state.code.currentCode.length) {
// Fetch and run model for default code
await dispatch(`model/${ModelAction.DefaultModel}`);
}
}
},
Expand Down
4 changes: 0 additions & 4 deletions app/static/src/app/types/payloadTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export interface SetAppPayload {
defaultLanguage: Language
}

export interface InitialisePayload extends SetAppPayload{
loadSessionId: string
}

export interface SetParameterSetResultPayload {
name: string,
result: OdinRunResultOde
Expand Down
3 changes: 3 additions & 0 deletions app/static/src/app/wodin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const getComponent = () => {
}
};

console.log("initialising store from wodin")
export const store = getStore();

const app = createApp({ components: { WodinSession, AppHeader } });
Expand All @@ -59,4 +60,6 @@ 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 1ff6e26

Please sign in to comment.