Skip to content

Commit

Permalink
refactor to move modal and associated logic to WodinApp
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Nov 8, 2023
1 parent 24c2aaa commit c921357
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
41 changes: 39 additions & 2 deletions app/static/src/app/components/WodinApp.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<template>
<session-initialise-modal v-if="!sessionInitialised && !immediateInitialise"
@new-session="newSession"
@reload-session="reloadSession"></session-initialise-modal>
<div class="container">
<div class="row">
<div class="col-12">
Expand All @@ -21,15 +24,20 @@
</template>

<script lang="ts">
import { computed, defineComponent } from "vue";
import {
computed, defineComponent
} from "vue";
import { useStore } from "vuex";
import SessionInitialiseModal from "./SessionInitialiseModal.vue";
import { AppStateAction } from "../store/appState/actions";
import ErrorsAlert from "./ErrorsAlert.vue";
import WodinPanels from "./WodinPanels.vue";
import LoadingSpinner from "./LoadingSpinner.vue";
export default defineComponent({
name: "WodinApp",
components: {
SessionInitialiseModal,
LoadingSpinner,
ErrorsAlert,
WodinPanels
Expand All @@ -40,9 +48,38 @@ export default defineComponent({
const appType = computed(() => store.state.appType);
const loading = computed(() => !store.state.config);
const sessionInitialised = computed(() => store.state.configured);
const latestSessionId = computed(() => store.state.sessions.latestSessionId);
const loadSessionId = computed(() => store.state.loadSessionId);
const initialise = (sessionId: string, copySession = true) => {
store.dispatch(AppStateAction.InitialiseSession, { loadSessionId: sessionId, copySession });
};
// If there's a load (share) session id, then immediately initialise with that
// If there is no latest session id then immediately initialise a new session - display modal conditional on
// this value as well as sessionInitialised to avoid momentary display of modal when this is true.
const immediateInitialise = computed(() => loadSessionId.value || !latestSessionId.value);
if (immediateInitialise.value) {
initialise(loadSessionId.value || "");
}
const newSession = () => {
initialise("");
};
const reloadSession = () => {
initialise(latestSessionId.value!, false);
};
return {
appType,
loading
loading,
sessionInitialised,
immediateInitialise,
newSession,
reloadSession
};
}
});
Expand Down
46 changes: 10 additions & 36 deletions app/static/src/app/components/WodinSession.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<template>
<session-initialise-modal v-if="appInitialised && !sessionInitialised && !isSessionsRoute"
@new-session="newSession"
@reload-session="reloadSession"></session-initialise-modal>
<router-view v-if="appInitialised"></router-view>
<router-view v-if="initialised"></router-view>
</template>

<script lang="ts">
Expand All @@ -12,7 +9,6 @@ import {
} from "vue";
import { RouterView } from "vue-router";
import { useStore } from "vuex";
import SessionInitialiseModal from "./SessionInitialiseModal.vue";
import { AppStateAction } from "../store/appState/actions";
import { ErrorsMutation } from "../store/errors/mutations";
import { localStorageManager } from "../localStorageManager";
Expand All @@ -32,17 +28,14 @@ export default defineComponent({
defaultLanguage: String
},
components: {
SessionInitialiseModal,
RouterView
},
setup(props) {
const store = useStore();
const path = new URL(window.location.href).pathname;
const isSessionsRoute = !!path.match(/\/sessions\/?$/);
const sessionInitialised = ref(false);
const initialised = ref(false);
const appInitialised = computed(() => !!store.state.config && !!store.state.sessions.sessionsMetadata);
const latestSessionId = computed(() => store.state.sessions.latestSessionId);
// These props won't change as provided by server
// eslint-disable-next-line vue/no-setup-props-destructure
Expand All @@ -60,7 +53,8 @@ export default defineComponent({
baseUrl,
appsPath,
enableI18n,
defaultLanguage
defaultLanguage,
loadSessionId
});
onMounted(() => {
Expand All @@ -70,14 +64,9 @@ export default defineComponent({
}
});
const initialise = (sessionId: string, copySession = true) => {
store.dispatch(AppStateAction.InitialiseSession, { loadSessionId: sessionId, copySession });
sessionInitialised.value = true;
};
watch(appInitialised, (newValue) => {
// We don't need to show session initialise modal if we have a loadSessionId (loading from share) or if
// there are no previous sessions - initialise as soon as config available
watch(appInitialised, () => {
// Child component will either be SessionsPage or WodinApp depending on route - both will need the latest
// session id so delay rendering these until this has been committed
const baseUrlPath = store.getters[AppStateGetter.baseUrlPath];
const sessions = localStorageManager.getSessionIds(store.state.appName, baseUrlPath);
const sessionId = sessions.length ? sessions[0] : null;
Expand All @@ -87,26 +76,11 @@ export default defineComponent({
if (sessionAvailable) {
store.commit(`sessions/${SessionsMutation.SetLatestSessionId}`, sessionId);
}
if (newValue && (loadSessionId || !latestSessionId.value)) {
initialise(loadSessionId || "");
}
initialised.value = true;
});
const newSession = () => {
initialise("");
};
const reloadSession = () => {
initialise(latestSessionId.value!, false);
};
return {
sessionInitialised,
appInitialised,
isSessionsRoute,
newSession,
reloadSession
initialised
};
}
});
Expand Down
14 changes: 2 additions & 12 deletions app/static/src/app/store/appState/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,9 @@ export const appStateActions: ActionTree<AppState, AppState> = {
async [AppStateAction.InitialiseApp](context, payload: InitialiseAppPayload) {
const { commit, dispatch } = context;
const {
appName,
baseUrl,
appsPath,
enableI18n,
defaultLanguage
appName
} = payload;
commit(AppStateMutation.SetApp, {
appName,
baseUrl,
appsPath,
enableI18n,
defaultLanguage
});
commit(AppStateMutation.SetApp, payload);

const response = await api(context)
.freezeResponse()
Expand Down
1 change: 1 addition & 0 deletions app/static/src/app/store/appState/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const appStateMutations: MutationTree<AppState> = {
state.appsPath = payload.appsPath;
state.language.currentLanguage = payload.defaultLanguage;
state.language.enableI18n = payload.enableI18n;
state.loadSessionId = payload.loadSessionId;
registerTranslations(state.language, collectedTranslations);
},

Expand Down
1 change: 1 addition & 0 deletions app/static/src/app/store/appState/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface AppState {
baseUrl: null | string,
appsPath: null | string,
appType: AppType
loadSessionId: null | string, // if sharing a session, this is the id of the session to share (copy)
openVisualisationTab: VisualisationTab
queuedStateUploadIntervalId: number
stateUploadInProgress: boolean
Expand Down
3 changes: 2 additions & 1 deletion app/static/src/app/types/payloadTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export interface InitialiseAppPayload {
baseUrl: string
appsPath: string,
enableI18n: boolean,
defaultLanguage: Language
defaultLanguage: Language,
loadSessionId: string | null
}

export interface InitialiseSessionPayload {
Expand Down

0 comments on commit c921357

Please sign in to comment.