refactoring: changing param list of a fn

This commit is contained in:
2024-02-01 15:07:30 +01:00
parent a1abc450bf
commit 2261e83715
8 changed files with 26 additions and 18 deletions

View File

@@ -11,11 +11,11 @@ import { formatYearMonth } from "../lib/format";
// Next.js does not encode an utf-8 file name correctly when sending a form with a file attachment
// This is a workaround for that
const updateOrAddBillMiddleware = (locationId: string, billId:string|undefined, billYear:number|undefined, prevState:any, formData: FormData) => {
const updateOrAddBillMiddleware = (locationId: string, billId:string|undefined, billYear:number|undefined, billMonth:number|undefined, prevState:any, formData: FormData) => {
// URL encode the file name of the attachment so it is correctly sent to the server
const billAttachment = formData.get('billAttachment') as File;
formData.set('billAttachment', billAttachment, encodeURIComponent(billAttachment.name));
return updateOrAddBill(locationId, billId, billYear, prevState, formData);
return updateOrAddBill(locationId, billId, billYear, billMonth, prevState, formData);
}
export interface BillEditFormProps {
@@ -27,17 +27,17 @@ export const BillEditForm:FC<BillEditFormProps> = ({ location, bill }) => {
const { _id: billID, name, paid, attachment, notes, payedAmount } = bill ?? { _id:undefined, name:"", paid:false, notes:"" };
const { yearMonth:{year: billYear}, _id: locationID } = location;
const { yearMonth:{year: billYear, month: billMonth}, _id: locationID } = location;
const initialState = { message: null, errors: {} };
const handleAction = updateOrAddBillMiddleware.bind(null, locationID, billID, billYear);
const handleAction = updateOrAddBillMiddleware.bind(null, locationID, billID, billYear, billMonth);
const [ state, dispatch ] = useFormState(handleAction, initialState);
const [ isPaid, setIsPaid ] = React.useState<boolean>(paid);
// redirect to the main page
const handleCancel = () => {
gotoHome(billYear ? `/?year=${billYear}&month=${location.yearMonth.month}` : undefined);
gotoHome(location.yearMonth);
};
const billPaid_handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {