diff --git a/src/Routers/routes/ResourceRoutes.tsx b/src/Routers/routes/ResourceRoutes.tsx index 4b4f3f63c58..357b7812513 100644 --- a/src/Routers/routes/ResourceRoutes.tsx +++ b/src/Routers/routes/ResourceRoutes.tsx @@ -1,21 +1,13 @@ -import { Redirect } from "raviger"; - +import View from "@/components/Common/View"; import BoardView from "@/components/Resource/ResourceBoard"; import ResourceDetails from "@/components/Resource/ResourceDetails"; import { ResourceDetailsUpdate } from "@/components/Resource/ResourceDetailsUpdate"; import ListView from "@/components/Resource/ResourceList"; import { AppRoutes } from "@/Routers/AppRouter"; -import { getDefaultView } from "@/Utils/viewStorageUtils"; const ResourceRoutes: AppRoutes = { - "/resource": () => ( - - ), - "/resource/board": () => , - "/resource/list": () => , + "/resource": () => , "/resource/:id": ({ id }) => , "/resource/:id/update": ({ id }) => , }; diff --git a/src/Routers/routes/ShiftingRoutes.tsx b/src/Routers/routes/ShiftingRoutes.tsx index e59240d40d7..fe3b4effaa0 100644 --- a/src/Routers/routes/ShiftingRoutes.tsx +++ b/src/Routers/routes/ShiftingRoutes.tsx @@ -1,5 +1,4 @@ -import { Redirect } from "raviger"; - +import View from "@/components/Common/View"; import { ShiftCreate } from "@/components/Patient/ShiftCreate"; import ShiftDetails from "@/components/Shifting/ShiftDetails"; import { ShiftDetailsUpdate } from "@/components/Shifting/ShiftDetailsUpdate"; @@ -7,14 +6,9 @@ import BoardView from "@/components/Shifting/ShiftingBoard"; import ListView from "@/components/Shifting/ShiftingList"; import { AppRoutes } from "@/Routers/AppRouter"; -import { getDefaultView } from "@/Utils/viewStorageUtils"; const ShiftingRoutes: AppRoutes = { - "/shifting": () => ( - - ), - "/shifting/board": () => , - "/shifting/list": () => , + "/shifting": () => , "/shifting/:id": ({ id }) => , "/shifting/:id/update": ({ id }) => , "/facility/:facilityId/patient/:patientId/shift/new": ({ diff --git a/src/Utils/useView.ts b/src/Utils/useView.ts new file mode 100644 index 00000000000..278ca8e223d --- /dev/null +++ b/src/Utils/useView.ts @@ -0,0 +1,24 @@ +import { useEffect, useState } from "react"; + +export function useView(name: string): [string, (view: string) => void] { + const [view, setView] = useState(() => localStorage.getItem(name) || "board"); + + const updateView = (view: string) => { + setView(view); + localStorage.setItem(name, view); + }; + + useEffect(() => { + const handleStorageChange = () => { + setView(localStorage.getItem(name) || "board"); + }; + + window.addEventListener("storage", handleStorageChange); + + return () => { + window.removeEventListener("storage", handleStorageChange); + }; + }, [name]); + + return [view, updateView]; +} diff --git a/src/Utils/viewStorageUtils.ts b/src/Utils/viewStorageUtils.ts deleted file mode 100644 index 655ad6677cc..00000000000 --- a/src/Utils/viewStorageUtils.ts +++ /dev/null @@ -1,7 +0,0 @@ -export const getDefaultView = (key: string, defaultValue: string): string => { - return localStorage.getItem(key) || defaultValue; -}; - -export const setDefaultView = (key: string, value: string): void => { - localStorage.setItem(key, value); -}; diff --git a/src/components/Common/View.tsx b/src/components/Common/View.tsx new file mode 100644 index 00000000000..ac8ea05903b --- /dev/null +++ b/src/components/Common/View.tsx @@ -0,0 +1,27 @@ +import React, { ComponentType } from "react"; + +import { useView } from "@/Utils/useView"; + +export default function View({ + name, + board, + list, +}: { + name: string; + board: ComponentType<{ setView: (view: string) => void }>; + list: ComponentType<{ setView: (view: string) => void }>; +}) { + const [view, setView] = useView(`${name}DefaultView`); + + const views: Record< + "board" | "list", + ComponentType<{ setView: (view: string) => void }> + > = { + board, + list, + }; + + const SelectedView = views[view as keyof typeof views] || board; + + return ; +} diff --git a/src/components/Resource/ResourceBoard.tsx b/src/components/Resource/ResourceBoard.tsx index 300d61b89e4..877b64c7d91 100644 --- a/src/components/Resource/ResourceBoard.tsx +++ b/src/components/Resource/ResourceBoard.tsx @@ -25,7 +25,6 @@ import { RESOURCE_CHOICES } from "@/common/constants"; import routes from "@/Utils/request/api"; import request from "@/Utils/request/request"; -import { setDefaultView } from "@/Utils/viewStorageUtils"; const KanbanBoard = lazy( () => import("@/components/Kanban/Board"), @@ -36,7 +35,11 @@ const resourceStatusOptions = RESOURCE_CHOICES.map((obj) => obj.text); const COMPLETED = ["COMPLETED", "REJECTED"]; const ACTIVE = resourceStatusOptions.filter((o) => !COMPLETED.includes(o)); -export default function BoardView() { +export default function BoardView({ + setView, +}: { + setView: (view: string) => void; +}) { const { qParams, FilterBadges, advancedFilter, updateQuery } = useFilters({ limit: -1, cacheBlacklist: ["title"], @@ -46,11 +49,6 @@ export default function BoardView() { const appliedFilters = formatFilter(qParams); const { t } = useTranslation(); - const onListViewBtnClick = () => { - navigate("/resource/list", { query: qParams }); - setDefaultView("defaultResourceView", "list"); - }; - return (
@@ -96,7 +94,7 @@ export default function BoardView() {