merge refactor/remove-user-cookies
This commit is contained in:
parent
d9e825e1c1
commit
dd2f404eb4
@ -14,7 +14,6 @@
|
||||
"axios": "^0.21.1",
|
||||
"craco-less": "^1.17.1",
|
||||
"graphql": "^15.5.0",
|
||||
"js-cookie": "^2.2.1",
|
||||
"lodash": "^4.17.21",
|
||||
"luxon": "^1.26.0",
|
||||
"mobx": "^6.1.8",
|
||||
|
||||
27
src/client/Containers/Calculation/lib/fetchData/getUser.js
Normal file
27
src/client/Containers/Calculation/lib/fetchData/getUser.js
Normal file
@ -0,0 +1,27 @@
|
||||
import UserStore from 'client/stores/UserStore';
|
||||
import UserService from 'core/services/UserService';
|
||||
|
||||
function getUserFromLocalStorage() {
|
||||
const user = ['username', 'domain'].reduce((acc, propName) => {
|
||||
let prop = localStorage.getItem(propName);
|
||||
if (!prop) {
|
||||
prop = prompt('Enter ' + propName);
|
||||
localStorage.setItem(propName, prop);
|
||||
}
|
||||
acc[propName] = prop;
|
||||
return acc;
|
||||
}, {});
|
||||
return user;
|
||||
}
|
||||
|
||||
export default () =>
|
||||
new Promise(async resolve => {
|
||||
let user;
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
user = getUserFromLocalStorage();
|
||||
} else {
|
||||
user = await UserService.fetchUser();
|
||||
}
|
||||
UserStore.setUser(user);
|
||||
resolve();
|
||||
});
|
||||
@ -1,6 +1,7 @@
|
||||
import CrmService from 'core/services/CrmService';
|
||||
import CalculationStore from 'client/stores/CalculationStore';
|
||||
import { getUser } from 'core/tools/user';
|
||||
import UserStore from 'client/stores/UserStore';
|
||||
import CrmService from 'core/services/CrmService';
|
||||
import getUser from './getUser';
|
||||
import insuranceQuery from './queries/insuranceQuery';
|
||||
import optionsQuery from './queries/optionsQuery';
|
||||
import initialOwnerQuery from './queries/ownerQuery';
|
||||
@ -8,8 +9,9 @@ import staticDataQuery from './queries/staticDataQuery';
|
||||
import systemUserQuery from './queries/systemUserQuery';
|
||||
|
||||
export default () =>
|
||||
new Promise((resolve, reject) => {
|
||||
const { domainname } = getUser();
|
||||
new Promise(async (resolve, reject) => {
|
||||
await getUser();
|
||||
const domainname = UserStore.getDomainName();
|
||||
|
||||
Promise.all([
|
||||
CrmService.crmgqlquery({
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
import Cookies from 'js-cookie';
|
||||
import styled from 'styled-components';
|
||||
import mq from 'client/UIKit/mq';
|
||||
import { Flex } from 'client/UIKit/grid';
|
||||
import { useStores } from 'client/hooks/useStores';
|
||||
import colors from 'client/UIKit/colors';
|
||||
import { Flex } from 'client/UIKit/grid';
|
||||
import mq from 'client/UIKit/mq';
|
||||
import { observer } from 'mobx-react-lite';
|
||||
import styled from 'styled-components';
|
||||
|
||||
const UserText = styled.span`
|
||||
margin: 0;
|
||||
@ -17,10 +18,17 @@ const UserText = styled.span`
|
||||
`}
|
||||
`;
|
||||
|
||||
const User = () => {
|
||||
const displayname = Cookies.get('displayname');
|
||||
return <UserText>{displayname}</UserText>;
|
||||
};
|
||||
const User = observer(() => {
|
||||
const { userStore } = useStores();
|
||||
let userInfo;
|
||||
if (userStore.user) {
|
||||
userInfo =
|
||||
process.env.NODE_ENV === 'development'
|
||||
? userStore.getDomainName()
|
||||
: userStore.user.displayName;
|
||||
}
|
||||
return <UserText>{userInfo}</UserText>;
|
||||
});
|
||||
|
||||
const Logout = styled.a`
|
||||
margin: 0;
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import { gql } from '@apollo/client';
|
||||
import { message } from 'antd';
|
||||
import { openNotification } from 'client/Elements/Notification';
|
||||
import CrmService from 'core/services/CrmService';
|
||||
import { getUser } from 'core/tools/user';
|
||||
import UserStore from 'client/stores/UserStore';
|
||||
import { CRM_PROXY_URL } from 'core/constants/urls';
|
||||
import CrmService from 'core/services/CrmService';
|
||||
import { toJS } from 'mobx';
|
||||
import CalculationStore, { calculationUrls } from '../..';
|
||||
import customValues from '../lib/customValues';
|
||||
@ -35,7 +35,7 @@ export default async () => {
|
||||
preparedPayments,
|
||||
} = calculationRes;
|
||||
|
||||
const { domainname } = getUser();
|
||||
const domainname = UserStore.getDomainName();
|
||||
// if (process.env.NODE_ENV === 'development') {
|
||||
console.log('domainname', domainname);
|
||||
console.log('values', values);
|
||||
|
||||
14
src/client/stores/UserStore/index.js
Normal file
14
src/client/stores/UserStore/index.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { makeAutoObservable } from 'mobx';
|
||||
|
||||
const UserStore = makeAutoObservable({
|
||||
user: undefined,
|
||||
setUser(user) {
|
||||
this.user = user;
|
||||
},
|
||||
getDomainName() {
|
||||
const { username, domain } = this.user;
|
||||
return `${domain}\\${username}`;
|
||||
},
|
||||
});
|
||||
|
||||
export default UserStore;
|
||||
@ -2,12 +2,14 @@ import CalculationStore, {
|
||||
calculationProcess,
|
||||
calculationUrls,
|
||||
} from './CalculationStore';
|
||||
import UserStore from './UserStore';
|
||||
|
||||
class RootStore {
|
||||
constructor() {
|
||||
this.calculationStore = CalculationStore;
|
||||
this.calculationProcess = calculationProcess;
|
||||
this.calculationUrls = calculationUrls;
|
||||
this.userStore = UserStore;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,2 +1,3 @@
|
||||
export const CRM_PROXY_URL = '/crmgraphql';
|
||||
export const CORE_PROXY_URL = '/core';
|
||||
export const AUTH_PROXY_URL = '/auth-srv';
|
||||
|
||||
16
src/core/services/UserService/index.js
Normal file
16
src/core/services/UserService/index.js
Normal file
@ -0,0 +1,16 @@
|
||||
import axios from 'axios';
|
||||
import { AUTH_PROXY_URL } from 'core/constants/urls';
|
||||
|
||||
export default class {
|
||||
static fetchUser = () =>
|
||||
new Promise((resolve, reject) => {
|
||||
axios
|
||||
.get(String.prototype.concat(AUTH_PROXY_URL, '/user'))
|
||||
.then(res => {
|
||||
resolve(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
import { User } from 'core/types/user';
|
||||
import Cookies from 'js-cookie';
|
||||
|
||||
export const getUser = (): User => {
|
||||
let { username, domain } = Cookies.get();
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
if (!username || !domain) {
|
||||
username = prompt('Enter username');
|
||||
domain = prompt('Enter domain');
|
||||
if (username && domain) {
|
||||
Cookies.set('username', username);
|
||||
Cookies.set('domain', domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
const user: User = {
|
||||
username,
|
||||
domain,
|
||||
domainname: `${domain}\\${username}`,
|
||||
};
|
||||
return user;
|
||||
};
|
||||
Reference in New Issue
Block a user