Fix session navigation ordering

- Order sessions by start timestamp
- Update derived states to use ordered sessions state

[src/routes/projects/[projectId]/sessions/[sessionId]/+page.ts]
- Add a derived state to order the sessions by start timestamp
- Update the `session` derived state to use the new ordered sessions state
- Update the `previousSession` and `nextSession` derived states to use the new ordered sessions state
This commit is contained in:
Kiril Videlov 2023-02-20 11:11:30 +01:00
parent a0b6e82795
commit 624796ba97

View File

@ -18,14 +18,18 @@ export const load: PageLoad = async ({ parent, params }) => {
projectId: params.projectId, projectId: params.projectId,
sessionId: params.sessionId, sessionId: params.sessionId,
}); });
const orderedSessions = derived(sessions, (sessions) =>{
return sessions.slice().sort((a, b) => a.meta.startTs - b.meta.startTs);
});
return { return {
session: derived(sessions, (sessions) => { orderedSessions,
session: derived(orderedSessions, (sessions) => {
const result = sessions.find( const result = sessions.find(
(session) => session.id === params.sessionId (session) => session.id === params.sessionId
); );
return result ? result : sessions[0]; return result ? result : sessions[0];
}), }),
previousSesssion: derived(sessions, (sessions) => { previousSesssion: derived(orderedSessions, (sessions) => {
const currentSessionIndex = sessions.findIndex( const currentSessionIndex = sessions.findIndex(
(session) => session.id === params.sessionId (session) => session.id === params.sessionId
); );
@ -35,7 +39,7 @@ export const load: PageLoad = async ({ parent, params }) => {
return undefined; return undefined;
} }
}), }),
nextSession: derived(sessions, (sessions) => { nextSession: derived(orderedSessions, (sessions) => {
const currentSessionIndex = sessions.findIndex( const currentSessionIndex = sessions.findIndex(
(session) => session.id === params.sessionId (session) => session.id === params.sessionId
); );