added last online and unauthorized status handling

main
alex 2023-06-11 20:23:36 +02:00
parent 027713fedd
commit 5ab2dfa0f1
6 changed files with 31 additions and 12 deletions

View File

@ -746,7 +746,6 @@ function InputRequiredHandler({
/>
</Form.Item>
);
break;
default:
notificationApi["error"]({
message: `Type ${groupTaskParameter.type} not implemented`,

View File

@ -1,7 +1,7 @@
import { LockOutlined, LoginOutlined, UserOutlined } from "@ant-design/icons";
import { Button, Form, Input, Modal, notification } from "antd";
import PropTypes from "prop-types";
import { Constants } from "../../utils";
import { Constants, setUserSessionToLocalStorage } from "../../utils";
import { useState } from "react";
import { Buffer } from "buffer";
@ -44,7 +44,7 @@ export default function Login({ setUserSession }) {
return Promise.reject(res.status);
})
.then((data) => {
setUserSession(data.Session);
setUserSessionToLocalStorage(data.Session);
window.location.href = "/";
})
.catch((err) => {

View File

@ -5,6 +5,7 @@ import {
WebSocketContext,
getUserId,
getUserSessionFromLocalStorage,
handleUnauthorizedStatus,
} from "../../utils";
import { useContext } from "react";
import { Link } from "react-router-dom";
@ -59,7 +60,9 @@ const columns = [
"Content-Type": "application/json",
"X-Authorization": getUserSessionFromLocalStorage(),
},
}).catch((err) => console.error(err));
})
.then((res) => handleUnauthorizedStatus(res.status))
.catch((err) => console.error(err));
}}
>
<Link to="#">Use scanner</Link>

View File

@ -7,6 +7,7 @@ import {
WebSocketContext,
getConnectionStatusItem,
getUserSessionFromLocalStorage,
handleUnauthorizedStatus,
} from "../../utils";
import { Link } from "react-router-dom";
@ -47,7 +48,9 @@ const columns = [
"Content-Type": "application/json",
"X-Authorization": getUserSessionFromLocalStorage(),
},
}).catch((err) => {
})
.then((res) => handleUnauthorizedStatus(res.status))
.catch((err) => {
console.error(err);
});
}}

View File

@ -1,5 +1,6 @@
import { Popover, Table } from "antd";
import {
FormatDatetime,
MyAvatar,
WebSocketContext,
getConnectionStatusItem,
@ -55,6 +56,7 @@ export default function Users() {
),
connectionStatus: getConnectionStatusItem(user.ConnectionStatus),
username: user.Username,
lastOnline: FormatDatetime(user.LastOnline),
});
});

View File

@ -62,7 +62,7 @@ export function UseUserSession() {
if (session === undefined) {
localStorage.removeItem("session");
} else {
localStorage.setItem("session", JSON.stringify(session));
setUserSessionToLocalStorage(session);
}
};
@ -73,7 +73,18 @@ export function UseUserSession() {
}
export function getUserSessionFromLocalStorage() {
return JSON.parse(localStorage.getItem("session"));
return localStorage.getItem("session");
}
export function setUserSessionToLocalStorage(session) {
localStorage.setItem("session", session);
}
export function handleUnauthorizedStatus(status) {
if (status === 401) {
setUserSessionToLocalStorage("");
window.location.href = "/";
}
}
/**
@ -187,9 +198,10 @@ export function WebSocketProvider({
setAllUsers((arr) => {
const newArr = [...arr];
newArr[
arr.findIndex((arr1) => arr1.Id === body.UserId)
].ConnectionStatus = body.ConnectionStatus;
const arrIndex = arr.findIndex((arr1) => arr1.Id === body.UserId);
newArr[arrIndex].ConnectionStatus = body.ConnectionStatus;
newArr[arrIndex].LastOnline = body.LastOnline;
return newArr;
});