From a8a5bfca5efc4afae56cac8d017289351e0f94ea Mon Sep 17 00:00:00 2001 From: ONS-Tom Date: Mon, 13 Nov 2017 12:26:56 +0000 Subject: [PATCH] Fix/user feedback (#8) * Add fix for PostCode on Match form - Add value prop * Add SummaryTable component for useful stats - For num results + capped results * Add ChildRefTable component for child references - Not using Redux, just using fetch() inside the component * Add siccodes conversion JSON - Add SIC code conversion to expand view * Add ResultsTable component * Add additional props into ResultsTable - DefaultPageSize, pagination etc. * Fix clear functionality for UBRN search - Focus on UBRN input after clear * Remove comments, add config for /business endpoint * Add proper formatting of ExpandView JSON * Add Business Index description on Home page * Fix Logout logic, handle 500's - If there is a server error whilst logging out, the user will still be logged out * Clear sessionStorage rather than localStorage --- server/app.js | 14 + src/actions/LoginActions.js | 7 +- src/components/ChildRefTable.js | 90 ++++ src/components/MatchForm.js | 2 +- src/components/ResultsTable.js | 88 ++++ src/components/SummaryTable.js | 33 ++ src/components/UBRNForm.js | 20 +- src/config/api-urls.js | 1 + src/utils/helperMethods.js | 25 ++ src/utils/siccode.js | 735 ++++++++++++++++++++++++++++++++ src/views/Home.js | 2 +- src/views/Match.js | 67 +-- src/views/RangeQuery.js | 59 +-- src/views/UBRNLookup.js | 79 +--- 14 files changed, 1034 insertions(+), 188 deletions(-) create mode 100644 src/components/ChildRefTable.js create mode 100644 src/components/ResultsTable.js create mode 100644 src/components/SummaryTable.js create mode 100644 src/utils/siccode.js diff --git a/server/app.js b/server/app.js index 2109e07..8fcd428 100644 --- a/server/app.js +++ b/server/app.js @@ -148,6 +148,20 @@ app.post('/logout', (req, res) => { res.sendStatus(200); }); +app.post('/logout', (req, res) => { + logger.info('Logging user out'); + const token = req.body.token; + try { + // Remove user from storage + delete sessions[token]; + logger.info('Successful logout'); + res.sendStatus(200); + } catch (e) { + logger.error(`Unable to log user out: ${e}`); + res.sendStatus(500); + } +}); + app.post('/api', (req, res) => { // re route api requests with API key const method = req.body.method; diff --git a/src/actions/LoginActions.js b/src/actions/LoginActions.js index a0b1d7b..ac705f1 100644 --- a/src/actions/LoginActions.js +++ b/src/actions/LoginActions.js @@ -118,10 +118,10 @@ export function logout() { return (dispatch) => { dispatch(sendingRequest(true)); auth.logout(sessionStorage.accessToken, (success) => { + dispatch(sendingRequest(false)); if (success) { - dispatch(sendingRequest(false)); dispatch(setAuthState(false)); - localStorage.clear(); + sessionStorage.clear(); browserHistory.push('/'); // This needs to go at the end, or else if we logout whilst on a page // that uses the redux store, an error will occur before the user @@ -129,6 +129,9 @@ export function logout() { dispatch(resetState(undefined)); } else { dispatch(setErrorMessage(errorMessages.GENERAL_ERROR)); + sessionStorage.clear(); + browserHistory.push('/'); + dispatch(resetState(undefined)); } }); }; diff --git a/src/components/ChildRefTable.js b/src/components/ChildRefTable.js new file mode 100644 index 0000000..0ec4076 --- /dev/null +++ b/src/components/ChildRefTable.js @@ -0,0 +1,90 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import ReactTable from 'react-table'; +import ErrorModal from '../components/ErrorModal'; +import industryCodeDescription from '../utils/siccode'; +import config from '../config/api-urls'; +import { formatData } from '../utils/helperMethods'; + +const { REROUTE_URL, API_VERSION, BUSINESS_ENDPOINT } = config; + +class ChildRefTable extends React.Component { + constructor(props) { + super(props); + this.state = { + isLoading: true, + data: [], + error: false, + errorMessage: '', + }; + this.closeModal = this.closeModal.bind(this); + this.fetchData = this.fetchData.bind(this); + } + componentDidMount() { + this.fetchData(this.props.row); + } + fetchData(row) { + fetch(`${REROUTE_URL}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': sessionStorage.getItem('accessToken'), + }, + body: JSON.stringify({ + method: 'GET', + endpoint: `${API_VERSION}/${BUSINESS_ENDPOINT}/${row.original.id}`, + }), + }) + .then(response => { + if (response.ok) { + return response.json(); + } + throw new Error(`Error: ${response.status} ${response.statusText}`); + }) + .then(data => this.setState({ data: formatData(data), isLoading: false })) + .catch(error => this.setState({ errorMessage: error.message, error: true, isLoading: false })); + } + closeModal() { + this.setState({ error: false, errorMessage: '' }); + } + render() { + return ( +
+ +

Industry Code: {industryCodeDescription[this.state.data.industryCode]}

+ +
+ ); + } +} + +ChildRefTable.propTypes = { + row: PropTypes.object.isRequired, +}; + +export default ChildRefTable; diff --git a/src/components/MatchForm.js b/src/components/MatchForm.js index 68e03cf..4624388 100644 --- a/src/components/MatchForm.js +++ b/src/components/MatchForm.js @@ -20,7 +20,7 @@ class MatchForm extends React.Component {


-
+
+ + (this.childTextInput = ip)} value={this.props.value} label="UBRN" id="UBRN" autoFocus onChange={this.props.onChange} />
+