diff --git a/web/src/App.tsx b/web/src/App.tsx index ad09850..f02c671 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -1,6 +1,11 @@ import LoadFilePage from "./pages/LoadFilePage/LoadFilePage"; +import { AppStoreProvider } from "./store/AppStore"; export default function App() { - return (); + return ( + + + + ); } diff --git a/web/src/pages/LoadFilePage/LoadFilePage.tsx b/web/src/pages/LoadFilePage/LoadFilePage.tsx index b026380..d4f299a 100644 --- a/web/src/pages/LoadFilePage/LoadFilePage.tsx +++ b/web/src/pages/LoadFilePage/LoadFilePage.tsx @@ -1,8 +1,10 @@ import { useCallback, useEffect, useState } from 'react'; import LoadFileForm from './LoadFileForm'; import { fetchConfig, loadTransactions } from '../../services/api.service'; +import { useStore } from '../../store/AppStore'; export default function LoadFilePage() { + const { dispatch } = useStore(); const [profiles, setProfiles] = useState([]); const [servers, setServers] = useState([]); @@ -16,6 +18,11 @@ export default function LoadFilePage() { setServers(config.servers); setDefaultProfile(config.defaultProfile); setDefaultServer(config.defaultServer); + + dispatch({ + type: 'UPDATE_CONFIG', + payload: config + }); }, [setProfiles, setServers, setDefaultProfile, setDefaultServer]); useEffect(() => { @@ -24,14 +31,22 @@ export default function LoadFilePage() { const handleSubmit = useCallback(async (csvFile: File, profile: string, server: string) => { const data = await loadTransactions(csvFile, profile, server); - console.log(data); + + dispatch({ + type: 'UPDATE_DATA', + payload: { + profile, + server, + ...data + } + }); }, []); return (
-

Import Transactions

+

Import Transactions

({ + state: initialState, + dispatch: () => {} +}); + +export function AppStoreProvider({ children }: AppStoreProviderProps) { + const [state, dispatch] = useReducer(reducer, initialState); + + const context = useMemo(() => ({ state, dispatch }), [state, dispatch]); + + return ( + + {children} + + ); +} + +export function useStore(): UseStoreReturnType { + const { state, dispatch } = useContext(AppContext); + + return { + state, + dispatch, + } +} \ No newline at end of file diff --git a/web/src/types/store.ts b/web/src/types/store.ts new file mode 100644 index 0000000..8a9690e --- /dev/null +++ b/web/src/types/store.ts @@ -0,0 +1,37 @@ +import type { Transaction } from "./api"; + +export type State = { + availableProfiles: string[]; + availableServers: string[]; + defaultProfile?: string; + defaultServer?: string; + profile?: string; + server?: string; + transactions: Transaction[]; + skipped: string[]; +}; + +export type Action = +| { + type: 'UPDATE_CONFIG', + payload: { + profiles: string[], + servers: string[], + defaultProfile?: string, + defaultServer?: string, + } +} +| { + type: 'UPDATE_DATA', + payload: { + profile: string; + server: string; + transactions: Transaction[]; + skipped: string[]; + } +} + +export type StoreContext = { + state: State, + dispatch: React.Dispatch +}; \ No newline at end of file