Skip to content

Commit

Permalink
πŸ”„ Merge pull request #8 from steeeee0223/feature/ui
Browse files Browse the repository at this point in the history
πŸ”§ `ui` Fix tree actions
  • Loading branch information
steeeee0223 authored Jan 25, 2024
2 parents e4813d0 + 25c5a0e commit 11ecc33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/ui/src/components/custom/tree/tree-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export function treeReducer(
{ ids, entities }: Entity<TreeItem>,
{ type, payload }: TreeAction<TreeItem>,
): Entity<TreeItem> {
let e;
switch (type) {
case "add":
payload.forEach((item) => (entities[item.id] = item));
return { ids: Object.keys(entities), entities };
case "set":
payload.forEach((item) => (entities[item.id] = item));
return { ids: Object.keys(entities), entities };
e = payload.reduce((acc, item) => ({ ...acc, [item.id]: item }), {});
// payload.forEach((item) => (entities[item.id] = item));
return { ids: Object.keys(e), entities: e };
case "update":
entities[payload.id] = payload;
return { ids, entities };
Expand Down
14 changes: 9 additions & 5 deletions packages/ui/src/components/custom/tree/tree-prodiver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TreeContext } from "./tree-context";

interface TreeProviderProps extends PropsWithChildren {
className?: string;
fetchItems: () => Promise<ActionState<never, TreeItem[]>>;
fetchItems?: () => Promise<ActionState<never, TreeItem[]>>;
isItemActive?: (id: string) => boolean;
onClickItem?: (id: string) => void;
}
Expand All @@ -29,10 +29,14 @@ export function TreeProvider({
}: TreeProviderProps) {
const $initialItems = { ids: [], entities: {} };
const [state, dispatch] = useReducer<TreeReducer>(treeReducer, $initialItems);
const { data, isLoading } = useFetch<TreeItem[]>(fetchItems, {
onSuccess: (data) => dispatch({ type: "set", payload: data }),
onError: (e) => toast.error(e),
});
const { data, isLoading } = useFetch<TreeItem[]>(
fetchItems!,
{
onSuccess: (data) => dispatch({ type: "set", payload: data }),
onError: (e) => toast.error(e),
},
[fetchItems],
);

const treeItems = Object.values(state.entities);
console.log(`treeItems:`, treeItems);
Expand Down

0 comments on commit 11ecc33

Please sign in to comment.