mirror of
https://gitea.gofwd.group/sean/gunbuilder-next-tailwind.git
synced 2025-12-06 02:56:45 -05:00
add state management and other stuff
This commit is contained in:
53
src/store/useBuildStore.ts
Normal file
53
src/store/useBuildStore.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { create, StateCreator } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
|
||||
export interface BuildPart {
|
||||
id: string;
|
||||
name: string;
|
||||
image_url: string;
|
||||
brand: {
|
||||
id: string;
|
||||
name: string;
|
||||
logo?: string;
|
||||
};
|
||||
category: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
offers: Array<{
|
||||
price: number;
|
||||
url: string;
|
||||
vendor: {
|
||||
name: string;
|
||||
logo?: string;
|
||||
};
|
||||
inStock?: boolean;
|
||||
shipping?: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface BuildState {
|
||||
selectedParts: Record<string, BuildPart | null>; // key: checklist component id
|
||||
selectPartForComponent: (componentId: string, part: BuildPart) => void;
|
||||
removePartForComponent: (componentId: string) => void;
|
||||
clearBuild: () => void;
|
||||
}
|
||||
|
||||
const buildStoreCreator: StateCreator<BuildState> = (set) => ({
|
||||
selectedParts: {},
|
||||
selectPartForComponent: (componentId, part) => set((state) => ({
|
||||
selectedParts: { ...state.selectedParts, [componentId]: part },
|
||||
})),
|
||||
removePartForComponent: (componentId) => set((state) => {
|
||||
const updated = { ...state.selectedParts };
|
||||
delete updated[componentId];
|
||||
return { selectedParts: updated };
|
||||
}),
|
||||
clearBuild: () => set({ selectedParts: {} }),
|
||||
});
|
||||
|
||||
export const useBuildStore = create<BuildState>()(
|
||||
persist(buildStoreCreator, {
|
||||
name: 'current-build-storage',
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user