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 #9485

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
51a053e
list and view card view
Mahendar0701 Dec 18, 2024
79fafe7
removed dediacted links for users view
Mahendar0701 Dec 18, 2024
fb3a446
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 18, 2024
8af90dd
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 18, 2024
71ac706
removed dediacted links for users view
Mahendar0701 Dec 18, 2024
db15f8b
changes
Mahendar0701 Dec 18, 2024
2430231
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 19, 2024
e0eb9d8
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 20, 2024
a939247
created view util
Mahendar0701 Dec 22, 2024
c7808ad
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 22, 2024
249be67
resolved conflicts
Mahendar0701 Dec 23, 2024
f785378
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 23, 2024
b14a320
clear view cache
Mahendar0701 Dec 26, 2024
ffa3559
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 26, 2024
d7f0669
Merge branch 'list-and-card-view-in-users-tab' of https://github.com/…
Mahendar0701 Dec 26, 2024
3a62c2f
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 27, 2024
609f723
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 28, 2024
0724ab4
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 29, 2024
1efe9f5
Merge branch 'develop' into list-and-card-view-in-users-tab
Mahendar0701 Dec 30, 2024
91765fd
updated view for facility users
Mahendar0701 Dec 30, 2024
1ac3800
Merge branch 'list-and-card-view-in-users-tab' of https://github.com/…
Mahendar0701 Dec 30, 2024
84b32c0
merge conflicts
Mahendar0701 Dec 31, 2024
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
8 changes: 4 additions & 4 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1444,8 +1444,8 @@
"qualification": "Qualification",
"qualification_required": "Qualification is required",
"quantity_approved": "Quantity Approved",
"quantity_required": "Quantity Required",
"quantity_requested": "Quantity Requested",
"quantity_required": "Quantity Required",
"raise_consent_request": "Raise a consent request to fetch patient records over ABDM",
"ration_card__APL": "APL",
"ration_card__BPL": "BPL",
Expand Down Expand Up @@ -1676,10 +1676,10 @@
"test_type": "Type of test done",
"tested_on": "Tested on",
"thank_you_for_choosing": "Thank you for choosing our care service",
"time": "Time",
"time_slot": "Time Slot",
"the_request_for_resources_placed_by_yourself_is": "The request for resource (details below) placed by yourself is",
"third_party_software_licenses": "Third Party Software Licenses",
"time": "Time",
"time_slot": "Time Slot",
"title_of_request": "Title of Request",
"titrate_dosage": "Titrate Dosage",
"to": "to",
Expand Down Expand Up @@ -1871,4 +1871,4 @@
"you_need_at_least_a_location_to_create_an_assest": "You need at least a location to create an assest.",
"zoom_in": "Zoom In",
"zoom_out": "Zoom Out"
}
}
10 changes: 2 additions & 8 deletions src/Routers/routes/ResourceRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +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";

const getDefaultView = () =>
localStorage.getItem("defaultResourceView") === "list" ? "list" : "board";

const ResourceRoutes: AppRoutes = {
"/resource": () => <Redirect to={`/resource/${getDefaultView()}`} />,
"/resource/board": () => <BoardView />,
"/resource/list": () => <ListView />,
"/resource": () => <View name="resource" board={BoardView} list={ListView} />,
"/resource/:id": ({ id }) => <ResourceDetails id={id} />,
"/resource/:id/update": ({ id }) => <ResourceDetailsUpdate id={id} />,
};
Expand Down
23 changes: 23 additions & 0 deletions src/Utils/ViewCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const getKey = (name: string) => {
return `${name}`;
};

/**
* Clears the view preference.
*/
const invalidate = (name: string) => {
localStorage.removeItem(getKey(name));
};

/**
* Clears all the view preferences.
*/
const invalidateAll = () => {
const viewKeys = ["users", "resource", "shifting"];
viewKeys.forEach(invalidate);
};

export default {
invalidate,
invalidateAll,
};
23 changes: 23 additions & 0 deletions src/Utils/useView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect, useState } from "react";

