Skip to content

Commit

Permalink
add renew tokens feature and fix duplicate lines in tables
Browse files Browse the repository at this point in the history
  • Loading branch information
RenauxLeaInsee committed Jul 16, 2024
1 parent 973b85c commit fcb06d6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sonor",
"version": "0.5.34",
"version": "0.5.35",
"private": true,
"dependencies": {
"@tanstack/react-query": "4.0.5",
Expand Down
4 changes: 2 additions & 2 deletions src/Authentication/useAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function initializeOidc (config) {
* Retrieve authentication status based of Oidc
*/
export function useIsAuthenticated() {
const { login, isUserLoggedIn, oidcTokens } = useOidc({ assertUserLoggedIn: false });
const { login, isUserLoggedIn, oidcTokens, renewTokens} = useOidc({ assertUserLoggedIn: false });

useEffect(() => {
if (!login) {
Expand All @@ -47,5 +47,5 @@ export function useIsAuthenticated() {
});
}, [login]);

return { isAuthenticated: isUserLoggedIn, tokens: oidcTokens };
return { isAuthenticated: isUserLoggedIn, tokens: oidcTokens, renewTokens };
}
37 changes: 35 additions & 2 deletions src/components/App/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useRef } from "react";
import { useIsAuthenticated } from "../../Authentication/useAuth";
import D from "../../i18n";
import View from "../View/View";
Expand All @@ -9,8 +9,41 @@ export const App = () => {
const [authenticated, setAuthenticated] = useState(false);
const [contactFailed, setContactFailed] = useState(false);
const [data, setData] = useState(null);
const timeoutIdRef = useRef(null);

const { tokens } = useIsAuthenticated();
const { tokens, renewTokens } = useIsAuthenticated();

useEffect(() => {
const resetInactivityTimeout = () => {
if (timeoutIdRef.current) {
clearTimeout(timeoutIdRef.current);
}
timeoutIdRef.current = setTimeout(renewTokens, 5 * 60 * 1000);
};

const events = [
"mousemove",
"mousedown",
"keypress",
"touchstart",
"click",
];

events.forEach((event) => {
window.addEventListener(event, resetInactivityTimeout);
});

resetInactivityTimeout();

return () => {
if (timeoutIdRef.current) {
clearTimeout(timeoutIdRef.current);
}
events.forEach((event) => {
window.removeEventListener(event, resetInactivityTimeout);
});
};
}, [renewTokens]);

useEffect(() => {
if (window.localStorage.getItem("AUTHENTICATION_MODE") === ANONYMOUS) {
Expand Down
6 changes: 2 additions & 4 deletions src/components/CollectionTable/CollectionTableDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,8 @@ function CollectionTableDisplay({
(pagination.page - 1) * pagination.size,
Math.min(pagination.page * pagination.size, displayedLines.length),
)
.map((line) => (
<CollectionTableDisplayLine
key={line.id || line.interviewerId || line.survey || line.site}
data={line}
.map((line, index) => (
<CollectionTableDisplayLine key={index} data={line}
/>
))}
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions src/components/MonitoringTable/FollowUpTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ function FollowUpTable({
(pagination.page - 1) * pagination.size,
Math.min(pagination.page * pagination.size, displayedLines.length),
)
.map((line) => (
<FollowUpTableLine key={line.interviewerId || line.survey || line.site} data={line} />
.map((line, index) => (
<FollowUpTableLine key={index} data={line} />
))}
</tbody>
{tableFooter}
Expand Down

0 comments on commit fcb06d6

Please sign in to comment.