Skip to content

Commit

Permalink
refactor: replace error conditions with hoc
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarber committed Aug 4, 2021
1 parent 5974dae commit 239b1b6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
10 changes: 3 additions & 7 deletions app/javascript/components/bathroom.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import styled from 'styled-components';
import { compose } from 'react-recompose';
import { WhiteSubTitle, WhiteTitleLarge } from '@components/typography';
import { LoadingMessage, ErrorMessage } from '@messages/default-messages';
import renderWhileError from '@hocs/render-while-error';
import renderWhileLoading from '@hocs/render-while-loading';
import withFragment from './hocs/with-fragment';

Expand All @@ -20,11 +21,7 @@ const Column = styled.div`
margin-top: 100px;
`;

export const Bathroom = ({ error, location }) => {
if (error) {
return <ErrorMessage />;
}

export const Bathroom = ({ location }) => {
const { bathroomCode } = location;
if (!bathroomCode) {
return null;
Expand All @@ -41,15 +38,14 @@ Bathroom.propTypes = {
location: PropTypes.shape({
bathroomCode: PropTypes.string,
}),
error: PropTypes.bool,
};

Bathroom.defaultProps = {
location: {},
error: false,
};

export default compose(
renderWhileError(ErrorMessage),
renderWhileLoading(LoadingMessage),
withFragment({ location: getBathroomCode }),
)(Bathroom);
13 changes: 3 additions & 10 deletions app/javascript/components/date.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { compose } from 'react-recompose';
import { dateForTimezone } from '@lib/datetime';
import { colors, fontSizes, spacing, fonts } from '@lib/theme';
import { LoadingMessage, ErrorMessage } from '@messages/default-messages';
import renderWhileError from '@hocs/render-while-error';
import renderWhileLoading from '@hocs/render-while-loading';
import withFragment from './hocs/with-fragment';

Expand All @@ -24,15 +25,12 @@ export const DateText = styled.div`

export class Date extends React.Component {
static propTypes = {
error: PropTypes.bool,
location: PropTypes.shape({
timezone: PropTypes.string,
}).isRequired,
};

static defaultProps = {
error: false,
};
static defaultProps = {};

state = { date: null };

Expand Down Expand Up @@ -66,12 +64,6 @@ export class Date extends React.Component {
};

render() {
const { error } = this.props;

if (error) {
return <ErrorMessage />;
}

const { date } = this.state;
if (!date) {
return null;
Expand All @@ -81,6 +73,7 @@ export class Date extends React.Component {
}

export default compose(
renderWhileError(ErrorMessage),
renderWhileLoading(LoadingMessage),
withFragment({ location: getTimeZone }),
)(Date);
13 changes: 4 additions & 9 deletions app/javascript/components/time-hero.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import gql from 'graphql-tag';
import Time from '@components/time';
import { compose } from 'react-recompose';
import { LoadingMessage, ErrorMessage } from '@messages/default-messages';
import renderWhileError from '@hocs/render-while-error';
import renderWhileLoading from '@hocs/render-while-loading';
import withFragment from './hocs/with-fragment';

Expand All @@ -13,24 +14,18 @@ export const getTimeHero = gql`
}
`;

export const TimeHero = ({ error, location }) => {
if (error) {
return <ErrorMessage />;
}

export const TimeHero = ({ location }) => {
return <Time location={location} />;
};

TimeHero.propTypes = {
error: PropTypes.bool,
location: PropTypes.shape({}).isRequired,
};

TimeHero.defaultProps = {
error: false,
};
TimeHero.defaultProps = {};

export default compose(
renderWhileError(ErrorMessage),
renderWhileLoading(LoadingMessage),
withFragment({ location: getTimeHero }),
)(TimeHero);
13 changes: 3 additions & 10 deletions app/javascript/components/wifi.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Row } from '@components/row';
import { LoadingMessage, ErrorMessage } from '@messages/default-messages';
import WifiIcon from '@assets/images/icons/icon-wifi.svg';
import LockIcon from '@assets/images/icons/icon-key.svg';
import renderWhileError from '@hocs/render-while-error';
import renderWhileLoading from '@hocs/render-while-loading';
import withFragment from './hocs/with-fragment';

Expand All @@ -32,11 +33,7 @@ export const getWifi = gql`
}
`;

export const Wifi = ({ error, location }) => {
if (error) {
return <ErrorMessage />;
}

export const Wifi = ({ location }) => {
const { wifiName, wifiPassword } = location;
if (!wifiName || !wifiPassword) {
return null;
Expand Down Expand Up @@ -69,14 +66,10 @@ Wifi.propTypes = {
wifiName: PropTypes.string,
wifiPassword: PropTypes.string,
}).isRequired,
error: PropTypes.bool,
};

Wifi.defaultProps = {
error: false,
};

export default compose(
renderWhileError(ErrorMessage),
renderWhileLoading(LoadingMessage),
withFragment({ location: getWifi }),
)(Wifi);

0 comments on commit 239b1b6

Please sign in to comment.