Skip to content

Commit

Permalink
feat: add error-boundary & error-indicator components (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliaksandr-Shliayeu authored and vharadkou committed Aug 2, 2019
1 parent 03cf22b commit fd410d2
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 57 deletions.
44 changes: 43 additions & 1 deletion js_machine_front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions js_machine_front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"version": "0.2.2",
"private": true,
"dependencies": {
"@types/react-intl": "^2.3.18",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-intl": "^2.9.0",
"react-router-dom": "^5.0.1"
},
"scripts": {
Expand Down
42 changes: 30 additions & 12 deletions js_machine_front/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,41 @@ import { Events } from 'routes/Events';
import { News } from 'routes/News';
import { About } from 'routes/About';
import { Main } from 'routes/Main';
import { ErrorBoundary } from 'components/error-boundary';
import { Auth } from 'routes/Authorization';
import { Route, Redirect, Switch } from 'react-router';

// Internationalization
import { messages } from 'i18n/messages';
import en from 'react-intl/locale-data/en';
import ru from 'react-intl/locale-data/ru';
import { addLocaleData, IntlProvider } from 'react-intl';

addLocaleData([...en, ...ru]);

const locale =
(navigator.languages && navigator.languages[0]) ||
navigator.language ||
'en-US';

const App: React.FC = () => {
return (
<div className="app">
<NavBar/>
<Switch>
<Route exact={true} path="/" component={Main} />
<Route exact={true} path="/about" component={About} />
<Route exact={true} path="/news" component={News} />
<Route exact={true} path="/events" component={Events} />
<Route exact={true} path="/partners" component={Partners} />
<Route exact={true} path="/authorization" component={Auth} />
<Redirect to={'/'} />
</Switch>
</div>
<IntlProvider locale={locale} messages={messages[locale]}>
<div className="app">
<NavBar />
<ErrorBoundary>
<Switch>
<Route exact={true} path="/" component={Main} />
<Route exact={true} path="/about" component={About} />
<Route exact={true} path="/news" component={News} />
<Route exact={true} path="/events" component={Events} />
<Route exact={true} path="/partners" component={Partners} />
<Route exact={true} path="/authorization" component={Auth} />
<Redirect to={'/'} />
</Switch>
</ErrorBoundary>
</div>
</IntlProvider>
);
};

Expand Down
11 changes: 6 additions & 5 deletions js_machine_front/src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { memo } from 'react';
import LogoSrc from 'images/logo.png';
import 'styles/route.css';
import { NavLink } from 'react-router-dom';
import { FormattedMessage } from 'react-intl';

export const NavBar: React.FC = memo(() => {
return (
Expand All @@ -14,19 +15,19 @@ export const NavBar: React.FC = memo(() => {

<div className="menu">
<NavLink exact={true} className="menu__nav-link" activeClassName="active-link" to="/about">
О НАС
<FormattedMessage id="page.about" />
</NavLink>
<NavLink exact={true} className="menu__nav-link" activeClassName="active-link" to="/news">
НОВОСТИ
<FormattedMessage id="page.news" />
</NavLink>
<NavLink exact={true} className="menu__nav-link" activeClassName="active-link" to="/events">
СОБЫТИЯ
<FormattedMessage id="page.events" />
</NavLink>
<NavLink exact={true} className="menu__nav-link" activeClassName="active-link" to="/partners">
ПАРТНЕРЫ
<FormattedMessage id="page.partners" />
</NavLink>
<NavLink exact={true} className="menu__nav-link" activeClassName="active-link" to="/authorization">
ВХОД
<FormattedMessage id="page.signIn" />
</NavLink>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ export const AuthorizationHOC = (AuthComponent: React.FC<AuthorizationHOCProps>)
signUpStyle: 'reset_width',
barStyle: 'authorization-bar__wrapper_sign-in',
barTitle: '',
barBtn: ''
barBtn: '',
};
}

changeLoginOptions = () => {
if (this.state.isAuthorizitationVisible) {
this.setState(() => {
let signUpMocks = getStateForSignUp();
let signUpStyles = {
const signUpMocks = getStateForSignUp();
const signUpStyles = {
isAuthorizitationVisible: false,
signInStyle: 'reset_width',
signUpStyle: 'sign-up__wrapper',
barStyle: 'authorization-bar__wrapper_sign-up'
barStyle: 'authorization-bar__wrapper_sign-up',
};
return {...signUpStyles, ...signUpMocks};
});
} else {
this.setState(() => {
let signInMocks = getStateForSignIn();
let signInStyles = {
const signInMocks = getStateForSignIn();
const signInStyles = {
isAuthorizitationVisible: true,
signInStyle: 'sign-in__wrapper',
signUpStyle: 'reset_width',
barStyle: 'authorization-bar__wrapper_sign-in'
barStyle: 'authorization-bar__wrapper_sign-in',
};
return {...signInStyles, ...signInMocks};
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import React, { memo } from 'react';
import { SignInFrom } from 'components/authorization/SignInFrom';
import { SocialAuth } from 'components/authorization/SocialAuth';
import { FormattedMessage } from 'react-intl';

import 'styles/authorizationSignIn.css';

interface SignInProps {
signInStyle: string;
signInStyle: string;
}

export const AuthorizationSignIn: React.FC<SignInProps> = memo((props: SignInProps) => {
export const AuthorizationSignIn: React.FC<SignInProps> = memo(
(props: SignInProps) => {
return (
<div className="sign-in">
<div className={`sign-in__wrapper ${props.signInStyle}`}>
<div className="sign-in__title">Вход</div>
<SocialAuth />
<SignInFrom />
</div>
<div className="sign-in">
<div className={`sign-in__wrapper ${props.signInStyle}`}>
<div className="sign-in__title">
<FormattedMessage id="authorization.signIn" />
</div>
<SocialAuth />
<SignInFrom />
</div>
</div>
);
});
},
);
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import React, { memo } from 'react';
import { SignUpForm } from 'components/authorization/SignUpForm';
import { SocialAuth } from 'components/authorization/SocialAuth';
import { FormattedMessage } from 'react-intl';

import 'styles/authorizationSignUp.css';

interface SignUpProps {
signUpStyle: string;
signUpStyle: string;
}

export const AuthorizationSignUp: React.FC<SignUpProps> = memo((props: SignUpProps) => {
export const AuthorizationSignUp: React.FC<SignUpProps> = memo(
(props: SignUpProps) => {
return (
<div className="sign-up">
<div className={`sign-up__wrapper ${props.signUpStyle}`}>
<div className="sign-up__title">Создать аккаунт</div>
<SocialAuth />
<SignUpForm />
</div>
<div className="sign-up">
<div className={`sign-up__wrapper ${props.signUpStyle}`}>
<div className="sign-up__title">
<FormattedMessage id="authorization.signUp" />
</div>
<SocialAuth />
<SignUpForm />
</div>
</div>
);
});
},
);
20 changes: 15 additions & 5 deletions js_machine_front/src/components/authorization/SignInFrom.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import React, { memo } from 'react';
import { FormattedMessage } from 'react-intl';

import 'styles/authForm.css';

export const SignInFrom: React.FC = memo(() => {
return (
<form className="auth-from">
<div className="auth-form__title">или используйте E-mail для регистрации:</div>
<input className="auth-form__input" type="text" placeholder="E-mail" />
<input className="auth-form__input" type="password" placeholder="Пароль" />
<button className="auth-form__btn" type="submit">ВОЙТИ</button>
</form>
<div className="auth-form__title">
<FormattedMessage id="authorization.signInFormTitle" />
</div>
<FormattedMessage id="authorization.signInFormEmail" >
{ placeholder => <input className="auth-form__input" type="email" placeholder={placeholder as string}/>}
</FormattedMessage>
<FormattedMessage id="authorization.signInFormPassword" >
{placeholder => <input className="auth-form__input" type="password" placeholder={placeholder as string}/>}
</FormattedMessage>
<button className="auth-form__btn" type="submit">
<FormattedMessage id="authorization.signInFormSubmit" />
</button>
</form >
);
});
24 changes: 18 additions & 6 deletions js_machine_front/src/components/authorization/SignUpForm.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import React, { memo } from 'react';
import { FormattedMessage } from 'react-intl';

import 'styles/authForm.css';

export const SignUpForm: React.FC = memo(() => {
return (
<form className="auth-from">
<div className="auth-form__title">или используйте E-mail для регистрации:</div>
<input className="auth-form__input" type="text" placeholder="Фамилия" />
<input className="auth-form__input" type="text" placeholder="E-mail" />
<input className="auth-form__input" type="text" placeholder="Имя" />
<input className="auth-form__input" type="password" placeholder="Пароль" />
<button className="auth-form__btn" type="submit">ЗАРЕГИСТРИРОВАТЬСЯ</button>
<div className="auth-form__title"><FormattedMessage id="authorization.signUpFormTitle" /></div>
<FormattedMessage id="authorization.signUpFormLastName" >
{ placeholder => <input className="auth-form__input" type="text" placeholder={placeholder as string}/>}
</FormattedMessage>
<FormattedMessage id="authorization.signUpFormEmail" >
{ placeholder => <input className="auth-form__input" type="email" placeholder={placeholder as string}/>}
</FormattedMessage>
<FormattedMessage id="authorization.signUpFormFirstName" >
{ placeholder => <input className="auth-form__input" type="text" placeholder={placeholder as string}/>}
</FormattedMessage>
<FormattedMessage id="authorization.signUpFormPassword" >
{ placeholder => <input className="auth-form__input" type="text" placeholder={placeholder as string}/>}
</FormattedMessage>
<button className="auth-form__btn" type="submit">
<FormattedMessage id="authorization.signUpFormSubmit" />
</button>
</form>
);
});
Empty file.
26 changes: 26 additions & 0 deletions js_machine_front/src/components/error-boundary/error-boundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as React from 'react';

import { ErrorIndicator } from '../error-indicator';

interface ErrorBoundaryState {
hasError: boolean;
}

export class ErrorBoundary extends React.Component<{}, ErrorBoundaryState> {
state: ErrorBoundaryState = {
hasError: false,
};

componentDidCatch(): void {
this.setState({
hasError: true,
});
}

render(): React.ReactNode | React.FC {
if (this.state.hasError) {
return <ErrorIndicator />;
}
return this.props.children;
}
}
1 change: 1 addition & 0 deletions js_machine_front/src/components/error-boundary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './error-boundary';
Loading

0 comments on commit fd410d2

Please sign in to comment.