mirror of
https://github.com/rjNemo/melon_frontend
synced 2026-06-12 05:16:46 +00:00
feat: edit bill
This commit is contained in:
parent
6ab5c8afe9
commit
ef167a6c37
2 changed files with 32 additions and 12 deletions
|
|
@ -4,6 +4,11 @@ import { Bill, BillFormType } from '../types/bill';
|
||||||
const BASE_URL = process.env.REACT_APP_API_URL;
|
const BASE_URL = process.env.REACT_APP_API_URL;
|
||||||
const client = axios.create({ baseURL: BASE_URL });
|
const client = axios.create({ baseURL: BASE_URL });
|
||||||
|
|
||||||
|
type Response<T> = {
|
||||||
|
data?: T;
|
||||||
|
error?: any;
|
||||||
|
};
|
||||||
|
|
||||||
export const createBill = async (data: BillFormType) => {
|
export const createBill = async (data: BillFormType) => {
|
||||||
try {
|
try {
|
||||||
const { data: response } = await client.post<number>('/', data);
|
const { data: response } = await client.post<number>('/', data);
|
||||||
|
|
@ -14,9 +19,14 @@ export const createBill = async (data: BillFormType) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
type Response<T> = {
|
export const updateBill = async (id: number, data: BillFormType): Promise<Response<void>> => {
|
||||||
data?: T;
|
try {
|
||||||
error?: any;
|
const { data: response } = await client.put<void>(`/${id}`, data);
|
||||||
|
return { data: response };
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return { error };
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const billFrom = (bill: any): Bill =>
|
const billFrom = (bill: any): Bill =>
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { Button, Col, Divider, message, PageHeader, Space, Typography } from 'antd';
|
import { Button, Col, Divider, message, PageHeader, Space, Typography } from 'antd';
|
||||||
import { useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { useHistory, useParams } from 'react-router-dom';
|
import { useHistory, useParams } from 'react-router-dom';
|
||||||
import { createBill, fetchOneBill, sendBillAsPDF } from '../../api';
|
import { fetchOneBill, sendBillAsPDF, updateBill } from '../../api';
|
||||||
import { BillForm } from '../../components/billForm';
|
import { BillForm } from '../../components/billForm';
|
||||||
import { withLayout } from '../../layouts/main';
|
import { withLayout } from '../../layouts/main';
|
||||||
import { Bill, BillFormType } from '../../types/bill';
|
import { Bill, BillFormType } from '../../types/bill';
|
||||||
|
|
@ -12,7 +12,7 @@ export type QueryParams = { id: string };
|
||||||
|
|
||||||
const BillPage = () => {
|
const BillPage = () => {
|
||||||
// Hooks
|
// Hooks
|
||||||
const { id } = useParams<QueryParams>();
|
let { id } = useParams<QueryParams>();
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const { handleSubmit, control, reset } = useForm<BillFormType>();
|
const { handleSubmit, control, reset } = useForm<BillFormType>();
|
||||||
|
|
||||||
|
|
@ -22,17 +22,22 @@ const BillPage = () => {
|
||||||
const [bill, setBill] = useState<Bill>({} as Bill);
|
const [bill, setBill] = useState<Bill>({} as Bill);
|
||||||
|
|
||||||
// Effects
|
// Effects
|
||||||
useEffect(() => {
|
const loadBill = useCallback(
|
||||||
(async (id: string) => {
|
async (id: string) => {
|
||||||
const billID = parseInt(id);
|
const billID = parseInt(id);
|
||||||
const { data: loadedBill, error } = await fetchOneBill(billID);
|
const { data: loadedBill, error } = await fetchOneBill(billID);
|
||||||
if (loadedBill && error) {
|
if (loadedBill && !error) {
|
||||||
setBill(() => loadedBill);
|
setBill(() => loadedBill);
|
||||||
const { id, ...values } = loadedBill;
|
const { id, ...values } = loadedBill;
|
||||||
reset(values);
|
reset(values);
|
||||||
}
|
}
|
||||||
})(id);
|
},
|
||||||
}, [id, reset]);
|
[reset]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadBill(id);
|
||||||
|
}, [id, loadBill, reset]);
|
||||||
|
|
||||||
// Logic
|
// Logic
|
||||||
const handleSendPDF = (id: number) => {
|
const handleSendPDF = (id: number) => {
|
||||||
|
|
@ -42,7 +47,12 @@ const BillPage = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const onSubmit = handleSubmit(async (data) => {
|
const onSubmit = handleSubmit(async (data) => {
|
||||||
await createBill(data);
|
const { error } = await updateBill(parseInt(id), data);
|
||||||
|
if (error) {
|
||||||
|
message.error(`Update failed because of ${error}`);
|
||||||
|
}
|
||||||
|
setEdit(() => false);
|
||||||
|
loadBill(id);
|
||||||
});
|
});
|
||||||
|
|
||||||
const content = edit ? (
|
const content = edit ? (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue