@data-fair/lib-vue 1.25.4 → 1.25.5
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 +20 -11
- package/package.json +1 -1
package/edit-fetch.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { AsyncActionOptions } from './async-action.js';
|
|
|
4
4
|
type UseEditFetchOptions = UseFetchOptions & {
|
|
5
5
|
patch?: boolean;
|
|
6
6
|
saveOptions?: AsyncActionOptions;
|
|
7
|
+
fetchAfterSave?: boolean;
|
|
7
8
|
};
|
|
8
9
|
type OptionalUrl = string | null | undefined;
|
|
9
10
|
export declare function useEditFetch<T extends Record<string, any>>(url: OptionalUrl | Ref<OptionalUrl> | (() => OptionalUrl), options?: UseEditFetchOptions): {
|
package/edit-fetch.js
CHANGED
|
@@ -14,16 +14,16 @@ export function useEditFetch(url, options = {}) {
|
|
|
14
14
|
data.value = clone(fetch.data.value);
|
|
15
15
|
});
|
|
16
16
|
const hasDiff = computed(() => !equal(data.value, serverData.value));
|
|
17
|
-
const getPatch = () => {
|
|
18
|
-
if (!
|
|
17
|
+
const getPatch = (oldData, newData) => {
|
|
18
|
+
if (!oldData || !newData)
|
|
19
19
|
return null;
|
|
20
20
|
const patch = {};
|
|
21
|
-
for (const key of Object.keys(
|
|
22
|
-
if (!equal(
|
|
23
|
-
patch[key] =
|
|
21
|
+
for (const key of Object.keys(newData)) {
|
|
22
|
+
if (!equal(newData[key], oldData[key]))
|
|
23
|
+
patch[key] = newData[key];
|
|
24
24
|
}
|
|
25
|
-
for (const key of Object.keys(
|
|
26
|
-
if (!(key in
|
|
25
|
+
for (const key of Object.keys(oldData)) {
|
|
26
|
+
if (!(key in newData))
|
|
27
27
|
patch[key] = null;
|
|
28
28
|
}
|
|
29
29
|
return patch;
|
|
@@ -31,17 +31,26 @@ export function useEditFetch(url, options = {}) {
|
|
|
31
31
|
const save = useAsyncAction(async () => {
|
|
32
32
|
if (!data.value || !serverData.value || !fetch.data.value)
|
|
33
33
|
throw new Error('cannot save data that has not been fetched yet');
|
|
34
|
+
let res;
|
|
35
|
+
const dataBeforeSave = clone(data.value);
|
|
34
36
|
if (options.patch) {
|
|
35
|
-
const patch = getPatch();
|
|
37
|
+
const patch = getPatch(serverData.value, data.value);
|
|
36
38
|
if (!Object.keys(patch).length)
|
|
37
39
|
return;
|
|
38
|
-
|
|
40
|
+
res = await ofetch(fetch.fullUrl.value, { method: 'PATCH', body: patch });
|
|
39
41
|
}
|
|
40
42
|
else {
|
|
41
43
|
// TODO: add if-unmodified-since header ?
|
|
42
|
-
|
|
44
|
+
res = await ofetch(fetch.fullUrl.value, { method: 'PUT', body: data.value });
|
|
45
|
+
}
|
|
46
|
+
if (options.fetchAfterSave || !res) {
|
|
47
|
+
fetch.refresh();
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
serverData.value = clone(res);
|
|
51
|
+
// case of server-side calculated properties, updatedAt, etc
|
|
52
|
+
Object.assign(data.value, getPatch(dataBeforeSave, res));
|
|
43
53
|
}
|
|
44
|
-
serverData.value = clone(data.value);
|
|
45
54
|
}, options.saveOptions);
|
|
46
55
|
return {
|
|
47
56
|
fetch,
|