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 5 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
10 changes: 6 additions & 4 deletions src/Routers/routes/ShiftingRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ 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("defaultShiftingView", "board")}`}
/>
),
"/shifting/board": () => <BoardView />,
"/shifting/list": () => <ListView />,
Copy link
Member

Choose a reason for hiding this comment

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

yup, let's remove these routes too and instead we could do:

(which uses that hook behind the scenes)

"/shifting": () => <View 
  name="shifting"
  board={BoardView}
  list={ListView}
  />

"/shifting/:id": ({ id }) => <ShiftDetails id={id} />,
Expand Down
5 changes: 4 additions & 1 deletion src/Routers/routes/UserRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import UserHome from "@/components/Users/UserHome";
import UserProfile from "@/components/Users/UserProfile";

import { AppRoutes } from "@/Routers/AppRouter";
import { getDefaultView } from "@/Utils/viewStorageUtils";

const UserRoutes: AppRoutes = {
"/users": () => <ManageUsers />,
"/users": () => (
<ManageUsers defaultView={getDefaultView("usersDefaultView", "card")} />
),
"/users/add": () => <UserAdd />,
"/users/:username": ({ username }) => (
<UserHome username={username} tab={"profile"} />
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");

12 changes: 9 additions & 3 deletions src/components/Users/ManageUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { classNames } from "@/Utils/utils";
import { setDefaultView } from "@/Utils/viewStorageUtils";

export default function ManageUsers() {
export default function ManageUsers({ defaultView }: { defaultView: string }) {
const { t } = useTranslation();
const {
qParams,
Expand All @@ -47,7 +48,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] = useState(defaultView === "list" ? 1 : 0);

const { data: homeFacilityData } = useTanStackQueryInstead(
routes.getAnyFacility,
Expand Down Expand Up @@ -105,6 +106,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 +119,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