Ship Expo Router screens, seeded stores, and polished UX so auth, shop, checkout, prescriptions, loyalty, family, and account processes work end-to-end offline. Co-authored-by: Cursor <cursoragent@cursor.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
import { create } from "zustand";
|
|
import { createJSONStorage, persist } from "zustand/middleware";
|
|
|
|
export type Fulfillment = "delivery" | "pickup";
|
|
export type PaymentMethod =
|
|
| "chapa"
|
|
| "telebirr"
|
|
| "mpesa"
|
|
| "pay_at_branch";
|
|
|
|
type LocationState = {
|
|
fulfillment: Fulfillment;
|
|
areaLabel: string;
|
|
branchId: string | null;
|
|
lat: number | null;
|
|
lng: number | null;
|
|
paymentPreference: PaymentMethod;
|
|
setFulfillment: (f: Fulfillment) => void;
|
|
setAreaLabel: (label: string) => void;
|
|
setBranchId: (id: string | null) => void;
|
|
setCoords: (lat: number | null, lng: number | null) => void;
|
|
setPaymentPreference: (m: PaymentMethod) => void;
|
|
allowedPayments: () => PaymentMethod[];
|
|
};
|
|
|
|
export const useLocationStore = create<LocationState>()(
|
|
persist(
|
|
(set, get) => ({
|
|
fulfillment: "delivery",
|
|
areaLabel: "Addis Ababa",
|
|
branchId: null,
|
|
lat: null,
|
|
lng: null,
|
|
paymentPreference: "chapa",
|
|
setFulfillment: (fulfillment) => {
|
|
const next: Partial<LocationState> = { fulfillment };
|
|
if (fulfillment === "delivery" && get().paymentPreference === "pay_at_branch") {
|
|
next.paymentPreference = "chapa";
|
|
}
|
|
set(next);
|
|
},
|
|
setAreaLabel: (areaLabel) => set({ areaLabel }),
|
|
setBranchId: (branchId) => set({ branchId }),
|
|
setCoords: (lat, lng) => set({ lat, lng }),
|
|
setPaymentPreference: (paymentPreference) => set({ paymentPreference }),
|
|
allowedPayments: () => {
|
|
const base: PaymentMethod[] = ["chapa", "telebirr", "mpesa"];
|
|
return get().fulfillment === "pickup"
|
|
? [...base, "pay_at_branch"]
|
|
: base;
|
|
},
|
|
}),
|
|
{
|
|
name: "gishen-location",
|
|
storage: createJSONStorage(() => AsyncStorage),
|
|
}
|
|
)
|
|
);
|