Skip to content

Commit

Permalink
chore: readd stores
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaoxuan committed Oct 18, 2024
1 parent 1a8d4d5 commit 0668bf6
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
18 changes: 18 additions & 0 deletions starters/shopify-algolia/stores/add-product-store.ts
Original file line number Diff line number Diff line change
@@ -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<AddProductStore>((set) => ({
product: null,
combination: null,
setProduct: ({ product, combination }) => set(() => ({ product, combination })),
clean: () => set(() => ({ product: null, combination: null })),
}))
28 changes: 28 additions & 0 deletions starters/shopify-algolia/stores/cart-store.ts
Original file line number Diff line number Diff line change
@@ -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<CartStore>((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 })),
}))
11 changes: 11 additions & 0 deletions starters/shopify-algolia/stores/filter-transition-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { create } from "zustand"

interface FilterTransitionStore {
selected: string[]
set: (filter: string) => void
}

export const useFilterTransitionStore = create<FilterTransitionStore>((set) => ({
selected: [],
set: (filter: string) => set((prev) => ({ selected: [...prev.selected, filter] })),
}))
13 changes: 13 additions & 0 deletions starters/shopify-algolia/stores/filters-store.ts
Original file line number Diff line number Diff line change
@@ -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<FilterStore>((set) => ({
status: "idle",
set: (value: FilterStatus) => set(() => ({ status: value })),
}))
35 changes: 35 additions & 0 deletions starters/shopify-algolia/stores/modal-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { create } from "zustand"

export type Modal = "login" | "signup" | "search" | "facets-mobile" | "review"

interface ModalStore {
modals: Partial<Record<Modal, boolean>>
openModal: (modal: Modal) => void
closeModal: (modal: Modal) => void
closeAllModals: () => void
}

export const useModalStore = create<ModalStore>((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<Modal, boolean>
),
})),
}))
12 changes: 12 additions & 0 deletions starters/shopify-algolia/stores/user-store.ts
Original file line number Diff line number Diff line change
@@ -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<UserStore>((set) => ({
user: null,
setUser: (payload: PlatformUser | null) => set(() => ({ user: payload })),
}))

0 comments on commit 0668bf6

Please sign in to comment.