diff --git a/context/MovimentationCtx.tsx b/context/MovimentationCtx.tsx index f72e545..4853778 100644 --- a/context/MovimentationCtx.tsx +++ b/context/MovimentationCtx.tsx @@ -12,6 +12,7 @@ interface IMovimentationCtx { spentValues: IMovimentation[]; addMovimentation: (movimentation: IMovimentation) => void; removeMovimentation: (id: string) => void; + editMovimentation: (id: string, mov: IMovimentation) => void; } const Movimentation = createContext(undefined); @@ -100,6 +101,36 @@ const MovimentationProvider = ({ children }: { children: ReactNode }) => { } }; + const editMovimentation = (id: string, mov: IMovimentation) => { + const { date, reason, value } = mov; + + const formatDate = (dateStr: string) => { + const [day, month, year] = dateStr.split("-"); + return `${year}/${month}/${day}`; + }; + + const treatedDate = formatDate(date as string); + + const updatedReceivedValues = receivedValues.map((receivedValue) => + receivedValue.id === id + ? { ...receivedValue, date: treatedDate, reason, value } + : receivedValue + ); + + const updatedSpentValues = spentValues.map((spentValue) => + spentValue.id === id + ? { ...spentValue, date: treatedDate, reason, value } + : spentValue + ); + + // Atualiza o estado e salva no storage + setReceivedValues(updatedReceivedValues); + setSpentValues(updatedSpentValues); + + saveToStorage("receivedValues", updatedReceivedValues); + saveToStorage("spentValues", updatedSpentValues); + }; + return ( { spentValues, addMovimentation, removeMovimentation, + editMovimentation, }} > {children} diff --git a/src/App.tsx b/src/App.tsx index f041473..8275b13 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,6 +4,7 @@ import Header from "./components/Header"; import Informations from "./components/Informations"; import ReceivedTable from "./components/ReceivedTable"; import RemoveModal from "./components/RemoveModal"; +import EditModal from "./components/EditModal"; import SpentTable from "./components/SpentTable"; const App = () => { @@ -31,6 +32,7 @@ const App = () => { +