Skip to content

Commit

Permalink
fix: admin name disappear when close browser or windows (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnWang86b authored Dec 20, 2022
1 parent f82e5d4 commit ab08ddb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/components/SideBar/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import useAuthRequest from "@/components/Login/helpers/useAuthRequest";
import { Text, Flex, IconButton, Tooltip } from "@chakra-ui/react";
import { TbLogout } from "react-icons/tb";
import { useSelector } from "react-redux";
import type { RootState } from "../../store";

interface Props {
sidebarExpand: boolean;
}

const Profile = ({ sidebarExpand }: Props) => {
const { logoutRequest } = useAuthRequest();
const adminLoginResponseData = useSelector((state: RootState) => state.currentAdmin);
const adminLoginResponseData = JSON.parse(localStorage.getItem("adminInfo") || "{}");

return (
<Flex
paddingTop="20px"
Expand All @@ -20,7 +19,7 @@ const Profile = ({ sidebarExpand }: Props) => {
>
{sidebarExpand && (
<Text fontWeight="500" color="#FFFFFF">
{adminLoginResponseData.currentAdmin?.name}
{adminLoginResponseData?.name}
</Text>
)}

Expand Down
10 changes: 4 additions & 6 deletions src/pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import { IAdmin } from "@/interfaces/adminData";
import DropDownFilter from "@/components/Admin/DropDownFilter";
import { ADMIN_TABLE_HEADER } from "../../constants/tableHeaders";
import TableHeader from "../../components/TableHeader";
import { useSelector } from "react-redux";
import type { RootState } from "../../store";

interface FilterType {
isActive: boolean;
Expand All @@ -47,9 +45,9 @@ const AdminAccounts = () => {
const { isOpen: isModalOpen, onOpen: onModalOpen, onClose: onModalClose } = useDisclosure();
const [filterValue, setFilterValue] = useState<FilterType>();

const adminLoginResponseData = useSelector((state: RootState) => state.currentAdmin);
const superAdmin = adminLoginResponseData.currentAdmin?.permission === "super";
const currentAdminId = adminLoginResponseData.currentAdmin?.id;
const adminLoginResponseData = JSON.parse(localStorage.getItem("adminInfo") || "{}");
const superAdmin = adminLoginResponseData?.permission === "super";
const currentAdminId = adminLoginResponseData?.id;

const confirmDeleteAdmin = (id: string) => {
deleteAdmin(id);
Expand Down Expand Up @@ -197,7 +195,7 @@ const AdminAccounts = () => {
isOpen={isModalOpen}
onClose={onModalClose}
onConfirm={() => confirmDeleteAdmin(adminIdToDelete)}
shownText={"Are you sure you want to restore this admin?"}
shownText={"Are you sure you want to delete this admin?"}
/>
)}
{currentModal === "Restore" && (
Expand Down
4 changes: 2 additions & 2 deletions src/store/reducer/currentAdminSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export const currentAdminSlice = createSlice({
reducers: {
setCurrentAdmin: (state, action: PayloadAction<currentAdmin>) => {
state.currentAdmin = action.payload;
console.log("setCurrentAdmin", state.currentAdmin);
localStorage.setItem("adminInfo", JSON.stringify(state.currentAdmin));
},
clearCurrentAdmin: (state) => {
state.currentAdmin = null;
console.log("clearCurrentAdmin", state.currentAdmin);
localStorage.removeItem("adminInfo");
},
},
});
Expand Down

0 comments on commit ab08ddb

Please sign in to comment.