This commit is contained in:
Ruidy 2022-03-10 11:07:29 -04:00
parent 5dd137b87e
commit 01dce1b76a
5 changed files with 22 additions and 20 deletions

View file

@ -1,39 +1,38 @@
import { client, Response } from '.';
import { Bill, BillFormType, billFrom } from '../types/bill';
export const billsURL = '/bills';
export const createBill = async (data: BillFormType) => {
try {
const { data: response } = await client.post<number>('/bills', data);
const { data: response } = await client.post<number>(billsURL, data);
return response;
} catch (error) {
console.error(error);
return 0;
}
};
export const updateBill = async (id: number, data: BillFormType): Promise<Response<void>> => {
try {
const { data: response } = await client.put<void>(`/bills/${id}`, data);
const { data: response } = await client.put<void>(`${billsURL}/${id}`, data);
return { data: response };
} catch (error) {
console.error(error);
return { error };
}
};
export const fetchOneBill = async (id: number): Promise<Response<Bill>> => {
try {
const { data } = await client.get<Bill>(`/bills/${id}`);
const { data } = await client.get<Bill>(`${billsURL}/${id}`);
return { data: billFrom(data) };
} catch (error) {
console.error(error);
return { error };
}
};
export const fetchAllBills = async () => {
try {
const { data } = await client.get<Bill[]>('/bills');
const { data } = await client.get<Bill[]>(billsURL);
return data;
} catch (error) {
console.error(error);
@ -43,7 +42,7 @@ export const fetchAllBills = async () => {
export const sendBillAsPDF = async (id: number) => {
try {
const { data } = await client.post<boolean>(`/bills/${id}/send`);
const { data } = await client.post<boolean>(`${billsURL}/${id}/send`);
return data;
} catch (error) {
console.error(error);

View file

@ -1,8 +1,8 @@
import axios from 'axios';
const BASE_URL = process.env.REACT_APP_API_URL;
const baseURL = process.env.REACT_APP_API_URL;
export const client = axios.create({ baseURL: BASE_URL });
export const client = axios.create({ baseURL });
export type Response<T> = {
data?: T;

View file

@ -1,19 +1,27 @@
import { client, Response } from '.';
import { Report, ReportFormType, ReportType } from '../types/report';
export const reportsURL = '/reports';
export const fetchReport = async ({
type,
year,
month
}: ReportFormType): Promise<Response<Report>> => {
const baseQueryURL = `/reports/?report_type=${type}&year=${year}`;
const queryURL =
const params =
type === ReportType.monthly && month
? baseQueryURL.concat(`&month=${month + 1}`)
: baseQueryURL;
? {
report_type: type,
month: month + 1,
year
}
: {
report_type: type,
year
};
try {
const { data } = await client.get<Report>(queryURL);
const { data } = await client.get<Report>(reportsURL, { params });
return {
data: {
// @ts-ignore

View file

@ -15,4 +15,3 @@ ReactDOM.render(
);
reportWebVitals();
export { enumToList } from './lib/enums';

View file

@ -1,5 +1 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';