diff --git a/controllers/publishController.js b/controllers/publishController.js index 55078caa..215160d8 100644 --- a/controllers/publishController.js +++ b/controllers/publishController.js @@ -106,21 +106,24 @@ module.exports = { return name; }; return name; + }) + .catch(error => { + throw error; }); }, checkChannelAvailability (name) { - return new Promise((resolve, reject) => { - // find any records where the name is used - db.Channel.findAll({ where: { channelName: name } }) - .then(result => { - if (result.length >= 1) { - return resolve(false); - } - resolve(true); - }) - .catch(error => { - reject(error); - }); - }); + return db.Channel + .findAll({ + where: { channelName: name }, + }) + .then(result => { + if (result.length >= 1) { + throw new Error('That channel has already been claimed'); + } + return name; + }) + .catch(error => { + throw error; + }); }, }; diff --git a/react/containers/ChannelCreateForm/view.jsx b/react/containers/ChannelCreateForm/view.jsx index 269a08eb..56dbdedd 100644 --- a/react/containers/ChannelCreateForm/view.jsx +++ b/react/containers/ChannelCreateForm/view.jsx @@ -38,32 +38,13 @@ class ChannelCreateForm extends React.Component { updateIsChannelAvailable (channel) { const channelWithAtSymbol = `@${channel}`; request(`/api/channel/availability/${channelWithAtSymbol}`) - .then(isAvailable => { - if (isAvailable) { - this.setState({'error': null}); - } else { - this.setState({'error': 'That channel has already been claimed'}); - } + .then(() => { + this.setState({'error': null}); }) .catch((error) => { this.setState({'error': error.message}); }); } - checkIsChannelAvailable (channel) { - const channelWithAtSymbol = `@${channel}`; - return new Promise((resolve, reject) => { - request(`/api/channel/availability/${channelWithAtSymbol}`) - .then(isAvailable => { - if (!isAvailable) { - return reject(new Error('That channel has already been claimed')); - } - resolve(); - }) - .catch((error) => { - reject(error); - }); - }); - } checkIsPasswordProvided () { const password = this.state.password; return new Promise((resolve, reject) => { @@ -96,7 +77,9 @@ class ChannelCreateForm extends React.Component { event.preventDefault(); this.checkIsPasswordProvided() .then(() => { - return this.checkIsChannelAvailable(this.state.channel, this.state.password); + if (this.state.error) { + throw new Error(); + } }) .then(() => { this.setState({status: 'We are publishing your new channel. Sit tight...'}); @@ -107,7 +90,7 @@ class ChannelCreateForm extends React.Component { this.props.onChannelLogin(result.channelName, result.shortChannelId, result.channelClaimId); }) .catch((error) => { - this.setState({'error': error.message, status: null}); + if (error.message) this.setState({'error': error.message, status: null}); }); } render () { diff --git a/routes/api-routes.js b/routes/api-routes.js index 2b37dc73..4f75ecb3 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -16,14 +16,12 @@ const NO_CLAIM = 'NO_CLAIM'; module.exports = (app) => { // route to check whether site has published to a channel - app.get('/api/channel/availability/:name', ({ ip, originalUrl, params }, res) => { - checkChannelAvailability(params.name) - .then(result => { - if (result === true) { - res.status(200).json(true); - } else { - res.status(200).json(false); - } + app.get('/api/channel/availability/:name', ({ ip, originalUrl, params: { name } }, res) => { + const gaStartTime = Date.now(); + checkChannelAvailability(name) + .then(availableName => { + res.status(200).json(availableName); + sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); }) .catch(error => { errorHandlers.handleErrorResponse(originalUrl, ip, error, res);