增加更新Scheme记录的功能。
This commit is contained in:
parent
8822432370
commit
1560b37b60
|
@ -1,7 +1,7 @@
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { useAtomValue, useSetAtom } from 'jotai';
|
import { useAtomValue, useSetAtom } from 'jotai';
|
||||||
import { atomWithStorage } from 'jotai/utils';
|
import { atomWithStorage } from 'jotai/utils';
|
||||||
import { isEqual } from 'lodash-es';
|
import { isEqual, reduce } from 'lodash-es';
|
||||||
import { useCallback, useMemo } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { v4 } from 'uuid';
|
import { v4 } from 'uuid';
|
||||||
|
|
||||||
|
@ -89,3 +89,27 @@ export function useCreateScheme(): (name: string, description?: string) => strin
|
||||||
|
|
||||||
return createSchemeAction;
|
return createSchemeAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useUpdateScheme(id: string): (updater: (prev: SchemeSet) => SchemeSet) => void {
|
||||||
|
const updateSchemes = useSetAtom(schemesAtom);
|
||||||
|
const updateAction = useCallback(
|
||||||
|
(updater: (prev: SchemeSet) => SchemeSet) => {
|
||||||
|
updateSchemes((prev) =>
|
||||||
|
reduce(
|
||||||
|
prev,
|
||||||
|
(acc, scheme) => {
|
||||||
|
if (isEqual(id, scheme.id)) {
|
||||||
|
acc.push(updater(scheme));
|
||||||
|
} else {
|
||||||
|
acc.push(scheme);
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
[] as SchemeSet[],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[id],
|
||||||
|
);
|
||||||
|
return updateAction;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user