|
@@ -20,17 +20,55 @@ export const storeSecurityDelete = <T extends any>(items: T[], pushItem: T) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export const addStoreItem = <T extends {id: any}>(items: Ref<T[]>, addAction: (item: T) => Promise<T>) => {
|
|
|
+export function addStoreItem <T extends {id: any}>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ addAction: (item: T) => Promise<T>,
|
|
|
+): (item: T) => Promise<void>
|
|
|
+export function addStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ addAction: (item: K) => Promise<K>,
|
|
|
+ transform: (item: T) => Promise<K> | K
|
|
|
+): (item: T) => Promise<void>
|
|
|
+export function addStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ addAction: (item: K) => Promise<K>,
|
|
|
+ transform?: (item: T) => Promise<K> | K
|
|
|
+) {
|
|
|
return async (item: T) => {
|
|
|
- const newItem = await addAction(item)
|
|
|
+ let actionData: K
|
|
|
+ if (transform) {
|
|
|
+ actionData = await transform(item)
|
|
|
+ } else {
|
|
|
+ actionData = item as unknown as K
|
|
|
+ }
|
|
|
+ const newItem = await addAction(actionData)
|
|
|
item.id = newItem.id
|
|
|
storeSecurityPush(items.value, item)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export const updateStoreItem = <T extends {id: any}>(items: Ref<T[]>, updateAction: (item: T, oldItem: T) => Promise<any>) => {
|
|
|
+export function updateStoreItem <T extends {id: any}>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ updateAction: (item: T, oldItem: T) => Promise<any>,
|
|
|
+): (item: T, oldItem: T) => Promise<void>
|
|
|
+export function updateStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ updateAction: (item: K, oldItem: T) => Promise<any>,
|
|
|
+ transform: (item: T) => Promise<K> | K
|
|
|
+): (item: T, oldItem: T) => Promise<void>
|
|
|
+export function updateStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ updateAction: (item: K, oldItem: T) => Promise<any>,
|
|
|
+ transform?: (item: T) => Promise<K> | K
|
|
|
+) {
|
|
|
return async (item: T, oldItem: T) => {
|
|
|
- await updateAction(item, oldItem)
|
|
|
+ let actionData: K
|
|
|
+ if (transform) {
|
|
|
+ actionData = await transform(item)
|
|
|
+ } else {
|
|
|
+ actionData = item as unknown as K
|
|
|
+ }
|
|
|
+ await updateAction(actionData, oldItem)
|
|
|
const storeItem = items.value.find(atom => atom.id === item.id)
|
|
|
if (storeItem) {
|
|
|
Object.assign(storeItem, item)
|
|
@@ -38,10 +76,28 @@ export const updateStoreItem = <T extends {id: any}>(items: Ref<T[]>, updateActi
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-export const deleteStoreItem = <T extends {id: any}>(items: Ref<T[]>, deleteAction: (item: T) => Promise<any>) => {
|
|
|
+export function deleteStoreItem <T extends {id: any}>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ deleteAction: (item: T) => Promise<any>,
|
|
|
+): (item: T) => Promise<void>
|
|
|
+export function deleteStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ deleteAction: (item: K) => Promise<any>,
|
|
|
+ transform: (item: T) => Promise<K> | K
|
|
|
+): (item: T) => Promise<void>
|
|
|
+export function deleteStoreItem <T extends {id: any}, K extends {id: any} = T>(
|
|
|
+ items: Ref<T[]>,
|
|
|
+ deleteAction: (item: K) => Promise<any>,
|
|
|
+ transform?: (item: T) => Promise<K> | K
|
|
|
+) {
|
|
|
return async (item: T) => {
|
|
|
- await deleteAction(item)
|
|
|
+ let actionData: K
|
|
|
+ if (transform) {
|
|
|
+ actionData = await transform(item)
|
|
|
+ } else {
|
|
|
+ actionData = item as unknown as K
|
|
|
+ }
|
|
|
+ await deleteAction(actionData)
|
|
|
storeSecurityDelete(items.value, item)
|
|
|
}
|
|
|
}
|
|
@@ -113,9 +169,11 @@ export const diffStoreItemsChange = <T extends Array<{ id: any }>>(newItems: T,
|
|
|
}
|
|
|
|
|
|
export const recoverStoreItems = <T extends Array<{ id: any }>>(items: Ref<T>, getBackupItems: () => T) => () => {
|
|
|
+ console.log('?????')
|
|
|
const backupItems = getBackupItems()
|
|
|
items.value = backupItems.map(oldItem => {
|
|
|
const model = items.value.find(item => item.id === oldItem.id)
|
|
|
+ console.log(model, oldItem)
|
|
|
return model ? Object.assign(model, oldItem) : oldItem
|
|
|
}) as T
|
|
|
}
|