@data-fair/lib-vue 1.25.0 → 1.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/edit-fetch.d.ts +1 -0
- package/edit-fetch.js +8 -4
- package/package.json +1 -1
package/edit-fetch.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export declare function useEditFetch<T extends Record<string, any>>(url: Optiona
|
|
|
16
16
|
refresh: () => Promise<T | null>;
|
|
17
17
|
};
|
|
18
18
|
data: [T | null] extends [Ref<any, any>] ? import("@vue/shared").IfAny<Ref<any, any> & T, Ref<Ref<any, any> & T, Ref<any, any> & T>, Ref<any, any> & T> : Ref<import("vue").UnwrapRef<T> | null, T | import("vue").UnwrapRef<T> | null>;
|
|
19
|
+
serverData: [T | null] extends [Ref<any, any>] ? import("@vue/shared").IfAny<Ref<any, any> & T, Ref<Ref<any, any> & T, Ref<any, any> & T>, Ref<any, any> & T> : Ref<import("vue").UnwrapRef<T> | null, T | import("vue").UnwrapRef<T> | null>;
|
|
19
20
|
save: {
|
|
20
21
|
execute: () => Promise<void>;
|
|
21
22
|
notif: Ref<import("./ui-notif.js").UiNotif | undefined>;
|
package/edit-fetch.js
CHANGED
|
@@ -5,32 +5,36 @@ import useAsyncAction from './async-action.js';
|
|
|
5
5
|
import equal from 'fast-deep-equal';
|
|
6
6
|
export function useEditFetch(url, options = {}) {
|
|
7
7
|
const fetch = useFetch(url, options);
|
|
8
|
+
const serverData = ref(null);
|
|
8
9
|
const data = ref(null);
|
|
9
10
|
watch(fetch.data, () => {
|
|
10
11
|
// TODO: check for local changes before overwriting ?
|
|
12
|
+
serverData.value = fetch.data.value;
|
|
11
13
|
data.value = fetch.data.value;
|
|
12
14
|
});
|
|
13
15
|
const save = useAsyncAction(async () => {
|
|
14
|
-
if (!data.value || !fetch.data.value)
|
|
16
|
+
if (!data.value || !serverData.value || !fetch.data.value)
|
|
15
17
|
throw new Error('cannot save data that has not been fetched yet');
|
|
16
18
|
if (options.patch) {
|
|
17
19
|
const patch = {};
|
|
18
20
|
for (const key of Object.keys(data.value)) {
|
|
19
|
-
if (!equal(data.value[key],
|
|
21
|
+
if (!equal(data.value[key], serverData.value[key]))
|
|
20
22
|
patch[key] = data.value[key];
|
|
21
23
|
}
|
|
22
24
|
if (!Object.keys(patch).length)
|
|
23
25
|
return;
|
|
24
|
-
await ofetch(fetch.fullUrl.value, { method: 'PATCH', body: patch });
|
|
26
|
+
serverData.value = await ofetch(fetch.fullUrl.value, { method: 'PATCH', body: patch });
|
|
25
27
|
}
|
|
26
28
|
else {
|
|
27
29
|
// TODO: add if-unmodified-since header ?
|
|
28
|
-
await ofetch(fetch.fullUrl.value, { method: 'PUT', body: data.value });
|
|
30
|
+
serverData.value = await ofetch(fetch.fullUrl.value, { method: 'PUT', body: data.value });
|
|
29
31
|
}
|
|
32
|
+
serverData.value = data.value;
|
|
30
33
|
}, options.saveOptions);
|
|
31
34
|
return {
|
|
32
35
|
fetch,
|
|
33
36
|
data,
|
|
37
|
+
serverData,
|
|
34
38
|
save
|
|
35
39
|
};
|
|
36
40
|
}
|