From 0668bf630d511025f71538235cbcc4f98a1791b9 Mon Sep 17 00:00:00 2001 From: ddaoxuan Date: Fri, 18 Oct 2024 19:20:21 +0200 Subject: [PATCH] chore: readd stores --- .../stores/add-product-store.ts | 18 ++++++++++ starters/shopify-algolia/stores/cart-store.ts | 28 +++++++++++++++ .../stores/filter-transition-store.ts | 11 ++++++ .../shopify-algolia/stores/filters-store.ts | 13 +++++++ .../shopify-algolia/stores/modal-store.ts | 35 +++++++++++++++++++ starters/shopify-algolia/stores/user-store.ts | 12 +++++++ 6 files changed, 117 insertions(+) create mode 100644 starters/shopify-algolia/stores/add-product-store.ts create mode 100644 starters/shopify-algolia/stores/cart-store.ts create mode 100644 starters/shopify-algolia/stores/filter-transition-store.ts create mode 100644 starters/shopify-algolia/stores/filters-store.ts create mode 100644 starters/shopify-algolia/stores/modal-store.ts create mode 100644 starters/shopify-algolia/stores/user-store.ts diff --git a/starters/shopify-algolia/stores/add-product-store.ts b/starters/shopify-algolia/stores/add-product-store.ts new file mode 100644 index 00000000..53dc31ae --- /dev/null +++ b/starters/shopify-algolia/stores/add-product-store.ts @@ -0,0 +1,18 @@ +import type { PlatformVariant } from "lib/shopify/types" +import type { CommerceProduct } from "types" +import type { Combination } from "utils/product-options-utils" +import { create } from "zustand" + +interface AddProductStore { + product: CommerceProduct | null + combination: Combination | null + setProduct: ({ product, combination }: { product: CommerceProduct; combination: Combination | PlatformVariant }) => void + clean: () => void +} + +export const useAddProductStore = create((set) => ({ + product: null, + combination: null, + setProduct: ({ product, combination }) => set(() => ({ product, combination })), + clean: () => set(() => ({ product: null, combination: null })), +})) diff --git a/starters/shopify-algolia/stores/cart-store.ts b/starters/shopify-algolia/stores/cart-store.ts new file mode 100644 index 00000000..97c065fb --- /dev/null +++ b/starters/shopify-algolia/stores/cart-store.ts @@ -0,0 +1,28 @@ +import { PlatformCart } from "lib/shopify/types" +import { create } from "zustand" + +interface CartStore { + isOpen: boolean + isSheetLoaded: boolean + lastUpdatedAt: number + cart: PlatformCart | null + + openCart: () => void + closeCart: () => void + preloadSheet: () => void + refresh: () => void + setCart: (payload: PlatformCart | null) => void +} + +export const useCartStore = create((set) => ({ + isOpen: false, + lastUpdatedAt: 0, + cart: null, + isSheetLoaded: false, + + openCart: () => set(() => ({ isOpen: true, isSheetLoaded: true, lastUpdatedAt: Date.now() })), + closeCart: () => set(() => ({ isOpen: false, isSheetLoaded: true, lastUpdatedAt: Date.now() })), + preloadSheet: () => set(() => ({ isSheetLoaded: true })), + refresh: () => set(() => ({ lastUpdatedAt: Date.now() })), + setCart: (payload: PlatformCart | null) => set(() => ({ cart: payload })), +})) diff --git a/starters/shopify-algolia/stores/filter-transition-store.ts b/starters/shopify-algolia/stores/filter-transition-store.ts new file mode 100644 index 00000000..8156cad1 --- /dev/null +++ b/starters/shopify-algolia/stores/filter-transition-store.ts @@ -0,0 +1,11 @@ +import { create } from "zustand" + +interface FilterTransitionStore { + selected: string[] + set: (filter: string) => void +} + +export const useFilterTransitionStore = create((set) => ({ + selected: [], + set: (filter: string) => set((prev) => ({ selected: [...prev.selected, filter] })), +})) diff --git a/starters/shopify-algolia/stores/filters-store.ts b/starters/shopify-algolia/stores/filters-store.ts new file mode 100644 index 00000000..02e87039 --- /dev/null +++ b/starters/shopify-algolia/stores/filters-store.ts @@ -0,0 +1,13 @@ +import { create } from "zustand" + +type FilterStatus = "idle" | "hidden" | "visible" + +interface FilterStore { + status: FilterStatus + set: (value: FilterStatus) => void +} + +export const useFilterStore = create((set) => ({ + status: "idle", + set: (value: FilterStatus) => set(() => ({ status: value })), +})) diff --git a/starters/shopify-algolia/stores/modal-store.ts b/starters/shopify-algolia/stores/modal-store.ts new file mode 100644 index 00000000..cc57ad55 --- /dev/null +++ b/starters/shopify-algolia/stores/modal-store.ts @@ -0,0 +1,35 @@ +import { create } from "zustand" + +export type Modal = "login" | "signup" | "search" | "facets-mobile" | "review" + +interface ModalStore { + modals: Partial> + openModal: (modal: Modal) => void + closeModal: (modal: Modal) => void + closeAllModals: () => void +} + +export const useModalStore = create((set) => ({ + modals: {}, + + openModal: (modalId: Modal) => + set((state) => ({ + modals: { ...state.modals, [modalId]: true }, + })), + + closeModal: (modalId: Modal) => + set((state) => ({ + modals: { ...state.modals, [modalId]: false }, + })), + + closeAllModals: () => + set((state) => ({ + modals: Object.keys(state.modals).reduce( + (acc, modalId) => { + acc[modalId as Modal] = false + return acc + }, + {} as Record + ), + })), +})) diff --git a/starters/shopify-algolia/stores/user-store.ts b/starters/shopify-algolia/stores/user-store.ts new file mode 100644 index 00000000..28ea92da --- /dev/null +++ b/starters/shopify-algolia/stores/user-store.ts @@ -0,0 +1,12 @@ +import { PlatformUser } from "lib/shopify/types" +import { create } from "zustand" + +interface UserStore { + user: PlatformUser | null + setUser: (payload: PlatformUser | null) => void +} + +export const useUserStore = create((set) => ({ + user: null, + setUser: (payload: PlatformUser | null) => set(() => ({ user: payload })), +}))