Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : View Retention in Users Tab After Navigation and Logout #9485

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Routers/routes/ResourceRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { ResourceDetailsUpdate } from "@/components/Resource/ResourceDetailsUpda
import ListView from "@/components/Resource/ResourceList";

import { AppRoutes } from "@/Routers/AppRouter";

const getDefaultView = () =>
localStorage.getItem("defaultResourceView") === "list" ? "list" : "board";
import { getDefaultView } from "@/Utils/viewStorageUtils";

const ResourceRoutes: AppRoutes = {
"/resource": () => <Redirect to={`/resource/${getDefaultView()}`} />,
"/resource": () => (
<Redirect
to={`/resource/${getDefaultView("defaultResourceView", "board")}`}
/>
),
Mahendar0701 marked this conversation as resolved.
Show resolved Hide resolved
"/resource/board": () => <BoardView />,
"/resource/list": () => <ListView />,
"/resource/:id": ({ id }) => <ResourceDetails id={id} />,
Expand Down
8 changes: 4 additions & 4 deletions src/Routers/routes/ShiftingRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import BoardView from "@/components/Shifting/ShiftingBoard";
import ListView from "@/components/Shifting/ShiftingList";

import { AppRoutes } from "@/Routers/AppRouter";

const getDefaultView = () =>
localStorage.getItem("defaultShiftView") === "list" ? "list" : "board";
import { getDefaultView } from "@/Utils/viewStorageUtils";

const ShiftingRoutes: AppRoutes = {
"/shifting": () => <Redirect to={`/shifting/${getDefaultView()}`} />,
"/shifting": () => (
<Redirect to={`/shifting/${getDefaultView("defaultShiftView", "board")}`} />
),
"/shifting/board": () => <BoardView />,
"/shifting/list": () => <ListView />,
"/shifting/:id": ({ id }) => <ShiftDetails id={id} />,
Expand Down
7 changes: 7 additions & 0 deletions src/Utils/viewStorageUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getDefaultView = (key: string, defaultValue: string): string => {
return localStorage.getItem(key) || defaultValue;
};
Mahendar0701 marked this conversation as resolved.
Show resolved Hide resolved

export const setDefaultView = (key: string, value: string): void => {
localStorage.setItem(key, value);
};
Mahendar0701 marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +1 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant something like:

const [view, setView] = useView("shifting");

3 changes: 2 additions & 1 deletion src/components/Resource/ResourceBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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"),
Expand All @@ -47,7 +48,7 @@ export default function BoardView() {

const onListViewBtnClick = () => {
navigate("/resource/list", { query: qParams });
localStorage.setItem("defaultResourceView", "list");
setDefaultView("defaultResourceView", "list");
};

return (
Expand Down
3 changes: 2 additions & 1 deletion src/components/Resource/ResourceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { formatDateTime } from "@/Utils/utils";
import { setDefaultView } from "@/Utils/viewStorageUtils";

export default function ListView() {
const {
Expand All @@ -37,7 +38,7 @@ export default function ListView() {

const onBoardViewBtnClick = () => {
navigate("/resource/board", { query: qParams });
localStorage.setItem("defaultResourceView", "board");
setDefaultView("defaultResourceView", "board");
};
const appliedFilters = formatFilter(qParams);

Expand Down
3 changes: 2 additions & 1 deletion src/components/Shifting/ShiftingBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {

import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import { setDefaultView } from "@/Utils/viewStorageUtils";

const KanbanBoard = lazy(
() => import("@/components/Kanban/Board"),
Expand Down Expand Up @@ -78,7 +79,7 @@ export default function BoardView() {
const { t } = useTranslation();
const onListViewBtnClick = () => {
navigate("/shifting/list", { query: qParams });
localStorage.setItem("defaultShiftView", "list");
setDefaultView("defaultShiftView", "list");
};

return (
Expand Down
3 changes: 2 additions & 1 deletion src/components/Shifting/ShiftingList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import useFilters from "@/hooks/useFilters";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { setDefaultView } from "@/Utils/viewStorageUtils";

import ShiftingTable from "./ShiftingTable";

Expand All @@ -35,7 +36,7 @@ export default function ListView() {
const { t } = useTranslation();
const onBoardViewBtnClick = () => {
navigate("/shifting/board", { query: qParams });
localStorage.setItem("defaultShiftView", "board");
setDefaultView("defaultShiftView", "board");
};
const {
data: shiftData,
Expand Down
12 changes: 10 additions & 2 deletions src/components/Users/ManageUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { classNames } from "@/Utils/utils";
import { getDefaultView, setDefaultView } from "@/Utils/viewStorageUtils";

export default function ManageUsers() {
const { t } = useTranslation();
Expand All @@ -47,7 +48,9 @@ export default function ManageUsers() {
const userTypes = authUser.is_superuser
? [...USER_TYPES]
: USER_TYPES.slice(0, userIndex + 1);
const [activeTab, setActiveTab] = useState(0);
const [activeTab, setActiveTab] = useState(
getDefaultView("usersDefaultView", "card") === "list" ? 1 : 0,
);

const { data: homeFacilityData } = useTanStackQueryInstead(
routes.getAnyFacility,
Expand Down Expand Up @@ -105,6 +108,11 @@ export default function ManageUsers() {
if (userListLoading || districtDataLoading || !userListData?.results) {
return <Loading />;
}
const handleTabChange = (tab: number) => {
setActiveTab(tab);
const newView = tab === 1 ? "list" : "card";
setDefaultView("usersDefaultView", newView);
};

manageUsers = (
<div>
Expand All @@ -113,7 +121,7 @@ export default function ManageUsers() {
onSearch={(username) => updateQuery({ username })}
searchValue={qParams.username}
activeTab={activeTab}
onTabChange={setActiveTab}
onTabChange={handleTabChange}
/>
<Pagination totalCount={userListData.count} />
</div>
Expand Down
Loading