Skip to content

Commit

Permalink
perf: 🚀 Optimised Sessions store in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
JamsRepos committed May 4, 2024
1 parent ad7b5a8 commit 4f96721
Showing 1 changed file with 32 additions and 55 deletions.
87 changes: 32 additions & 55 deletions apps/wizarr-frontend/src/stores/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,40 @@ export const useSessionsStore = defineStore('sessions', {
}),
actions: {
async getSessions() {
// Get the sessions from the API
const sessions = await this.$axios
.get<Sessions, { data: Sessions }>('/api/sessions')
.catch((err) => {
this.$toast.error('Could not get sessions');
return null;
});

// If the sessions are null, return
if (sessions === null) return;

// Update the sessions that are already in the store
this.sessions.forEach((session, index) => {
const new_session = sessions.data.find(
(new_session: Session) => new_session.id === session.id,
);
if (new_session) this.sessions[index] = new_session;
});

// Add the new sessions to the store if they don't exist
sessions.data.forEach((session: Session) => {
if (
!this.sessions.find(
(old_session) => old_session.id === session.id,
)
)
this.sessions.push(session);
});

// Remove the sessions that were not in the response
this.sessions.forEach((session, index) => {
if (
!sessions.data.find(
(new_session: Session) => new_session.id === session.id,
)
)
this.sessions.splice(index, 1);
});
try {
const response = await this.$axios.get<Sessions>('/api/sessions');
if (!response.data) throw new Error('No data received');

const newSessionsMap = new Map<number, Session>(response.data.map((session: Session) => [session.id, session]));

// Update existing sessions and add new ones
this.sessions = this.sessions.reduce((updatedSessions: Session[], session) => {
if (newSessionsMap.has(session.id)) {
const newSession = newSessionsMap.get(session.id);
if (newSession) {
updatedSessions.push(newSession);
newSessionsMap.delete(session.id);
}
} else {
updatedSessions.push(session);
}
return updatedSessions;
}, []);
} catch (error) {
this.$toast.error('Could not get sessions');
console.error(error);
}
},
async deleteSession(id: number) {
// Delete the session from the API
const response = await this.$axios
.delete(`/api/sessions/${id}`)
.catch((err) => {
this.$toast.error('Could not delete session');
console.error(err);
return null;
});

// If the response is null, return
if (response === null) return;

// Remove the session from the store
const index = this.sessions.findIndex(
(session: Session) => session.id === id,
);
if (index !== -1) this.sessions.splice(index, 1);
async deleteSession(id: number) {
try {
await this.$axios.delete(`/api/sessions/${id}`);
const index = this.sessions.findIndex(session => session.id === id);
if (index !== -1) this.sessions.splice(index, 1);
} catch (error) {
this.$toast.error('Could not delete session');
console.error(error);
}
},
},
persist: true,
Expand Down

0 comments on commit 4f96721

Please sign in to comment.