WIP: Use useEffect to prevent duplicated load of resources

This commit is contained in:
Manuel Bustillo 2024-12-08 12:03:12 +01:00
parent 98d3afcd8e
commit e5279e7095
3 changed files with 23 additions and 24 deletions

View File

@ -1,15 +1,30 @@
/* Copyright (C) 2024 Manuel Bustillo*/ /* Copyright (C) 2024 Manuel Bustillo*/
'use client';
import { lusitana } from '@/app/ui/fonts'; import { lusitana } from '@/app/ui/fonts';
import ExpensesTable from '@/app/ui/expenses/table'; import ExpensesTable from '@/app/ui/expenses/table';
import { useState, useEffect } from 'react';
import { Expense } from '@/app/lib/definitions';
import { loadExpenses } from '@/app/api/expenses';
export default function Page () { export default function Page () {
const [expenses, setExpenses] = useState<Array<Expense>>([]);
function refreshExpenses() {
loadExpenses((expenses) => {
setExpenses(expenses);
});
}
useEffect(() => refreshExpenses, []);
return ( return (
<div className="w-full"> <div className="w-full">
<div className="w-full items-center justify-between"> <div className="w-full items-center justify-between">
<h1 className={`${lusitana.className} text-2xl`}>Expenses</h1> <h1 className={`${lusitana.className} text-2xl`}>Expenses</h1>
<h2 className={`${lusitana.className} text-xl`}>Summary</h2> <h2 className={`${lusitana.className} text-xl`}>Summary</h2>
<ExpensesTable /> <ExpensesTable expenses={expenses}/>
</div> </div>
</div> </div>
); );

View File

@ -11,33 +11,30 @@ import GroupsTable from '@/app/ui/groups/table';
import SkeletonTable from '@/app/ui/guests/skeleton-row'; import SkeletonTable from '@/app/ui/guests/skeleton-row';
import GuestsTable from '@/app/ui/guests/table'; import GuestsTable from '@/app/ui/guests/table';
import { TabPanel, TabView } from 'primereact/tabview'; import { TabPanel, TabView } from 'primereact/tabview';
import { Suspense, useState } from 'react'; import { Suspense, useEffect, useState } from 'react';
export default function Page() { export default function Page() {
function refreshGuests() { function refreshGuests() {
loadGuests((guests) => { loadGuests((guests) => {
setGuests(guests); setGuests(guests);
setGuestsLoaded(true);
}); });
} }
function refreshGroups() { function refreshGroups() {
loadGroups((groups) => { loadGroups((groups) => {
setGroups(groups); setGroups(groups);
setGroupsLoaded(true);
}); });
} }
const [groupsLoaded, setGroupsLoaded] = useState(false);
const [groups, setGroups] = useState<Array<Group>>([]); const [groups, setGroups] = useState<Array<Group>>([]);
const [guestsLoaded, setGuestsLoaded] = useState(false);
const [guests, setGuests] = useState<Array<Guest>>([]); const [guests, setGuests] = useState<Array<Guest>>([]);
const [guestBeingEdited, setGuestBeingEdited] = useState<Guest | undefined>(undefined); const [guestBeingEdited, setGuestBeingEdited] = useState<Guest | undefined>(undefined);
!groupsLoaded && refreshGroups(); useEffect(() => {
!guestsLoaded && refreshGuests(); refreshGroups();
refreshGuests();
}, []);
return ( return (
<div className="w-full"> <div className="w-full">

View File

@ -2,25 +2,12 @@
'use client' 'use client'
import { loadExpenses, updateExpense } from '@/app/api/expenses'; import { updateExpense } from '@/app/api/expenses';
import { Expense } from '@/app/lib/definitions'; import { Expense } from '@/app/lib/definitions';
import { useState } from "react";
import InlineTextField from "../components/form/inlineTextField"; import InlineTextField from "../components/form/inlineTextField";
import TableOfContents from "../components/table-of-contents"; import TableOfContents from "../components/table-of-contents";
export default function ExpensesTable() { export default function ExpensesTable({expenses}: {expenses: Array<Expense>}) {
const [expenses, setExpenses] = useState<Array<Expense>>([]);
const [expensesLoaded, setExpensesLoaded] = useState(false);
function refreshExpenses() {
loadExpenses((expenses) => {
setExpenses(expenses);
setExpensesLoaded(true);
});
}
!expensesLoaded && refreshExpenses();
return ( return (
<TableOfContents <TableOfContents
headers={['Name', 'Amount (€)', 'Pricing Type']} headers={['Name', 'Amount (€)', 'Pricing Type']}