refactoring user settings in the frontend

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-11-16 17:51:21 +03:00
committed by Daniel Hutzel
parent d78e759bf7
commit 4b4fe2dc3f
2 changed files with 55 additions and 44 deletions

View File

@@ -1,5 +1,6 @@
import React, { useMemo, createContext, useContext, useState } from "react"; import React, { useMemo, createContext, useContext, useState } from "react";
import axios from "axios"; import axios from "axios";
import { isEmpty, isArray } from "lodash";
const globalContext = { const globalContext = {
error: {}, error: {},
@@ -8,7 +9,6 @@ const globalContext = {
ID: undefined, ID: undefined,
roles: [], roles: [],
email: undefined, email: undefined,
level: undefined,
token: undefined, token: undefined,
}, },
locale: undefined, locale: undefined,
@@ -19,46 +19,57 @@ const GlobalContext = createContext(globalContext);
const useGlobals = () => useContext(GlobalContext); const useGlobals = () => useContext(GlobalContext);
const AVAILABLE_LOCALES = ["en", "fr", "de"]; const AVAILABLE_LOCALES = ["en", "fr", "de"];
const isValidUser = (user) => {
return (
!isEmpty(user) &&
user.ID &&
user.roles &&
user.email &&
user.token &&
isArray(user.roles)
);
};
const resetAxiosParams = () => {
delete axios.defaults.headers.common["Authorization"];
delete axios.defaults.userEntity;
axios.defaults.tracksEntity = "Tracks";
};
const setAxiosParams = (user) => {
axios.defaults.headers.common["Authorization"] = `Basic ${user.token}`;
axios.defaults.userID = user.ID;
if (user.roles.includes("customer")) {
axios.defaults.userEntity = `Customers/${user.ID}`;
axios.defaults.tracksEntity = "MarkedTracks";
} else {
axios.defaults.userEntity = `Employees/${user.ID}`;
axios.defaults.tracksEntity = "Tracks";
}
};
const useUserData = () => { const useUserData = () => {
const getUserDataFromLS = () => { const getUserDataFromLS = () => {
let userFromLS; let userFromLS;
try { try {
userFromLS = JSON.parse(localStorage.getItem("user")); userFromLS = JSON.parse(localStorage.getItem("user"));
} catch (e) {} } catch (e) {}
if (userFromLS) { if (isValidUser(userFromLS)) {
axios.defaults.headers.common[ setAxiosParams(userFromLS);
"Authorization" return userFromLS;
] = `Basic ${userFromLS.token}`; } else {
axios.defaults.userID = userFromLS.ID; localStorage.removeItem("user");
axios.defaults.userEntity = resetAxiosParams();
!!userFromLS && userFromLS.roles.includes("customer")
? `Customers/${userFromLS.ID}`
: `Employees/${userFromLS.ID}`;
} }
axios.defaults.tracksEntity =
!!userFromLS && userFromLS.roles.includes("customer")
? "MarkedTracks"
: "Tracks";
return userFromLS;
}; };
const setUserDataToLS = (value) => { const setUserDataToLS = (value) => {
if (!!value) { if (isValidUser(value)) {
localStorage.setItem("user", JSON.stringify(value)); localStorage.setItem("user", JSON.stringify(value));
axios.defaults.headers.common["Authorization"] = `Basic ${value.token}`; setAxiosParams(value);
axios.defaults.tracksEntity = value.roles.includes("customer")
? "MarkedTracks"
: "Tracks";
axios.defaults.userEntity =
!!value && value.roles.includes("customer")
? `Customers/${value.ID}`
: `Employees/${value.ID}`;
} else { } else {
localStorage.removeItem("user"); localStorage.removeItem("user");
delete axios.defaults.headers.common["Authorization"]; resetAxiosParams();
delete axios.defaults.userEntity;
axios.defaults.tracksEntity =
!!value && value.roles.includes("customer") ? "MarkedTracks" : "Tracks";
} }
}; };

View File

@@ -7,17 +7,17 @@ aspect Named {
aspect Person { aspect Person {
key ID : Integer; key ID : Integer;
lastName : String(20); lastName : String(20) default 'dummy';
firstName : String(40); firstName : String(40) default 'dummy';
city : String(40); city : String(40) default 'dummy';
state : String(40); state : String(40) default 'dummy';
address : String(70); address : String(70) default 'dummy';
country : String(40); country : String(40) default 'dummy';
postalCode : String(10); postalCode : String(10) default 123;
phone : String(24); phone : String(24) default 'dummy';
fax : String(24); fax : String(24) default 'dummy';
email : String(60); email : String(60) default 'dummy@email.com';
password : String(500); password : String(500) default 'dummy';
} }
entity MediaTypes : Named {} entity MediaTypes : Named {}
@@ -66,11 +66,11 @@ entity Invoices {
key ID : Integer; key ID : Integer;
customer : Association to Customers; customer : Association to Customers;
invoiceDate : DateTime; invoiceDate : DateTime;
billingAddress : String(70); billingAddress : String(70) default 'dummy';
billingCity : String(40); billingCity : String(40) default 'dummy';
billingState : String(40); billingState : String(40) default 'dummy';
billingCountry : String(40); billingCountry : String(40) default 'dummy';
billingPostalCode : String(40); billingPostalCode : String(40) default 123;
total : Decimal(10, 2); total : Decimal(10, 2);
invoiceItems : Composition of many InvoiceItems invoiceItems : Composition of many InvoiceItems
on invoiceItems.invoice = $self; on invoiceItems.invoice = $self;