Changeset 057453c for src/api/invoice.ts


Ignore:
Timestamp:
02/26/25 10:05:32 (5 weeks ago)
Author:
Naum Shapkarovski <naumshapkarovski@…>
Branches:
main
Children:
299af01
Parents:
5d6f37a
Message:

feat: implement employees

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/api/invoice.ts

    r5d6f37a r057453c  
    11import { useMemo } from 'react';
    22// types
    3 import { Invoice } from 'mvpmasters-shared';
     3import { Invoice, UpdateInvoice } from 'src/schemas';
    44// db
    55import useSWR from 'swr';
    66import { endpoints, fetcher } from 'src/utils/axios';
     7import axios from 'src/utils/axios';
     8import { mutate } from 'swr';
     9import { useSWRConfig } from 'swr';
    710
    811interface InvoiceFilters {
     
    5154}
    5255
    53 // export function useGetInvoice({ id }: { id: string }) {
    54 //   const collectionName = collections.invoice;
     56export function useGetInvoice({ id }: { id: string }) {
     57  const path = endpoints.invoice;
    5558
    56 //   const { data, isLoading, error, isValidating } = useSWR(
    57 //     [collectionName, id],
    58 //     () => documentFetcher<Invoice>(collectionName, id),
    59 //     {
    60 //       revalidateOnFocus: false,
    61 //     }
    62 //   );
     59  const { data, isLoading, error, isValidating } = useSWR(
     60    `${path}/${id}`,
     61    () => fetcher<Invoice>(`${path}/${id}`),
     62    {
     63      revalidateOnFocus: false,
     64    }
     65  );
    6366
    64 //   const memoizedValue = useMemo(
    65 //     () => ({
    66 //       currentInvoice: data || null,
    67 //       currentInvoiceLoading: isLoading,
    68 //       currentInvoiceError: error,
    69 //       currentInvoiceValidating: isValidating,
    70 //       currentInvoiceEmpty: !isLoading && !data,
    71 //     }),
    72 //     [data, error, isLoading, isValidating]
    73 //   );
     67  const memoizedValue = useMemo(
     68    () => ({
     69      currentInvoice: data || null,
     70      currentInvoiceLoading: isLoading,
     71      currentInvoiceError: error,
     72      currentInvoiceValidating: isValidating,
     73      currentInvoiceEmpty: !isLoading && !data,
     74    }),
     75    [data, error, isLoading, isValidating]
     76  );
    7477
    75 //   return memoizedValue;
    76 // }
     78  return memoizedValue;
     79}
     80
     81// Add this interface for the create invoice payload
     82interface CreateInvoicePayload {
     83  createDate: Date;
     84  dueDate: Date;
     85  items: any[];
     86  invoiceNumber: string;
     87  invoiceFrom: any;
     88  invoiceTo: any;
     89  currency: string;
     90  quantityType: string;
     91  month: string;
     92  status?: string;
     93  taxes?: number;
     94  discount?: number;
     95  totalAmount: number;
     96  pdfRef?: string;
     97}
     98
     99export async function createInvoice(data: CreateInvoicePayload): Promise<Invoice> {
     100  const response = await axios.post<Invoice>(endpoints.invoice, data);
     101
     102  // Mutate the SWR cache to include the new invoice
     103  await mutate(
     104    endpoints.invoice,
     105    (existingInvoices: Invoice[] = []) => [response.data, ...existingInvoices],
     106    false // Set to false to avoid revalidation since we already have the new data
     107  );
     108
     109  return response.data;
     110}
     111
     112export async function updateInvoice(id: string, data: Partial<UpdateInvoice>) {
     113  const response = await axios.patch<Invoice>(`${endpoints.invoice}/${id}`, data);
     114
     115  // Mutate the individual invoice cache
     116  await mutate(`${endpoints.invoice}/${id}`, response.data, false);
     117
     118  // Mutate the invoice list cache
     119  await mutate(
     120    endpoints.invoice,
     121    (existingInvoices: Invoice[] = []) =>
     122      existingInvoices.map((invoice) => (invoice.id === id ? response.data : invoice)),
     123    false
     124  );
     125
     126  return response.data;
     127}
     128
     129export async function deleteInvoice(id: string) {
     130  const response = await axios.delete<Invoice>(`${endpoints.invoice}/${id}`);
     131
     132  // Mutate the invoice list cache to remove the deleted invoice
     133  await mutate(
     134    endpoints.invoice,
     135    (existingInvoices: Invoice[] = []) => existingInvoices.filter((invoice) => invoice.id !== id),
     136    false
     137  );
     138
     139  return response.data;
     140}
     141
     142// Update the useDeleteInvoice hook to use the new implementation
     143export function useDeleteInvoice() {
     144  const deleteInvoiceMutation = async (id: string) => {
     145    try {
     146      await deleteInvoice(id);
     147      return true;
     148    } catch (error) {
     149      console.error('Error deleting invoice:', error);
     150      throw error;
     151    }
     152  };
     153
     154  return { deleteInvoiceMutation };
     155}
Note: See TracChangeset for help on using the changeset viewer.