updated the channel creation route with GA event

This commit is contained in:
bill bittner 2018-03-07 19:39:22 -08:00
parent d21ab2e4b0
commit 188706ca31
3 changed files with 28 additions and 44 deletions

View file

@ -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 } })
return db.Channel
.findAll({
where: { channelName: name },
})
.then(result => {
if (result.length >= 1) {
return resolve(false);
throw new Error('That channel has already been claimed');
}
resolve(true);
return name;
})
.catch(error => {
reject(error);
});
throw error;
});
},
};

View file

@ -38,32 +38,13 @@ class ChannelCreateForm extends React.Component {
updateIsChannelAvailable (channel) {
const channelWithAtSymbol = `@${channel}`;
request(`/api/channel/availability/${channelWithAtSymbol}`)
.then(isAvailable => {
if (isAvailable) {
.then(() => {
this.setState({'error': null});
} else {
this.setState({'error': 'That channel has already been claimed'});
}
})
.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 () {

View file

@ -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);