"feat: enhance invoice management with schema updates, seed data, and new APIs"
This commit is contained in:
221
03_source/frontend/src/actions/invoice.ts
Normal file
221
03_source/frontend/src/actions/invoice.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
// src/actions/invoice.ts
|
||||
import { useMemo } from 'react';
|
||||
import axiosInstance, { endpoints, fetcher } from 'src/lib/axios';
|
||||
import type { IInvoiceItem } from 'src/types/invoice';
|
||||
import type { SWRConfiguration } from 'swr';
|
||||
import useSWR from 'swr';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
const swrOptions: SWRConfiguration = {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type InvoicesData = {
|
||||
invoices: IInvoiceItem[];
|
||||
};
|
||||
|
||||
export function useGetInvoices() {
|
||||
const url = endpoints.invoice.list;
|
||||
|
||||
const { data, isLoading, error, isValidating, mutate } = useSWR<InvoicesData>(
|
||||
url,
|
||||
fetcher,
|
||||
swrOptions
|
||||
);
|
||||
|
||||
const memoizedValue = useMemo(
|
||||
() => ({
|
||||
invoices: data?.invoices || [],
|
||||
invoicesLoading: isLoading,
|
||||
invoicesError: error,
|
||||
invoicesValidating: isValidating,
|
||||
invoicesEmpty: !isLoading && !isValidating && !data?.invoices.length,
|
||||
mutate,
|
||||
}),
|
||||
[data?.invoices, error, isLoading, isValidating, mutate]
|
||||
);
|
||||
|
||||
return memoizedValue;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type InvoiceData = {
|
||||
invoice: IInvoiceItem;
|
||||
};
|
||||
|
||||
export function useGetInvoice(invoiceId: string) {
|
||||
const url = invoiceId ? [endpoints.invoice.details, { params: { invoiceId } }] : '';
|
||||
|
||||
const { data, isLoading, error, isValidating } = useSWR<InvoiceData>(url, fetcher, swrOptions);
|
||||
|
||||
const memoizedValue = useMemo(
|
||||
() => ({
|
||||
currentInvoice: data?.invoice,
|
||||
invoiceLoading: isLoading,
|
||||
invoiceError: error,
|
||||
invoiceValidating: isValidating,
|
||||
}),
|
||||
[data?.invoice, error, isLoading, isValidating]
|
||||
);
|
||||
|
||||
return memoizedValue;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type SearchResultsData = {
|
||||
results: IInvoiceItem[];
|
||||
};
|
||||
|
||||
export function useSearchInvoices(query: string) {
|
||||
const url = query ? [endpoints.invoice.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 SaveInvoiceData = IInvoiceItem;
|
||||
|
||||
export async function saveInvoice(invoiceId: string, saveInvoiceData: SaveInvoiceData) {
|
||||
const url = endpoints.invoice.saveInvoice(invoiceId);
|
||||
const res = await axiosInstance.put(url, { data: saveInvoiceData });
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function uploadInvoiceImage(saveInvoiceData: SaveInvoiceData) {
|
||||
console.log('save invoice ?');
|
||||
// const url = invoiceId ? [endpoints.invoice.details, { params: { invoiceId } }] : '';
|
||||
|
||||
const res = await axiosInstance.get('http://localhost:7272/api/invoice/helloworld');
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type CreateInvoiceData = {
|
||||
// id: string;
|
||||
sku: string;
|
||||
name: string;
|
||||
code: string;
|
||||
price: number | null;
|
||||
taxes: number | null;
|
||||
tags: string[];
|
||||
sizes: string[];
|
||||
publish: string;
|
||||
gender: string[];
|
||||
coverUrl: string;
|
||||
images: (string | File)[];
|
||||
colors: string[];
|
||||
quantity: number | null;
|
||||
category: string;
|
||||
available: number;
|
||||
totalSold: number;
|
||||
description: string;
|
||||
totalRatings: number;
|
||||
totalReviews: number;
|
||||
inventoryType: string;
|
||||
subDescription: string;
|
||||
priceSale: number | null;
|
||||
newLabel: {
|
||||
content: string;
|
||||
enabled: boolean;
|
||||
};
|
||||
saleLabel: {
|
||||
content: string;
|
||||
enabled: boolean;
|
||||
};
|
||||
// ratings: {
|
||||
// name: string;
|
||||
// starCount: number;
|
||||
// reviewCount: number;
|
||||
// }[];
|
||||
};
|
||||
|
||||
export async function createInvoice(createInvoiceData: CreateInvoiceData) {
|
||||
console.log('create invoice ?');
|
||||
// const url = invoiceId ? [endpoints.invoice.details, { params: { invoiceId } }] : '';
|
||||
|
||||
const res = await axiosInstance.post('http://localhost:7272/api/invoice/createInvoice', {
|
||||
data: createInvoiceData,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type DeleteInvoiceResponse = {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export async function deleteInvoice(invoiceId: string): Promise<DeleteInvoiceResponse> {
|
||||
const url = `http://localhost:7272/api/invoice/deleteInvoice?invoiceId=${invoiceId}`;
|
||||
|
||||
try {
|
||||
const res = await axiosInstance.delete(url);
|
||||
console.log({ res });
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Invoice deleted successfully',
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to delete invoice',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type ChangeStatusResponse = {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export async function changeStatus(
|
||||
invoiceId: string,
|
||||
newStatus: string
|
||||
): Promise<ChangeStatusResponse> {
|
||||
const url = endpoints.invoice.changeStatus(invoiceId);
|
||||
|
||||
try {
|
||||
const res = await axiosInstance.put(url, { data: { status: newStatus } });
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'status updated successfully',
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to delete product',
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user