export function useView(name: string): [string, (view: string) => void] {
const [view, setView] = useState(() => {
return localStorage.getItem(name) || "board";
});
const updateView = (newView: string) => {
localStorage.setItem(name, newView);
setView(newView);
};
useEffect(() => {
const interval = setInterval(() => {
const storedView = localStorage.getItem(name);
if (storedView !== view) {
setView(storedView || "board");
}
}, 100);
return () => {
clearInterval(interval);
};
}, [name, view]);
return [view, updateView];
}
2 changes: 2 additions & 0 deletions src/components/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { CarePatientTokenKey } from "@/common/constants";

import FiltersCache from "@/Utils/FiltersCache";
import * as Notification from "@/Utils/Notifications";
import ViewCache from "@/Utils/ViewCache";
import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import request from "@/Utils/request/request";
Expand Down Expand Up @@ -254,6 +255,7 @@ const Login = (props: { forgot?: boolean }) => {

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
ViewCache.invalidateAll();
const validated = validateData();
if (!validated) return;

Expand Down
24 changes: 24 additions & 0 deletions src/components/Common/View.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { ComponentType } from "react";

import { useView } from "@/Utils/useView";

export default function View({
name,
board,
list,
}: {
name: "shifting" | "resource";
board: ComponentType;
list: ComponentType;
}) {
const [view] = useView(name);

const views: Record<"board" | "list", ComponentType> = {
board,
list,
};

const SelectedView = views[view as keyof typeof views] || board;

return <SelectedView />;
}
12 changes: 8 additions & 4 deletions src/components/Facility/FacilityUsers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import { useTranslation } from "react-i18next";

import CountBlock from "@/CAREUI/display/Count";
Expand All @@ -12,14 +11,15 @@ import useFilters from "@/hooks/useFilters";
import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { useView } from "@/Utils/useView";

export default function FacilityUsers(props: { facilityId: number }) {
const { t } = useTranslation();
const { qParams, updateQuery, Pagination } = useFilters({
limit: 18,
cacheBlacklist: ["username"],
});
const [activeTab, setActiveTab] = useState(0);
const [activeTab, setActiveTab] = useView("users");
const { facilityId } = props;

const { data: facilityData } = useTanStackQueryInstead(
Expand Down Expand Up @@ -47,6 +47,10 @@ export default function FacilityUsers(props: { facilityId: number }) {
return <div>No users found</div>;
}

const handleTabChange = (tab: number) => {
setActiveTab(tab === 1 ? "list" : "card");
};

return (
<Page
title={`${t("users")} - ${facilityData?.name}`}
Expand All @@ -65,8 +69,8 @@ export default function FacilityUsers(props: { facilityId: number }) {
users={userListData?.results ?? []}
onSearch={(username) => updateQuery({ username })}
searchValue={qParams.username}
activeTab={activeTab}
onTabChange={setActiveTab}
activeTab={activeTab === "list" ? 1 : 0}
onTabChange={handleTabChange}
/>

<Pagination totalCount={userListData.count} />
Expand Down
9 changes: 3 additions & 6 deletions src/components/Resource/ResourceBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { RESOURCE_CHOICES } from "@/common/constants";

import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import { useView } from "@/Utils/useView";
import { ResourceRequest } from "@/types/resourceRequest/resourceRequest";

const KanbanBoard = lazy(
Expand All @@ -36,6 +37,7 @@ const COMPLETED = ["COMPLETED", "REJECTED"];
const ACTIVE = resourceStatusOptions.filter((o) => !COMPLETED.includes(o));

export default function BoardView() {
const [, setView] = useView("resource");
const { qParams, FilterBadges, advancedFilter, updateQuery } = useFilters({
limit: -1,
cacheBlacklist: ["title"],
Expand All @@ -45,11 +47,6 @@ export default function BoardView() {
const appliedFilters = formatFilter(qParams);
const { t } = useTranslation();

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

return (
<div className="flex-col px-2 pb-2">
<div className="flex w-full flex-col items-center justify-between lg:flex-row">
Expand Down Expand Up @@ -97,7 +94,7 @@ export default function BoardView() {
<div className="flex w-full flex-col gap-2 lg:mr-4 lg:w-fit lg:flex-row lg:gap-4">
<Button
variant={"primary"}
onClick={onListViewBtnClick}
onClick={() => setView("list")}
className="h-10.8 px-4 py-2"
>
<CareIcon icon="l-list-ul" className="mr-2" />
Expand Down
10 changes: 4 additions & 6 deletions src/components/Resource/ResourceList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link, navigate } from "raviger";
import { Link } from "raviger";
import { useTranslation } from "react-i18next";

import Chip from "@/CAREUI/display/Chip";
Expand All @@ -20,10 +20,12 @@ import useFilters from "@/hooks/useFilters";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { useView } from "@/Utils/useView";
import { formatDateTime } from "@/Utils/utils";
import { ResourceRequest } from "@/types/resourceRequest/resourceRequest";

export default function ListView() {
const [, setView] = useView("resource");
const {
qParams,
Pagination,
Expand All @@ -35,10 +37,6 @@ export default function ListView() {

const { t } = useTranslation();

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

const { loading, data, refetch } = useTanStackQueryInstead(
Expand Down Expand Up @@ -218,7 +216,7 @@ export default function ListView() {
</div>

<div className="mt-2 flex w-full flex-col gap-2 lg:w-fit lg:flex-row lg:gap-4">
<Button variant={"primary"} onClick={onBoardViewBtnClick}>
<Button variant={"primary"} onClick={() => setView("board")}>
<CareIcon icon="l-list-ul" className="rotate-90 mr-2" />
{t("board_view")}
</Button>
Expand Down
12 changes: 8 additions & 4 deletions src/components/Users/ManageUsers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { navigate } from "raviger";
import { useEffect, useState } from "react";
import { useEffect } from "react";
import { useTranslation } from "react-i18next";

import CountBlock from "@/CAREUI/display/Count";
Expand All @@ -19,6 +19,7 @@ import { USER_TYPES } from "@/common/constants";

import routes from "@/Utils/request/api";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { useView } from "@/Utils/useView";
Mahendar0701 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

View preferences should be cleared during logout

The useView hook stores view preferences in localStorage, but these preferences are not cleared during logout. While the auth-related tokens are properly cleared in the signOut function (AuthUserProvider.tsx), the view preferences persist across sessions. To maintain proper session isolation:

  • Add view preference clearing to the signOut function in AuthUserProvider.tsx
  • Consider using the existing ViewCache.ts utility to manage view-related storage operations

Example locations that need attention:

  • src/Providers/AuthUserProvider.tsx: Add view preference clearing to the signOut function
  • src/Utils/ViewCache.ts: Consider adding a clearAll utility if not already present
🔗 Analysis chain

Verify view retention behavior across user sessions

The implementation stores view preferences in localStorage. To prevent view preferences from persisting across different user sessions, ensure the storage is cleared on logout.

Also applies to: 43-43


Let me search for the implementation of the useView hook to understand how it handles storage.


Let me check how the auth state and logout is handled to see if localStorage is properly cleared.


Let me check if there's a utility to clear view preferences and if it's being used during logout.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for logout handling to verify storage clearing
rg -A 5 "logout|signout|clearStorage|clear\(" 

Length of output: 18775


Script:

#!/bin/bash
# Search for useView implementation
ast-grep --pattern 'export function useView($$$) {
  $$$
}'

# Also search for any references to localStorage in the hook
rg -A 5 "localStorage" src/Utils/useView.ts

Length of output: 500


Script:

#!/bin/bash
# Search for auth-related storage handling
rg -A 10 "signOut|logout" src/Providers/AuthUserProvider.tsx

# Search for any localStorage clear operations
rg -A 5 "localStorage\.clear|removeItem" src/

Length of output: 4325


Script:

#!/bin/bash
# Search for view cache clearing utilities
rg -A 5 "clearAll|clearView" src/Utils/ViewCache.ts

# Search for any usage of view cache clearing in auth-related files
rg -l "ViewCache" src/Providers/AuthUserProvider.tsx src/hooks/usePatientSignOut.ts

Length of output: 137


export default function ManageUsers() {
const { t } = useTranslation();
Expand All @@ -39,7 +40,7 @@ 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] = useView("users");

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

manageUsers = (
<div>
<UserListView
users={userListData?.results ?? []}
onSearch={(username) => updateQuery({ username })}
searchValue={qParams.username}
activeTab={activeTab}
onTabChange={setActiveTab}
activeTab={activeTab === "list" ? 1 : 0}
onTabChange={handleTabChange}
/>
<Pagination totalCount={userListData.count} />
</div>
Expand Down
Loading