Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Commit

Permalink
fix(Add player): #79 email duplicate, add mongoose unique to email an…
Browse files Browse the repository at this point in the history
…d display error message when email is duplicated during submission (#88)
  • Loading branch information
fredwong-it authored and ahstein3521 committed Mar 22, 2018
1 parent 6127344 commit c2abfc5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
10 changes: 8 additions & 2 deletions client/src/actions/players/createPlayer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import { ADD_PLAYER, ADD_PLAYER_TO_TEAM } from '../types';
import { ADD_PLAYER, ADD_PLAYER_TO_TEAM, OPEN_SNACKBAR } from '../types';
import { ROOT_URL } from '../../../globals';
import { openSnackbar } from '../index';


// Add a new player to DB,
Expand All @@ -18,6 +19,8 @@ export function createPlayer(form, dispatch, props) {

axios.post(`${ROOT_URL}/player/add`, reqBody)
.then(({data}) => {
// successful message
openSnackbar(form, dispatch, props);

// Send new player to the playersReducer to be appended to the list
dispatch({ type: ADD_PLAYER, payload: data });
Expand All @@ -29,6 +32,9 @@ export function createPlayer(form, dispatch, props) {
payload: { teamId: team.teamId, player: data }
});
}

})
.catch(error => {
// display error message
dispatch({ type: OPEN_SNACKBAR, message: error.response.data.message });
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ function mapStateToProps(state, ownProps) {
}

PlayerFormTemplate = reduxForm({
validate,
onSubmitSuccess: openSnackbar
validate
})(PlayerFormTemplate);

export default connect(mapStateToProps)(PlayerFormTemplate);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"lodash": "^4.17.4",
"material-ui": "^0.17.4",
"mongoose": "^4.7.8",
"mongoose-unique-validator": "^2.0.0",
"nodemailer": "^4.0.1",
"nodemon": "^1.12.1",
"passport": "^0.3.2",
Expand Down
4 changes: 3 additions & 1 deletion server/api/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const addPlayerToLeague = (req, res) => {
res.send(newPlayer);
}
})
.catch(error => { throw error; });
.catch(error => {
res.status(400).send({ message: error.message });
});
};

const getPlayer = (req, res) => {
Expand Down
5 changes: 4 additions & 1 deletion server/models/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mongoose = require('mongoose');

const Schema = mongoose.Schema;
const capitalize = require('./plugins/capitalize');
const mongooseUniqueValidator = require('mongoose-unique-validator');

const PlayerSchema = new Schema({
firstName: {
Expand All @@ -18,7 +19,8 @@ const PlayerSchema = new Schema({
},
email: {
type: String,
required: true
required: true,
unique: true
},
phoneNum: {
type: String
Expand Down Expand Up @@ -68,5 +70,6 @@ PlayerSchema.virtual('fullName').get(function() {
});

PlayerSchema.plugin(capitalize, { fields });
PlayerSchema.plugin(mongooseUniqueValidator);

module.exports = mongoose.model('player', PlayerSchema);

0 comments on commit c2abfc5

Please sign in to comment.