Web.Client: confirmRoute: refactoring

This commit is contained in:
Daniil Senkiv 2019-10-02 11:31:50 +03:00
parent 3d9945b767
commit 188443f49f
4 changed files with 86 additions and 46 deletions

View File

@ -1802,14 +1802,15 @@ asap@~2.0.6:
integrity sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=
"asc-web-components@file:../../../packages/asc-web-components":
version "1.0.87"
version "1.0.102"
dependencies:
moment "^2.24.0"
prop-types "^15.7.2"
rc-tree "^2.1.2"
react-autosize-textarea "^7.0.0"
react-avatar-edit "^0.8.3"
react-avatar-editor "^11.0.7"
react-custom-scrollbars "^4.2.1"
react-dropzone "^10.1.8"
react-text-mask "^5.4.3"
react-toastify "^5.3.2"
react-virtualized-auto-sizer "^1.0.2"
@ -1897,6 +1898,13 @@ atob@^2.1.1:
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==
attr-accept@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/attr-accept/-/attr-accept-1.1.3.tgz#48230c79f93790ef2775fcec4f0db0f5db41ca52"
integrity sha512-iT40nudw8zmCweivz6j58g+RT33I4KbaIvRUhjNmDwO2WmsQUxFEZZYZ5w3vXe5x5MX9D7mfvA/XaLOZYFR9EQ==
dependencies:
core-js "^2.5.0"
autoprefixer@^9.6.1:
version "9.6.1"
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-9.6.1.tgz#51967a02d2d2300bb01866c1611ec8348d355a47"
@ -2968,7 +2976,7 @@ core-js@3.1.4:
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.1.4.tgz#3a2837fc48e582e1ae25907afcd6cf03b0cc7a07"
integrity sha512-YNZN8lt82XIMLnLirj9MhKDFZHalwzzrL9YLt6eb0T5D0EDl4IQ90IGkua8mHbnxNrkj1d8hbdizMc0Qmg1WnQ==
core-js@^2.4.0, core-js@^2.6.4:
core-js@^2.4.0, core-js@^2.5.0, core-js@^2.6.4:
version "2.6.9"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.9.tgz#6b4b214620c834152e179323727fc19741b084f2"
integrity sha512-HOpZf6eXmnl7la+cUdMnLvUxKNqLUzJvgIziQ0DiF3JwSImNphIqdGqzj6hIKyX04MmV0poclQ7+wjWvxQyR2A==
@ -4320,6 +4328,13 @@ file-loader@3.0.1:
loader-utils "^1.0.2"
schema-utils "^1.0.0"
file-selector@^0.1.11:
version "0.1.12"
resolved "https://registry.yarnpkg.com/file-selector/-/file-selector-0.1.12.tgz#fe726547be219a787a9dcc640575a04a032b1fd0"
integrity sha512-Kx7RTzxyQipHuiqyZGf+Nz4vY9R1XGxuQl/hLoJwq+J4avk/9wxxgZyHKtbyIPJmbD4A66DWGYfyykWNpcYutQ==
dependencies:
tslib "^1.9.0"
filesize@3.6.1:
version "3.6.1"
resolved "https://registry.yarnpkg.com/filesize/-/filesize-3.6.1.tgz#090bb3ee01b6f801a8a8be99d31710b3422bb317"
@ -6298,11 +6313,6 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==
konva@2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/konva/-/konva-2.5.1.tgz#cca611a9522e831e54cf57c508a1aed3f0ceac25"
integrity sha512-YdHEWqmbWPieqIZuLx7JFGm9Ui08hSUaSJ2k2Ml8o5giFgJ0WmxAS0DPXIM+Ty2ADRagOHZfXSJ/skwYqqlwgQ==
last-call-webpack-plugin@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/last-call-webpack-plugin/-/last-call-webpack-plugin-3.0.0.tgz#9742df0e10e3cf46e5c0381c2de90d3a7a2d7555"
@ -8736,12 +8746,12 @@ react-autosize-textarea@^7.0.0:
line-height "^0.3.1"
prop-types "^15.5.6"
react-avatar-edit@^0.8.3:
version "0.8.3"
resolved "https://registry.yarnpkg.com/react-avatar-edit/-/react-avatar-edit-0.8.3.tgz#0ebf21391328fc255429bdfbc782f795827109bf"
integrity sha512-QEedh6DjDCSI7AUsUHHtfhxApCWC5hJAoywxUA5PtUdw03iIjEurgVqPOIt1UBHhU/Zk/9amElRF3oepN9JZSg==
react-avatar-editor@^11.0.7:
version "11.0.7"
resolved "https://registry.yarnpkg.com/react-avatar-editor/-/react-avatar-editor-11.0.7.tgz#021053cfeaa138407b79279ee5a0384f273f0c54"
integrity sha512-GbNYBd1/L1QyuU9VRvOW0hSkW1R0XSneOWZFgqI5phQf6dX+dF/G3/AjiJ0hv3JWh2irMQ7DL0oYDKzwtTnNBQ==
dependencies:
konva "2.5.1"
prop-types "^15.5.8"
react-custom-scrollbars@^4.2.1:
version "4.2.1"
@ -8798,6 +8808,15 @@ react-dom@^16.9.0:
prop-types "^15.6.2"
scheduler "^0.15.0"
react-dropzone@^10.1.8:
version "10.1.9"
resolved "https://registry.yarnpkg.com/react-dropzone/-/react-dropzone-10.1.9.tgz#8093ecd7d2dc4002280eb2dac1d5fa4216c800ee"
integrity sha512-7iqALZ0mzk+4g/AsYxEy3QyWPMTVQYKQVkYUe9zIbH18u+pi7EBDg010KEwfIX6jeTDH2qP0E6/eUnXvBYrovA==
dependencies:
attr-accept "^1.1.3"
file-selector "^0.1.11"
prop-types "^15.7.2"
react-error-overlay@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.1.tgz#b8d3cf9bb991c02883225c48044cb3ee20413e0f"

