hooks refactoring

master
Tudor Stanciu 2023-02-12 01:18:56 +02:00
parent 627e2105f8
commit 20a1fcb7dd
4 changed files with 39 additions and 28 deletions

3
src/hooks/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from "./useTuitioClient";
export * from "./useTuitioToken";
export * from "./useTuitioUser";

View File

@ -1,5 +1,5 @@
import { useContext } from "react";
import { TuitioContext, TuitioDispatchContext } from "./contexts";
import { TuitioContext, TuitioDispatchContext } from "../contexts";
import { TuitioClient, invalidate } from "@flare/tuitio-client";
import type { TuitioAuthenticationResult } from "@flare/tuitio-client";
@ -40,30 +40,4 @@ const useTuitioClient = (options: TuitioClientHookOptions) => {
return { login, logout };
};
const useTuitioUser = () => {
const state = useContext(TuitioContext);
const userName = state.userName;
const lastLoginDate = state.token?.validFrom;
return { userName, lastLoginDate };
};
const useTuitioToken = () => {
const state = useContext(TuitioContext);
const token = state.token;
const validate = (): boolean => {
const token = state.token;
if (!token) {
return false;
}
const valid = new Date(token.validUntil) >= new Date();
return valid;
};
const valid: boolean = validate();
return { token, validate, valid };
};
export { useTuitioClient, useTuitioUser, useTuitioToken };
export { useTuitioClient };

View File

@ -0,0 +1,23 @@
import { useContext } from "react";
import { TuitioContext } from "../contexts";
const useTuitioToken = () => {
const state = useContext(TuitioContext);
const token = state.token;
const validate = (): boolean => {
const token = state.token;
if (!token) {
return false;
}
const valid = new Date(token.validUntil) >= new Date();
return valid;
};
const valid: boolean = validate();
return { token, validate, valid };
};
export { useTuitioToken };

View File

@ -0,0 +1,11 @@
import { useContext } from "react";
import { TuitioContext } from "../contexts";
const useTuitioUser = () => {
const state = useContext(TuitioContext);
const userName = state.userName;
const lastLoginDate = state.token?.validFrom;
return { userName, lastLoginDate };
};
export { useTuitioUser };