adding new billing location completed

This commit is contained in:
2024-01-05 15:13:15 +01:00
parent 3f7b681ab9
commit 94fee6f775
6 changed files with 24 additions and 14 deletions

View File

@@ -30,7 +30,7 @@ const UpdateLocation = FormSchema.omit({ _id: true });
* @param formData form data
* @returns
*/
export async function updateOrAddLocation(locationId?: string, prevState:State, formData: FormData) {
export async function updateOrAddLocation(locationId?: string, yearMonth?: string, prevState:State, formData: FormData) {
const validatedFields = UpdateLocation.safeParse({
locationName: formData.get('locationName'),
@@ -65,12 +65,12 @@ export async function updateOrAddLocation(locationId?: string, prevState:State,
notes: locationNotes,
}
});
} else {
} else if(yearMonth) {
await db.collection<BillingLocation>("lokacije").insertOne({
_id: (new ObjectId()).toHexString(),
name: locationName,
notes: locationNotes,
yearMonth: 202101, // ToDo: get the current year and month
yearMonth: parseInt(yearMonth), // ToDo: get the current year and month
bills: [],
});
}

View File

@@ -0,0 +1,6 @@
import { LocationEditForm } from '@/app/ui/LocationEditForm';
export default async function Page({ params:{ id:yearMonth } }: { params: { id:string } }) {
return (<LocationEditForm yearMonth={yearMonth} />);
}

View File

@@ -25,7 +25,7 @@ export const Page = async () => {
return (
<>
{
location.yearMonth !== array[0].yearMonth && location.yearMonth !== array[ix-1].yearMonth ? <AddLocationButton key={`add-loc-${location.yearMonth}`} /> : null
location.yearMonth !== array[0].yearMonth && location.yearMonth !== array[ix-1].yearMonth ? <AddLocationButton key={`add-loc-${location.yearMonth}`} yyyymm={array[ix-1].yearMonth} /> : null
}
{
// show month title if it's the first location in the month

View File

@@ -2,13 +2,15 @@ import { PlusCircleIcon } from "@heroicons/react/24/outline";
export interface AddLocationButtonProps {
/** year month at which the new billing location should be addes */
yyyymm: string
}
export const AddLocationButton:React.FC<AddLocationButtonProps> = () =>
export const AddLocationButton:React.FC<AddLocationButtonProps> = ({yyyymm}) =>
<div className="card card-compact card-bordered max-w-[36em] bg-base-100 shadow-s my-1">
<div className="card-body">
<span className='grid self-center' data-tip="Dodaj novu lokaciju">
<PlusCircleIcon className="h-[1em] w-[1em] cursor-pointer text-4xl" />
</span>
</div>
<a href={`/location/${yyyymm}/add`} className="card-body tooltip self-center" data-tip="Add a new billing location">
<span className='grid self-center' data-tip="Add a new billing location">
<PlusCircleIcon className="h-[1em] w-[1em] cursor-pointer text-4xl" />
</span>
</a>
</div>;

View File

@@ -21,7 +21,7 @@ export const LocationCard:FC<LocationCardProps> = ({location: { _id, name, yearM
{
bills.map(bill => <BillBadge key={`${bill._id}`} locationId={_id} bill={bill} />)
}
<a href={`/bill/${_id}/add`} className="tooltip" data-tip="Add a new bull">
<a href={`/bill/${_id}/add`} className="tooltip" data-tip="Add a new bill">
<PlusCircleIcon className="h-[1em] w-[1em] cursor-pointer text-2xl" />
</a>
</div>

View File

@@ -9,13 +9,15 @@ import { gotoHome } from "../lib/billActions";
export interface LocationEditFormProps {
/** location which should be edited */
location?: BillingLocation
location?: BillingLocation,
/** year month at a new billing location should be assigned */
yearMonth?: string
}
export const LocationEditForm:FC<LocationEditFormProps> = ({ location }) =>
export const LocationEditForm:FC<LocationEditFormProps> = ({ location, yearMonth }) =>
{
const initialState = { message: null, errors: {} };
const handleAction = updateOrAddLocation.bind(null, location?._id);
const handleAction = updateOrAddLocation.bind(null, location?._id, yearMonth);
const [ state, dispatch ] = useFormState(handleAction, initialState);
// redirect to the main page