Skip to content

Commit a36c856

Browse files
committed
Expose loading state during API requests on useWorklogStorage()
1 parent 9d880a7 commit a36c856

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

components/applications/worklog-tracker/useWorklogStorage.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ref } from 'vue'
2+
13
import { WorklogItem } from '~/composables/server/worklog-tracker/types/worklogItem'
24

35
const url = '/api/worklog-tracker/worklogs'
@@ -15,6 +17,8 @@ function transformWorklogItem(worklogItem: WorklogItem): WorklogItem {
1517
}
1618

1719
export function useWorklogStorage() {
20+
const operationLoading = ref(false)
21+
1822
async function load(date: Date): Promise<WorklogItem[]> {
1923
const query = new URLSearchParams({
2024
date: date.toLocaleDateString()
@@ -27,41 +31,60 @@ export function useWorklogStorage() {
2731
}
2832

2933
async function save(worklogItem: WorklogItem): Promise<WorklogItem> {
30-
const response = await fetch(url, {
31-
method: 'POST',
32-
body: JSON.stringify(worklogItem),
33-
headers: {
34-
'Content-Type': 'application/json'
35-
}
36-
})
34+
try {
35+
operationLoading.value = true
3736

38-
const result = await response.json()
37+
const response = await fetch(url, {
38+
method: 'POST',
39+
body: JSON.stringify(worklogItem),
40+
headers: {
41+
'Content-Type': 'application/json'
42+
}
43+
})
3944

40-
return transformWorklogItem(result)
45+
const result = await response.json()
46+
47+
return transformWorklogItem(result)
48+
} finally {
49+
operationLoading.value = false
50+
}
4151
}
4252

4353
async function remove(worklogItem: WorklogItem): Promise<void> {
44-
const query = new URLSearchParams({
45-
issueId: worklogItem.issueId,
46-
worklogId: worklogItem.id
47-
})
54+
try {
55+
operationLoading.value = true
4856

49-
await fetch(`${url}?${query.toString()}`, {
50-
method: 'DELETE'
51-
})
57+
const query = new URLSearchParams({
58+
issueId: worklogItem.issueId,
59+
worklogId: worklogItem.id
60+
})
61+
62+
await fetch(`${url}?${query.toString()}`, {
63+
method: 'DELETE'
64+
})
65+
} finally {
66+
operationLoading.value = false
67+
}
5268
}
5369

5470
async function update(worklogItem: WorklogItem): Promise<void> {
55-
await fetch(url, {
56-
method: 'PUT',
57-
body: JSON.stringify(worklogItem),
58-
headers: {
59-
'Content-Type': 'application/json'
60-
}
61-
})
71+
try {
72+
operationLoading.value = true
73+
74+
await fetch(url, {
75+
method: 'PUT',
76+
body: JSON.stringify(worklogItem),
77+
headers: {
78+
'Content-Type': 'application/json'
79+
}
80+
})
81+
} finally {
82+
operationLoading.value = false
83+
}
6284
}
6385

6486
return {
87+
operationLoading,
6588
load,
6689
save,
6790
remove,

0 commit comments

Comments
 (0)