Files
evidencija-rezija/app/ui/MonthLocationList.tsx
Knee Cola 5bbf80c2ae Implement redirect with toast notification on profile save
- Add gotoHomeWithMessage function to navigationActions
- Redirect to home page after successful profile save
- Display success message in toast notification instead of in-form
- Check for profileSaved URL parameter on home page mount
- Clean up URL parameter after showing toast
- Move success message translation to home-page section
- Remove unused success state and message from AccountForm
- Remove useEffect import from AccountForm

User experience: After saving profile, users are redirected to the
familiar home screen and see a toast notification confirming the save.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 18:58:43 +01:00

113 lines
4.1 KiB
TypeScript

"use client";
import React from "react";
import { AddLocationButton } from "./AddLocationButton";
import { AddMonthButton } from "./AddMonthButton";
import { MonthCard } from "./MonthCard";
import Pagination from "./Pagination";
import { LocationCard } from "./LocationCard";
import { PrintButton } from "./PrintButton";
import { BillingLocation, YearMonth } from "../lib/db-types";
import { useRouter, useSearchParams } from "next/navigation";
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { useTranslations } from "next-intl";
const getNextYearMonth = (yearMonth:YearMonth) => {
const {year, month} = yearMonth;
return({
year: month===12 ? year+1 : year,
month: month===12 ? 1 : month+1
} as YearMonth);
}
export interface MonthLocationListProps {
availableYears?: number[];
months?: {
[key: string]: {
yearMonth: YearMonth;
locations: BillingLocation[];
monthlyExpense: number;
};
};
}
export const MonthLocationList:React.FC<MonthLocationListProps > = ({
availableYears,
months,
}) => {
const router = useRouter();
const search = useSearchParams();
const t = useTranslations("home-page");
const initialMonth = search.get('month')
const [expandedMonth, setExpandedMonth] = React.useState<number>(
initialMonth ? parseInt(initialMonth as string) : -1 // no month is expanded
);
// Check for profile saved success message
React.useEffect(() => {
if (search.get('profileSaved') === 'true') {
toast.success(t("profile-saved-message"), { theme: "dark" });
// Clean up URL parameter
const params = new URLSearchParams(search.toString());
params.delete('profileSaved');
const newSearch = params.toString();
router.replace(newSearch ? `?${newSearch}` : '/');
}
}, [search, router, t]);
if(!availableYears || !months) {
const currentYearMonth:YearMonth = {
year: new Date().getFullYear(),
month: new Date().getMonth() + 1
};
return(
<>
<MonthCard yearMonth={currentYearMonth} key={`month-${currentYearMonth}`} monthlyExpense={0} onToggle={() => {}} expanded={true} >
<AddLocationButton yearMonth={currentYearMonth} />
</MonthCard>
</>)
};
const monthsArray = Object.entries(months);
// when the month is toggled, update the URL
// and set the the new expandedMonth
const handleMonthToggle = (yearMonth:YearMonth) => {
// if the month is already expanded, collapse it
if(expandedMonth === yearMonth.month) {
// router.push(`/?year=${yearMonth.year}`);
setExpandedMonth(-1); // no month is expanded
} else {
// router.push(`/?year=${yearMonth.year}&month=${yearMonth.month}`);
setExpandedMonth(yearMonth.month);
}
}
return(<>
<AddMonthButton yearMonth={getNextYearMonth(monthsArray[0][1].locations[0].yearMonth)} />
{
monthsArray.map(([monthKey, { yearMonth, locations, monthlyExpense }], monthIx) =>
<MonthCard yearMonth={yearMonth} key={`month-${monthKey}`} monthlyExpense={monthlyExpense} expanded={ yearMonth.month === expandedMonth } onToggle={handleMonthToggle} >
{
yearMonth.month === expandedMonth ?
locations.map((location, ix) => <LocationCard key={`location-${location._id}`} location={location} />)
: null
}
<div className="flex gap-2 justify-center">
<AddLocationButton yearMonth={yearMonth} />
<PrintButton yearMonth={yearMonth} />
</div>
</MonthCard>
)
}
<div className="mt-5 flex w-full justify-center">
<Pagination availableYears={availableYears} />
</div>
<ToastContainer />
</>)
}