Skip to content

Commit 95f9fa2

Browse files
committed
fix: identify usage of -1 as empty id and improve single item upsert performance
1 parent 84f2d43 commit 95f9fa2

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

resources/js/stores/DashboardStore.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,29 @@ export const useDashboardStore = defineStore('Dashboard', () => {
6363
watch(
6464
() => route?.params?.id,
6565
(URL_ID) => {
66-
stateLibraryId.value = parseInt(`${URL_ID}`) && parseInt(`${URL_ID}`) > 0 ? parseInt(`${URL_ID}`) : -1;
66+
stateLibraryId.value = parseInt(`${URL_ID}`) && parseInt(`${URL_ID}`) > 0 ? parseInt(`${URL_ID}`) : -1; // ISSUE: using -1 as an empty value
6767
},
6868
{ immediate: true },
6969
);
7070

7171
const updateSingleTask = (data: TaskResource) => {
72-
if (stateTasks.value.findIndex((task) => task.id === data.id) === -1) {
72+
const index = stateTasks.value.findIndex((task) => task.id === data.id);
73+
74+
if (index === -1) {
7375
stateTasks.value = [data, ...stateTasks.value];
7476
return;
7577
}
76-
stateTasks.value = stateTasks.value.map((task) => (task.id === data.id ? data : task));
78+
stateTasks.value = [...stateTasks.value.slice(0, index), data, ...stateTasks.value.slice(index + 1)];
7779
};
7880

7981
const updateSingleLibrary = (data: CategoryResource) => {
80-
if (stateLibraries.value.findIndex((lib) => lib.id === data.id) === -1) {
82+
const index = stateLibraries.value.findIndex((lib) => lib.id === data.id);
83+
84+
if (index === -1) {
8185
stateLibraries.value = [data, ...stateLibraries.value];
8286
return;
8387
}
84-
stateLibraries.value = stateLibraries.value.map((lib) => (lib.id === data.id ? data : lib));
88+
stateLibraries.value = [...stateLibraries.value.slice(0, index), data, ...stateLibraries.value.slice(index + 1)];
8589
};
8690

8791
return {

0 commit comments

Comments
 (0)