rename blocks to widgets

This commit is contained in:
Ren Amamiya
2023-08-11 15:25:33 +07:00
parent 0cfc3a48d8
commit 36b2acba6a
22 changed files with 118 additions and 107 deletions

View File

@@ -1,37 +0,0 @@
import { create } from 'zustand';
import { createJSONStorage, persist } from 'zustand/middleware';
import { createBlock, getBlocks, removeBlock } from '@libs/storage';
import { Block } from '@utils/types';
interface BlockState {
blocks: null | Array<Block>;
fetchBlocks: () => void;
setBlock: ({ kind, title, content }: Block) => void;
removeBlock: (id: string) => void;
}
export const useBlocks = create<BlockState>()(
persist(
(set) => ({
blocks: null,
fetchBlocks: async () => {
const blocks = await getBlocks();
set({ blocks: blocks });
},
setBlock: async ({ kind, title, content }: Block) => {
const block: Block = await createBlock(kind, title, content);
set((state) => ({ blocks: [...state.blocks, block] }));
},
removeBlock: async (id: string) => {
await removeBlock(id);
set((state) => ({ blocks: state.blocks.filter((block) => block.id !== id) }));
},
}),
{
name: 'blocks',
storage: createJSONStorage(() => localStorage),
}
)
);

37
src/stores/widgets.tsx Normal file
View File

@@ -0,0 +1,37 @@
import { create } from 'zustand';
import { createJSONStorage, persist } from 'zustand/middleware';
import { createWidget, getWidgets, removeWidget } from '@libs/storage';
import { Widget } from '@utils/types';
interface WidgetState {
widgets: null | Array<Widget>;
fetchWidgets: () => void;
setWidget: ({ kind, title, content }: Widget) => void;
removeWidget: (id: string) => void;
}
export const useWidgets = create<WidgetState>()(
persist(
(set) => ({
widgets: null,
fetchWidgets: async () => {
const widgets = await getWidgets();
set({ widgets: widgets });
},
setWidget: async ({ kind, title, content }: Widget) => {
const widget: Widget = await createWidget(kind, title, content);
set((state) => ({ widgets: [...state.widgets, widget] }));
},
removeWidget: async (id: string) => {
await removeWidget(id);
set((state) => ({ widgets: state.widgets.filter((widget) => widget.id !== id) }));
},
}),
{
name: 'blocks',
storage: createJSONStorage(() => localStorage),
}
)
);