mirror of
https://github.com/andwati/cymelle.git
synced 2026-11-19 14:45:14 +00:00
deploying
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import * as React from "react"
|
||||
|
||||
const MOBILE_BREAKPOINT = 768
|
||||
|
||||
export function useIsMobile() {
|
||||
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
|
||||
|
||||
React.useEffect(() => {
|
||||
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
|
||||
const onChange = () => {
|
||||
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
|
||||
}
|
||||
mql.addEventListener("change", onChange)
|
||||
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
|
||||
return () => mql.removeEventListener("change", onChange)
|
||||
}, [])
|
||||
|
||||
return !!isMobile
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import {login, logout, register} from "#/api/auth";
|
||||
import type {AuthUser, LoginRequest, RegisterRequest} from "#/types/auth";
|
||||
import {useMutation, useQueryClient} from "@tanstack/react-query";
|
||||
import {createContext, useContext, useState} from "react";
|
||||
|
||||
type AuthContextValue = {
|
||||
user: AuthUser | null;
|
||||
isLoading: boolean;
|
||||
login: (request: LoginRequest) => Promise<AuthUser>;
|
||||
register: (request: RegisterRequest) => Promise<AuthUser>;
|
||||
logout: () => Promise<void>;
|
||||
};
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
const storedUserKey = "cymelle.auth.user";
|
||||
|
||||
export const authQueryKeys = {
|
||||
me: ["auth", "me"] as const,
|
||||
};
|
||||
|
||||
export function AuthProvider({children}: {children: React.ReactNode}) {
|
||||
const queryClient = useQueryClient();
|
||||
const [user, setUser] = useState<AuthUser | null>(() => readStoredUser());
|
||||
|
||||
const loginMutation = useMutation({
|
||||
mutationFn: login,
|
||||
onSuccess: (user) => {
|
||||
queryClient.clear();
|
||||
persistUser(user);
|
||||
setUser(user);
|
||||
queryClient.setQueryData(authQueryKeys.me, user);
|
||||
},
|
||||
});
|
||||
|
||||
const registerMutation = useMutation({
|
||||
mutationFn: register,
|
||||
onSuccess: (user) => {
|
||||
queryClient.clear();
|
||||
persistUser(user);
|
||||
setUser(user);
|
||||
queryClient.setQueryData(authQueryKeys.me, user);
|
||||
},
|
||||
});
|
||||
|
||||
const logoutMutation = useMutation({
|
||||
mutationFn: logout,
|
||||
onSettled: () => {
|
||||
persistUser(null);
|
||||
setUser(null);
|
||||
queryClient.clear();
|
||||
},
|
||||
});
|
||||
|
||||
async function logoutUser() {
|
||||
persistUser(null);
|
||||
setUser(null);
|
||||
queryClient.clear();
|
||||
await logoutMutation.mutateAsync().catch(() => undefined);
|
||||
}
|
||||
|
||||
const value: AuthContextValue = {
|
||||
user,
|
||||
isLoading: false,
|
||||
login: loginMutation.mutateAsync,
|
||||
register: registerMutation.mutateAsync,
|
||||
logout: logoutUser,
|
||||
};
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const context = useContext(AuthContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used within AuthProvider");
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
function readStoredUser() {
|
||||
if (typeof window === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const raw = window.localStorage.getItem(storedUserKey);
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(raw) as AuthUser;
|
||||
} catch {
|
||||
window.localStorage.removeItem(storedUserKey);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function persistUser(user: AuthUser | null) {
|
||||
if (typeof window === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
window.localStorage.removeItem(storedUserKey);
|
||||
return;
|
||||
}
|
||||
|
||||
window.localStorage.setItem(storedUserKey, JSON.stringify(user));
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import type {Product} from "#/types/product";
|
||||
import {createContext, useContext, useEffect, useMemo, useState} from "react";
|
||||
|
||||
export type CartItem = {
|
||||
product: Product;
|
||||
quantity: number;
|
||||
};
|
||||
|
||||
type CartContextValue = {
|
||||
items: CartItem[];
|
||||
itemCount: number;
|
||||
totalAmount: number;
|
||||
currency: string;
|
||||
addItem: (product: Product, quantity: number) => void;
|
||||
updateQuantity: (productId: string, quantity: number) => void;
|
||||
removeItem: (productId: string) => void;
|
||||
clearCart: () => void;
|
||||
};
|
||||
|
||||
const CartContext = createContext<CartContextValue | null>(null);
|
||||
const CART_STORAGE_KEY = "cymelle_cart";
|
||||
|
||||
export function CartProvider({children}: {children: React.ReactNode}) {
|
||||
const [items, setItems] = useState<CartItem[]>(() => {
|
||||
if (typeof window === "undefined") {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(window.localStorage.getItem(CART_STORAGE_KEY) ?? "[]") as CartItem[];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
const itemCount = useMemo(
|
||||
() => items.reduce((total, item) => total + item.quantity, 0),
|
||||
[items],
|
||||
);
|
||||
|
||||
const totalAmount = useMemo(
|
||||
() => items.reduce((total, item) => total + item.product.price * item.quantity, 0),
|
||||
[items],
|
||||
);
|
||||
|
||||
const currency = items[0]?.product.currency ?? "KES";
|
||||
|
||||
useEffect(() => {
|
||||
window.localStorage.setItem(CART_STORAGE_KEY, JSON.stringify(items));
|
||||
}, [items]);
|
||||
|
||||
function addItem(product: Product, quantity: number) {
|
||||
const safeQuantity = Math.min(Math.max(quantity, 1), product.availableQuantity);
|
||||
|
||||
setItems((currentItems) => {
|
||||
const existing = currentItems.find((item) => item.product.id === product.id);
|
||||
|
||||
if (existing) {
|
||||
return currentItems.map((item) =>
|
||||
item.product.id === product.id
|
||||
? {
|
||||
...item,
|
||||
product,
|
||||
quantity: Math.min(item.quantity + safeQuantity, product.availableQuantity),
|
||||
}
|
||||
: item,
|
||||
);
|
||||
}
|
||||
|
||||
return [...currentItems, {product, quantity: safeQuantity}];
|
||||
});
|
||||
}
|
||||
|
||||
function updateQuantity(productId: string, quantity: number) {
|
||||
setItems((currentItems) =>
|
||||
currentItems.flatMap((item) => {
|
||||
if (item.product.id !== productId) {
|
||||
return [item];
|
||||
}
|
||||
|
||||
const nextQuantity = Math.min(Math.max(quantity, 0), item.product.availableQuantity);
|
||||
return nextQuantity === 0 ? [] : [{...item, quantity: nextQuantity}];
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function removeItem(productId: string) {
|
||||
setItems((currentItems) => currentItems.filter((item) => item.product.id !== productId));
|
||||
}
|
||||
|
||||
function clearCart() {
|
||||
setItems([]);
|
||||
}
|
||||
|
||||
return (
|
||||
<CartContext.Provider
|
||||
value={{
|
||||
items,
|
||||
itemCount,
|
||||
totalAmount,
|
||||
currency,
|
||||
addItem,
|
||||
updateQuantity,
|
||||
removeItem,
|
||||
clearCart,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</CartContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useCart() {
|
||||
const context = useContext(CartContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error("useCart must be used within CartProvider");
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import {cancelOrder, getOrders} from "#/api/orders";
|
||||
import {cancelOrder, createOrder, getOrders, updateOrderStatus} from "#/api/orders";
|
||||
import {inventoryQueryKeys} from "#/hooks/useInventory";
|
||||
import {productQueryKeys} from "#/hooks/useProducts";
|
||||
import type {OrderFilters} from "#/types/order";
|
||||
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";
|
||||
|
||||
@@ -29,7 +30,35 @@ export function useCancelOrder() {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryQueryKeys.all,
|
||||
}),
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productQueryKeys.all,
|
||||
}),
|
||||
]);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function useCreateOrder() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: createOrder,
|
||||
onSuccess: async () => {
|
||||
await Promise.all([
|
||||
queryClient.invalidateQueries({queryKey: orderQueryKeys.all}),
|
||||
queryClient.invalidateQueries({queryKey: inventoryQueryKeys.all}),
|
||||
queryClient.invalidateQueries({queryKey: productQueryKeys.all}),
|
||||
]);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateOrderStatus() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, status}: {id: string; status: import("#/types/order").OrderStatus}) =>
|
||||
updateOrderStatus(id, status),
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: orderQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import {createProduct, deactivateProduct, getProduct, getProducts, updateProduct} from "#/api/products";
|
||||
import type {ProductRequest} from "#/types/product";
|
||||
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";
|
||||
|
||||
export const productQueryKeys = {
|
||||
all: ["products"] as const,
|
||||
detail: (id: string) => [...productQueryKeys.all, "detail", id] as const,
|
||||
};
|
||||
|
||||
export function useProducts() {
|
||||
return useQuery({
|
||||
queryKey: productQueryKeys.all,
|
||||
queryFn: ({signal}) => getProducts(signal),
|
||||
});
|
||||
}
|
||||
|
||||
export function useProduct(id: string) {
|
||||
return useQuery({
|
||||
queryKey: productQueryKeys.detail(id),
|
||||
queryFn: ({signal}) => getProduct(id, signal),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateProduct() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: createProduct,
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: productQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateProduct() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, request}: {id: string; request: ProductRequest}) =>
|
||||
updateProduct(id, request),
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: productQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeactivateProduct() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: deactivateProduct,
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: productQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import {
|
||||
acceptRide,
|
||||
cancelRide,
|
||||
completeRide,
|
||||
getRides,
|
||||
requestRide,
|
||||
updateRideStatus,
|
||||
} from "#/api/rides";
|
||||
import type {CreateRideRequest, RideStatus} from "#/types/ride";
|
||||
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";
|
||||
|
||||
export const rideQueryKeys = {
|
||||
all: ["rides"] as const,
|
||||
};
|
||||
|
||||
export function useRides() {
|
||||
return useQuery({
|
||||
queryKey: rideQueryKeys.all,
|
||||
queryFn: ({signal}) => getRides(signal),
|
||||
});
|
||||
}
|
||||
|
||||
export function useRequestRide() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (request: CreateRideRequest) => requestRide(request),
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: rideQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAcceptRide() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: acceptRide,
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: rideQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCompleteRide() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: completeRide,
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: rideQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCancelRide() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: cancelRide,
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: rideQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateRideStatus() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, status}: {id: string; status: RideStatus}) =>
|
||||
updateRideStatus(id, status),
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: rideQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import {createUser, disableUser, getCustomers, getDrivers, getUsers, updateUser} from "#/api/users";
|
||||
import type {AdminUserRequest} from "#/types/user";
|
||||
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";
|
||||
|
||||
export const userQueryKeys = {
|
||||
all: ["users"] as const,
|
||||
list: () => [...userQueryKeys.all, "list"] as const,
|
||||
customers: () => [...userQueryKeys.all, "customers"] as const,
|
||||
drivers: () => [...userQueryKeys.all, "drivers"] as const,
|
||||
};
|
||||
|
||||
export function useUsers() {
|
||||
return useQuery({
|
||||
queryKey: userQueryKeys.list(),
|
||||
queryFn: ({signal}) => getUsers(signal),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCustomers() {
|
||||
return useQuery({
|
||||
queryKey: userQueryKeys.customers(),
|
||||
queryFn: ({signal}) => getCustomers(signal),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDrivers() {
|
||||
return useQuery({
|
||||
queryKey: userQueryKeys.drivers(),
|
||||
queryFn: ({signal}) => getDrivers(signal),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateUser() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: createUser,
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: userQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateUser() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, request}: {id: string; request: AdminUserRequest}) => updateUser(id, request),
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: userQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDisableUser() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: disableUser,
|
||||
onSuccess: () => queryClient.invalidateQueries({queryKey: userQueryKeys.all}),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user