refactoring: changing param list of a fn
This commit is contained in:
@@ -109,7 +109,7 @@ const serializeAttachment = async (billAttachment: File | null) => {
|
||||
* @param formData form data
|
||||
* @returns
|
||||
*/
|
||||
export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationId: string, billId:string|undefined, billYear:number|undefined, prevState:State, formData: FormData) => {
|
||||
export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationId: string, billId:string|undefined, billYear:number|undefined, billMonth:number|undefined, prevState:State, formData: FormData) => {
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
@@ -191,7 +191,9 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
}
|
||||
});
|
||||
}
|
||||
await gotoHome(billYear ? `/?year=${billYear}` : undefined);
|
||||
if(billYear && billMonth ) {
|
||||
await gotoHome({ year: billYear, month: billMonth });
|
||||
}
|
||||
})
|
||||
|
||||
export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
|
||||
@@ -219,7 +221,7 @@ export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:
|
||||
return([billLocation, bill] as [BillingLocation, Bill]);
|
||||
})
|
||||
|
||||
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string, year:number) => {
|
||||
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string, year:number, month:number) => {
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
@@ -240,6 +242,6 @@ export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID
|
||||
}
|
||||
});
|
||||
|
||||
await gotoHome(`/?year=${year}`);
|
||||
await gotoHome({year, month});
|
||||
return(post.modifiedCount);
|
||||
});
|
||||
@@ -138,5 +138,5 @@ export const deleteLocationById = withUser(async (user:AuthenticatedUser, locati
|
||||
// find a location with the given locationID
|
||||
const post = await dbClient.collection<BillingLocation>("lokacije").deleteOne({ _id: locationID, userId });
|
||||
|
||||
await gotoHome(`/?year=${yearMonth?.year}`)
|
||||
await gotoHome(yearMonth)
|
||||
})
|
||||
@@ -2,8 +2,15 @@
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from 'next/navigation';
|
||||
import { YearMonth } from "../db-types";
|
||||
|
||||
export async function gotoHome(path: string = '/') {
|
||||
export async function gotoHome({year, month}: YearMonth) {
|
||||
const path = `/?year=${year}&month=${month}`;
|
||||
await gotoUrl(path);
|
||||
}
|
||||
|
||||
export async function gotoUrl(path: string) {
|
||||
console.log(path)
|
||||
revalidatePath(path, "page");
|
||||
redirect(path);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import { FC } from "react";
|
||||
import { Bill, BillingLocation } from "../lib/db-types";
|
||||
import { deleteLocationById } from "../lib/actions/locationActions";
|
||||
import { useFormState } from "react-dom";
|
||||
import { Main } from "./Main";
|
||||
import { gotoHome } from "../lib/actions/navigationActions";
|
||||
@@ -15,11 +14,11 @@ export interface BillDeleteFormProps {
|
||||
|
||||
export const BillDeleteForm:FC<BillDeleteFormProps> = ({ bill, location }) =>
|
||||
{
|
||||
const handleAction = deleteBillById.bind(null, location._id, bill._id, location.yearMonth.year);
|
||||
const handleAction = deleteBillById.bind(null, location._id, bill._id, location.yearMonth.year, location.yearMonth.month);
|
||||
const [ state, dispatch ] = useFormState(handleAction, null);
|
||||
|
||||
const handleCancel = () => {
|
||||
gotoHome(`/?year=${location.yearMonth.year}&month=${location.yearMonth.month}`);
|
||||
gotoHome(location.yearMonth);
|
||||
};
|
||||
|
||||
return(
|
||||
|
||||
@@ -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>) => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { BillingLocation } from "../lib/db-types";
|
||||
import { deleteLocationById } from "../lib/actions/locationActions";
|
||||
import { useFormState } from "react-dom";
|
||||
import { Main } from "./Main";
|
||||
import { gotoHome } from "../lib/actions/navigationActions";
|
||||
import { gotoUrl } from "../lib/actions/navigationActions";
|
||||
|
||||
export interface LocationDeleteFormProps {
|
||||
/** location which should be deleted */
|
||||
@@ -18,7 +18,7 @@ export const LocationDeleteForm:FC<LocationDeleteFormProps> = ({ location }) =>
|
||||
const [ state, dispatch ] = useFormState(handleAction, null);
|
||||
|
||||
const handleCancel = () => {
|
||||
gotoHome(`/location/${location._id}/edit/`);
|
||||
gotoUrl(`/location/${location._id}/edit/`);
|
||||
};
|
||||
|
||||
return(
|
||||
|
||||
@@ -24,7 +24,7 @@ export const LocationEditForm:FC<LocationEditFormProps> = ({ location, yearMonth
|
||||
|
||||
// redirect to the main page
|
||||
const handleCancel = () => {
|
||||
gotoHome(location ? `/?year=${location?.yearMonth?.year}&month=${location?.yearMonth?.month}` : undefined);
|
||||
if(location) gotoHome(location?.yearMonth);
|
||||
};
|
||||
|
||||
return(
|
||||
|
||||
@@ -7,7 +7,7 @@ export default async function Page({ params:{ id } }: { params: { id:string } })
|
||||
const { year, month } = parseYearMonth(id);
|
||||
await addMonth({ year, month });
|
||||
|
||||
await gotoHome(`/?year=${year}`);
|
||||
await gotoHome({ year, month });
|
||||
|
||||
return null; // if we don't return anything, the client-side will not re-validate cache
|
||||
}
|
||||
Reference in New Issue
Block a user