Skip to content

Commit

Permalink
separate db data from storage context provider
Browse files Browse the repository at this point in the history
  • Loading branch information
yethranayeh committed Dec 31, 2024
1 parent 37406e3 commit 59dd38b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
5 changes: 1 addition & 4 deletions src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { CurrencyMapProvider } from "@/utils/CurrencyMapProvider";
import { StorageProvider } from "@/utils/StorageProvider";
import { CalculationView } from "./CalculationView";

export const Home = () => (
<CurrencyMapProvider mode='latest'>
<StorageProvider>
<CalculationView />
</StorageProvider>
<CalculationView />
</CurrencyMapProvider>
);
19 changes: 11 additions & 8 deletions src/routes/layout/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import { Footer } from "./Footer";

import { AuthGuard } from "@/utils/AuthGuard";
import ErrorBoundary from "@/components/ErrorBoundary";
import { StorageProvider } from "@/utils/StorageProvider";

export function MainLayout() {
return (
<div className='flex flex-col gap-4 min-h-screen overflow-x-hidden'>
<AuthGuard>
<ErrorBoundary>
<Header />
<SideMenu />
<StorageProvider>
<ErrorBoundary>
<Header />
<SideMenu />

<div className='w-full flex flex-1 justify-center px-4'>
<Outlet />
</div>
<Footer />
</ErrorBoundary>
<div className='w-full flex flex-1 justify-center px-4'>
<Outlet />
</div>
<Footer />
</ErrorBoundary>
</StorageProvider>
</AuthGuard>
</div>
);
Expand Down
19 changes: 17 additions & 2 deletions src/utils/CurrencyMapProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useContext, useEffect, useState, type PropsWithChildren } from "react";
import { collection, getDocs, limit, orderBy, query, where } from "firebase/firestore";
import { DatabaseContext } from "@/context/DatabaseContext";
import { sub } from "date-fns";

import { useStorage } from "@/hooks/useStorage";

import { CenterChild } from "@/components/CenterChild";
import { Alert, AlertDescription, AlertTitle } from "@/components/shadcn/Alert";
import { Button } from "@/components/shadcn/Button";
import { Gears } from "@/components/Gears";

import { DatabaseContext } from "@/context/DatabaseContext";
import { CurrencyMapContext } from "@/context/CurrencyMapContext";
import { sub } from "date-fns";

interface CurrencyMapProviderProps extends PropsWithChildren {
mode: "latest" | "monthly";
Expand All @@ -15,6 +19,7 @@ interface CurrencyMapProviderProps extends PropsWithChildren {
export function CurrencyMapProvider({ mode, children }: CurrencyMapProviderProps) {
const db = useContext(DatabaseContext);
const [currencyMap, setCurrencyMap] = useState<RateDefinitions[] | null>(null);
const storage = useStorage();

const [error, setError] = useState<string | null>(null);
const [isLoadingDelayed, setIsLoadingDelayed] = useState(false);
Expand Down Expand Up @@ -82,6 +87,16 @@ export function CurrencyMapProvider({ mode, children }: CurrencyMapProviderProps
return () => clearTimeout(loadingDelayTimeout);
}, [db, mode]);

useEffect(() => {
if (currencyMap?.length) {
const latest = currencyMap.slice(-1)[0];

if (storage.cache?.meta.createdAt !== latest.meta.createdAt) {
storage.setCache(latest);
}
}
}, [currencyMap, storage.cache]);

if (error) {
return (
<CenterChild>
Expand Down
10 changes: 1 addition & 9 deletions src/utils/StorageProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useEffect, useMemo, useState, type PropsWithChildren } from "react";
import { useMemo, useState, type PropsWithChildren } from "react";

import { getCache, getPreferences, setCache, setPreferences } from "./storage";
import { StorageContext } from "../context/StorageContext";
import { useCurrencyMapData } from "../hooks/useCurrencyMap";

export function StorageProvider(props: PropsWithChildren) {
const currencyMap = useCurrencyMapData()![0];
const [cacheState, setCacheState] = useState(getCache());
const [preferencesState, setPreferencesState] = useState(getPreferences());

Expand Down Expand Up @@ -37,11 +35,5 @@ export function StorageProvider(props: PropsWithChildren) {
[cacheState, preferencesState]
);

useEffect(() => {
if (currencyMap) {
update("cache", currencyMap);
}
}, [currencyMap]);

return <StorageContext.Provider value={value}>{props.children}</StorageContext.Provider>;
}

0 comments on commit 59dd38b

Please sign in to comment.