View File

@ -1,12 +1,11 @@
import React from 'react';
import { Redirect, Route } from 'react-router-dom';
import { AUTH_KEY } from './constants';
import Cookies from 'universal-cookie';
import { Route } from 'react-router-dom';
import { ValidationResult } from './../helpers/constants';
import { decomposeConfirmLink } from './../helpers/converters';
import { PageLayout, Loader } from "asc-web-components";
import { connect } from 'react-redux';
import { checkConfirmLink } from './../store/auth/actions';
import { ValidationResult } from './../helpers/constants';
import decomposeConfirmLink from './../helpers/decomposeConfirmLink';
import { PageLayout, Loader } from "asc-web-components";
import { withRouter } from "react-router";
class ConfirmRoute extends React.Component {
constructor(props) {
@ -20,16 +19,20 @@ class ConfirmRoute extends React.Component {
uid: '',
firstname: '',
lastname: '',
isReady: false,
isLoaded: false,
componentProps: {}
}
}
componentDidMount() {
const { pathname, search } = this.props.location;
const { checkConfirmLink } = this.props;
const decomposedLink = decomposeConfirmLink(pathname, search);
const { location, checkConfirmLink, isAuthenticated, history } = this.props;
const { search } = location;
const decomposedLink = decomposeConfirmLink(location);
let validationResult;
let path = '';
if (!isAuthenticated) {
path = '/login';
}
checkConfirmLink(decomposedLink)
.then((res) => {
validationResult = res.data.response;
@ -38,44 +41,49 @@ class ConfirmRoute extends React.Component {
const confirmHeader = `type=${decomposedLink.type}&${search.slice(1)}`;
const componentProps = Object.assign({}, decomposedLink, { confirmHeader });
this.setState({
isReady: true,
isLoaded: true,
componentProps
});
break;
case ValidationResult.Invalid:
window.location.href = '/login/error=Invalid link'
history.push(`${path}/error=Invalid link`);
break;
case ValidationResult.Expired:
window.location.href = '/login/error=Expired link'
history.push(`${path}/error=Expired link`);
break;
default:
window.location.href = '/login/error=Unknown error'
history.push(`${path}/error=Unknown error`);
break;
}
})
.catch((e) => window.location.href = '/');
.catch((e) => history.push(`${path}/error=${e}`));
}
render() {
const { component: Component, location, path, computedMatch, ...rest } = this.props;
const newProps = Object.assign({}, { location, path, computedMatch }, { linkData: this.state.componentProps });
const { component: Component, ...rest } = this.props;
return (
<Route
{...rest}
render={props =>
!this.state.isReady ? (
!this.state.isLoaded ? (
<PageLayout
sectionBodyContent={
<Loader className="pageLoader" type="rombs" size={40} />
}
/>
) : (
<Component {...newProps} />
)
<Component {...props = { ...props, linkData: this.state.componentProps }} />
)
}
/>
)
}
};
export default connect(null, { checkConfirmLink })(ConfirmRoute);
function mapStateToProps(state) {
return {
isAuthenticated: state.auth.isAuthenticated
};
}
export default connect(mapStateToProps, { checkConfirmLink })(withRouter(ConfirmRoute));

View File

@ -0,0 +1,25 @@
export function getObjectByLocation(location) {
if (!location.search || !location.search.length) return null;
const searchUrl = location.search.substring(1);
const object = JSON.parse(
'{"' +
decodeURIComponent(searchUrl)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
);
return object;
}
export function decomposeConfirmLink(location) {
const queryParams = getObjectByLocation(location);
const url = location.pathname;
const posSeparator = url.lastIndexOf('/');
const type = url.slice(posSeparator + 1);
const data = Object.assign({ type }, queryParams);
return data;
}

View File

@ -1,12 +0,0 @@
const decomposeConfirmLink = (url, querySearch) => {
const decodedString = decodeURIComponent(querySearch);
const queryString = decodedString.slice(1).split('&');
const arrayOfQueryString = queryString.map(queryParam => queryParam.split('='));
const queryParams = Object.fromEntries(arrayOfQueryString);
const posSeparator = url.lastIndexOf('/');
const type = url.slice(posSeparator + 1);
const data = Object.assign({ type }, queryParams);
return data;
}
export default decomposeConfirmLink;