Skip to content

Commit 4aff603

Browse files
AchoArnoldCopilot
andcommitted
fix(web): guard stale contacts loads
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 08f8d93 commit 4aff603

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎web/app/stores/contacts.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const useContactsStore = defineStore('contacts', () => {
1616
const search = ref('')
1717
const { apiFetch } = useApi()
1818
const notificationsStore = useNotificationsStore()
19+
let loadContactsGeneration = 0
1920

2021
const total = computed(() => contacts.value.length)
2122

@@ -39,6 +40,7 @@ export const useContactsStore = defineStore('contacts', () => {
3940
async function loadContacts(force = false): Promise<void> {
4041
if (contacts.value.length > 0 && !force) return
4142

43+
const generation = ++loadContactsGeneration
4244
loading.value = true
4345
try {
4446
const term = search.value.trim()
@@ -50,15 +52,22 @@ export const useContactsStore = defineStore('contacts', () => {
5052
'/v1/contacts',
5153
{ params },
5254
)
53-
contacts.value = response.data ?? []
55+
if (generation === loadContactsGeneration) {
56+
contacts.value = response.data ?? []
57+
}
5458
} catch (error: unknown) {
59+
if (generation !== loadContactsGeneration) {
60+
return
61+
}
5562
notificationsStore.addNotification({
5663
message: getApiErrorMessage(error, 'Error while loading contacts'),
5764
type: 'error',
5865
})
5966
throw error
6067
} finally {
61-
loading.value = false
68+
if (generation === loadContactsGeneration) {
69+
loading.value = false
70+
}
6271
}
6372
}
6473

@@ -172,6 +181,7 @@ export const useContactsStore = defineStore('contacts', () => {
172181
}
173182

174183
function resetState() {
184+
loadContactsGeneration++
175185
contacts.value = []
176186
loading.value = false
177187
search.value = ''

0 commit comments

Comments
 (0)