form action redirects user to tjhe appropriate year

This commit is contained in:
2024-01-17 15:47:55 +01:00
parent 119d64344f
commit 0eb11e7d02
18 changed files with 158 additions and 86 deletions

42
app/ui/BillDeleteForm.tsx Normal file
View File

@@ -0,0 +1,42 @@
"use client";
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";
import { deleteBillById } from "../lib/actions/billActions";
export interface BillDeleteFormProps {
bill: Bill,
location: BillingLocation
}
export const BillDeleteForm:FC<BillDeleteFormProps> = ({ bill, location }) =>
{
const handleAction = deleteBillById.bind(null, location._id, bill._id, location.yearMonth.year);
const [ state, dispatch ] = useFormState(handleAction, null);
const handleCancel = () => {
gotoHome(`/?year=${location.yearMonth.year}`);
};
return(
<Main>
<div className="card card-compact card-bordered min-w-[20em] max-w-[90em] bg-base-100 shadow-s my-1">
<div className="card-body">
<form action={dispatch}>
<p className="py-6 px-6">
Please confirm deletion of bill <strong>{bill.name}</strong> at <strong>{location.name}</strong>.
</p>
<div className="pt-4 text-center">
<button className="btn btn-primary">Confim</button>
<button type="button" className="btn btn-neutral ml-3" onClick={handleCancel}>Cancel</button>
</div>
</form>
</div>
</div>
</Main>
)
}

View File

@@ -4,37 +4,38 @@ import { DocumentIcon, TrashIcon } from "@heroicons/react/24/outline";
import { Bill } from "../lib/db-types";
import React, { FC } from "react";
import { useFormState } from "react-dom";
import { gotoHome, updateOrAddBill } from "../lib/actions/billActions";
import { updateOrAddBill } from "../lib/actions/billActions";
import Link from "next/link";
import { gotoHome } from "../lib/actions/navigationActions";
// 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, prevState:any, formData: FormData) => {
const updateOrAddBillMiddleware = (locationId: string, billId:string|undefined, billYear: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, prevState, formData);
return updateOrAddBill(locationId, billId, billYear, prevState, formData);
}
export interface BillEditFormProps {
locationID: string,
bill?: Bill
bill?: Bill,
billYear?: number
}
export const BillEditForm:FC<BillEditFormProps> = ({ locationID, bill }) => {
export const BillEditForm:FC<BillEditFormProps> = ({ locationID, bill, billYear }) => {
const { _id: billID, name, paid, attachment, notes, payedAmount } = bill ?? { _id:undefined, name:"", paid:false, notes:"" };
const initialState = { message: null, errors: {} };
const handleAction = updateOrAddBillMiddleware.bind(null, locationID, billID);
const handleAction = updateOrAddBillMiddleware.bind(null, locationID, billID, billYear);
const [ state, dispatch ] = useFormState(handleAction, initialState);
const [ isPaid, setIsPaid ] = React.useState<boolean>(paid);
// redirect to the main page
const handleCancel = () => {
console.log('handleCancel');
gotoHome();
gotoHome(billYear ? `/?year=${billYear}` : undefined);
};
const billPaid_handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {

View File

@@ -16,9 +16,9 @@ export const LocationCard:FC<LocationCardProps> = ({location: { _id, name, yearM
// sum all the billAmounts
const monthlyExpense = bills.reduce((acc, bill) => acc + (bill.payedAmount ?? 0), 0);
return(
<div className="card card-compact card-bordered max-w-[30em] bg-base-100 shadow-s my-1">
<div data-key={_id } className="card card-compact card-bordered max-w-[30em] bg-base-100 shadow-s my-1">
<div className="card-body">
<Link href={`/location/${_id}/edit`} className="card-subtitle tooltip" data-tip="Edit Location">
<Cog8ToothIcon className="h-[1em] w-[1em] absolute cursor-pointer top-3 right-3 text-2xl" />
@@ -26,7 +26,7 @@ export const LocationCard:FC<LocationCardProps> = ({location: { _id, name, yearM
<h2 className="card-title mr-[2em]">{formatYearMonth(yearMonth)} {name}</h2>
<div className="card-actions">
{
bills.map(bill => <BillBadge key={`${bill._id}`} locationId={_id} bill={bill} />)
bills.map(bill => <BillBadge key={`${_id}-${bill._id}`} locationId={_id} bill={bill} />)
}
<Link href={`/bill/${_id}/add`} className="tooltip" data-tip="Add a new bill">
<PlusCircleIcon className="h-[1em] w-[1em] cursor-pointer text-2xl" />

View File

@@ -0,0 +1,41 @@
"use client";
import { FC } from "react";
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";
export interface LocationDeleteFormProps {
/** location which should be deleted */
location: BillingLocation
}
export const LocationDeleteForm:FC<LocationDeleteFormProps> = ({ location }) =>
{
const handleAction = deleteLocationById.bind(null, location._id, location.yearMonth);
const [ state, dispatch ] = useFormState(handleAction, null);
const handleCancel = () => {
gotoHome(`/location/${location._id}/edit/`);
};
return(
<Main>
<div className="card card-compact card-bordered min-w-[20em] max-w-[90em] bg-base-100 shadow-s my-1">
<div className="card-body">
<form action={dispatch}>
<p className="py-6 px-6">
Please confirm deletion of location <strong>{location.name}</strong>.
</p>
<div className="pt-4 text-center">
<button className="btn btn-primary">Confim</button>
<button type="button" className="btn btn-neutral ml-3" onClick={handleCancel}>Cancel</button>
</div>
</form>
</div>
</div>
</Main>
)
}

View File

@@ -5,9 +5,9 @@ import { FC } from "react";
import { BillingLocation, YearMonth } from "../lib/db-types";
import { updateOrAddLocation } from "../lib/actions/locationActions";
import { useFormState } from "react-dom";
import { gotoHome } from "../lib/actions/billActions";
import { Main } from "./Main";
import Link from "next/link";
import { gotoHome } from "../lib/actions/navigationActions";
export interface LocationEditFormProps {
/** location which should be edited */
@@ -25,7 +25,7 @@ export const LocationEditForm:FC<LocationEditFormProps> = ({ location, yearMonth
// redirect to the main page
const handleCancel = () => {
console.log('handleCancel');
gotoHome();
gotoHome(location ? `/?year=${location?.yearMonth?.year}` : undefined);
};
return(

View File

@@ -83,7 +83,7 @@ export default function Pagination({ availableYears } : { availableYears: number
return (
<YearNumber
key={`${year}-${index}`}
key={`year-number-${year}-${index}`}
href={createYearURL(year)}
year={year}
position={position}