227 lines
5.4 KiB
TypeScript
227 lines
5.4 KiB
TypeScript
// src/actions/order.ts
|
|
import { useMemo } from 'react';
|
|
import axiosInstance, { endpoints, fetcher } from 'src/lib/axios';
|
|
import type { IOrderItem } from 'src/types/order';
|
|
import type { IProductItem } from 'src/types/product';
|
|
import type { SWRConfiguration } from 'swr';
|
|
import useSWR from 'swr';
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
const swrOptions: SWRConfiguration = {
|
|
revalidateIfStale: false,
|
|
revalidateOnFocus: false,
|
|
revalidateOnReconnect: false,
|
|
};
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type OrdersData = {
|
|
orders: IOrderItem[];
|
|
};
|
|
|
|
export function useGetOrders() {
|
|
const url = endpoints.order.list;
|
|
|
|
const { data, isLoading, error, isValidating, mutate } = useSWR<OrdersData>(
|
|
url,
|
|
fetcher,
|
|
swrOptions
|
|
);
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
orders: data?.orders || [],
|
|
ordersLoading: isLoading,
|
|
ordersError: error,
|
|
ordersValidating: isValidating,
|
|
ordersEmpty: !isLoading && !isValidating && !data?.orders.length,
|
|
mutate,
|
|
}),
|
|
[data?.orders, error, isLoading, isValidating, mutate]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type OrderData = {
|
|
order: IOrderItem;
|
|
};
|
|
|
|
export function useGetOrder(orderId: string) {
|
|
const url = orderId ? [endpoints.order.details, { params: { orderId } }] : '';
|
|
|
|
const { data, isLoading, error, isValidating } = useSWR<OrderData>(url, fetcher, swrOptions);
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
order: data?.order,
|
|
orderLoading: isLoading,
|
|
orderError: error,
|
|
orderValidating: isValidating,
|
|
}),
|
|
[data?.order, error, isLoading, isValidating]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type SearchResultsData = {
|
|
results: IProductItem[];
|
|
};
|
|
|
|
export function useSearchProducts(query: string) {
|
|
const url = query ? [endpoints.product.search, { params: { query } }] : '';
|
|
|
|
const { data, isLoading, error, isValidating } = useSWR<SearchResultsData>(url, fetcher, {
|
|
...swrOptions,
|
|
keepPreviousData: true,
|
|
});
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
searchResults: data?.results || [],
|
|
searchLoading: isLoading,
|
|
searchError: error,
|
|
searchValidating: isValidating,
|
|
searchEmpty: !isLoading && !isValidating && !data?.results.length,
|
|
}),
|
|
[data?.results, error, isLoading, isValidating]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type SaveOrderData = {
|
|
name: string;
|
|
city: string;
|
|
role: string;
|
|
email: string;
|
|
state: string;
|
|
status: string;
|
|
address: string;
|
|
country: string;
|
|
zipCode: string;
|
|
company: string;
|
|
avatarUrl: string;
|
|
phoneNumber: string;
|
|
isVerified: boolean;
|
|
//
|
|
ordername: string;
|
|
password: string;
|
|
};
|
|
|
|
export async function saveOrder(orderId: string, saveOrderData: SaveOrderData) {
|
|
// const url = orderId ? [endpoints.order.details, { params: { orderId } }] : '';
|
|
|
|
const res = await axiosInstance.post(
|
|
//
|
|
`http://localhost:7272/api/order/saveOrder?orderId=${orderId}`,
|
|
{
|
|
data: saveOrderData,
|
|
}
|
|
);
|
|
|
|
return res;
|
|
}
|
|
|
|
export async function uploadOrderImage(saveOrderData: SaveOrderData) {
|
|
console.log('uploadOrderImage ?');
|
|
// const url = orderId ? [endpoints.order.details, { params: { orderId } }] : '';
|
|
|
|
const res = await axiosInstance.get('http://localhost:7272/api/product/helloworld');
|
|
|
|
return res;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type CreateOrderData = {
|
|
name: string;
|
|
city: string;
|
|
role: string;
|
|
email: string;
|
|
state: string;
|
|
status: string;
|
|
address: string;
|
|
country: string;
|
|
zipCode: string;
|
|
company: string;
|
|
avatarUrl: string;
|
|
phoneNumber: string;
|
|
isVerified: boolean;
|
|
//
|
|
ordername: string;
|
|
password: string;
|
|
};
|
|
|
|
export async function createOrder(createOrderData: CreateOrderData) {
|
|
console.log('create product ?');
|
|
// const url = productId ? [endpoints.product.details, { params: { productId } }] : '';
|
|
|
|
const res = await axiosInstance.post('http://localhost:7272/api/order/createOrder', {
|
|
data: createOrderData,
|
|
});
|
|
|
|
return res;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type DeleteOrderResponse = {
|
|
success: boolean;
|
|
message?: string;
|
|
};
|
|
|
|
export async function deleteOrder(orderId: string): Promise<DeleteOrderResponse> {
|
|
const url = `http://localhost:7272/api/order/deleteOrder?orderId=${orderId}`;
|
|
|
|
try {
|
|
const res = await axiosInstance.delete(url);
|
|
|
|
return {
|
|
success: true,
|
|
message: 'Order deleted successfully',
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
message: error instanceof Error ? error.message : 'Failed to delete product',
|
|
};
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type ChangeStatusResponse = {
|
|
success: boolean;
|
|
message?: string;
|
|
};
|
|
|
|
export async function changeStatus(
|
|
orderId: string,
|
|
newOrderStatus: string
|
|
): Promise<ChangeStatusResponse> {
|
|
const url = endpoints.order.changeStatus(orderId);
|
|
|
|
try {
|
|
const res = await axiosInstance.put(url, { data: { status: newOrderStatus } });
|
|
|
|
return {
|
|
success: true,
|
|
message: 'status updated successfully',
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
message: error instanceof Error ? error.message : 'Failed to delete product',
|
|
};
|
|
}
|
|
}
|