diff --git a/client/coral-embed-stream/src/Embed.js b/client/coral-embed-stream/src/Embed.js
index 550fc5fc1..d31b716e9 100644
--- a/client/coral-embed-stream/src/Embed.js
+++ b/client/coral-embed-stream/src/Embed.js
@@ -20,6 +20,7 @@ import CommentBox from 'coral-plugin-commentbox/CommentBox';
import UserBox from 'coral-sign-in/components/UserBox';
import SignInContainer from 'coral-sign-in/containers/SignInContainer';
import SuspendedAccount from 'coral-framework/components/SuspendedAccount';
+import ChangeDisplayNameContainer from '../../coral-sign-in/containers/ChangeDisplayNameContainer';
import SettingsContainer from 'coral-settings/containers/SettingsContainer';
import RestrictedContent from 'coral-framework/components/RestrictedContent';
import ConfigureStreamContainer from 'coral-configure/containers/ConfigureStreamContainer';
@@ -131,6 +132,7 @@ class Embed extends Component {
:
{asset.settings.closedMessage}
}
{!loggedIn && }
+ {loggedIn && user && }
({type: actions.SHOW_SIGNIN_DIALOG, offset});
export const hideSignInDialog = () => ({type: actions.HIDE_SIGNIN_DIALOG});
+export const createDisplayNameRequest = () => ({type: actions.CREATE_DISPLAYNAME_REQUEST});
+export const showCreateDisplayNameDialog = () => ({type: actions.SHOW_CREATEDISPLAYNAME_DIALOG});
+export const hideCreateDisplayNameDialog = () => ({type: actions.HIDE_CREATEDISPLAYNAME_DIALOG});
+
+const createDisplayNameSuccess = () => ({type: actions.CREATEDISPLAYNAME_SUCCESS});
+const createDisplayNameFailure = error => ({type: actions.CREATEDISPLAYNAME_FAILURE, error});
+
+export const updateDisplayName = displayName => ({type: actions.UPDATE_DISPLAYNAME, displayName});
+
+export const createDisplayName = (userId, formData) => dispatch => {
+ dispatch(createDisplayNameRequest());
+ coralApi(`/users/${userId}/displayname`, {method: 'POST', body: formData})
+ .then((user) => {
+ dispatch(createDisplayNameSuccess());
+ dispatch(hideCreateDisplayNameDialog());
+ dispatch(updateDisplayName(user));
+ })
+ .catch(error => {
+ dispatch(createDisplayNameFailure(lang.t(`error.${error.message}`)));
+ });
+};
+
export const changeView = view => dispatch =>
dispatch({
type: actions.CHANGE_VIEW,
@@ -60,6 +82,19 @@ export const fetchSignInFacebook = () => dispatch => {
);
};
+// Sign Up Facebook
+
+const signUpFacebookRequest = () => ({type: actions.FETCH_SIGNUP_FACEBOOK_REQUEST});
+
+export const fetchSignUpFacebook = () => dispatch => {
+ dispatch(signUpFacebookRequest());
+ window.open(
+ `${base}/auth/facebook`,
+ 'Continue with Facebook',
+ 'menubar=0,resizable=0,width=500,height=500,top=200,left=500'
+ );
+};
+
export const facebookCallback = (err, data) => dispatch => {
if (err) {
signInFacebookFailure(err);
@@ -69,6 +104,7 @@ export const facebookCallback = (err, data) => dispatch => {
const user = JSON.parse(data);
dispatch(signInFacebookSuccess(user));
dispatch(hideSignInDialog());
+ dispatch(showCreateDisplayNameDialog());
} catch (err) {
dispatch(signInFacebookFailure(err));
return;
diff --git a/client/coral-framework/constants/auth.js b/client/coral-framework/constants/auth.js
index 1dae348df..2b5f762c1 100644
--- a/client/coral-framework/constants/auth.js
+++ b/client/coral-framework/constants/auth.js
@@ -4,6 +4,13 @@ export const CLEAN_STATE = 'CLEAN_STATE';
export const SHOW_SIGNIN_DIALOG = 'SHOW_SIGNIN_DIALOG';
export const HIDE_SIGNIN_DIALOG = 'HIDE_SIGNIN_DIALOG';
+export const CREATE_DISPLAYNAME_REQUEST = 'CREATE_DISPLAYNAME_REQUEST';
+export const CREATEDISPLAYNAME_SUCCESS = 'CREATEDISPLAYNAME_SUCCESS';
+export const CREATEDISPLAYNAME_FAILURE = 'CREATEDISPLAYNAME_FAILURE';
+export const CREATE_DISPLAYNAME = 'CREATE_DISPLAYNAME';
+export const SHOW_CREATEDISPLAYNAME_DIALOG = 'SHOW_CREATEDISPLAYNAME_DIALOG';
+export const HIDE_CREATEDISPLAYNAME_DIALOG = 'HIDE_CREATEDISPLAYNAME_DIALOG';
+
export const FETCH_SIGNUP_REQUEST = 'FETCH_SIGNUP_REQUEST';
export const FETCH_SIGNUP_FAILURE = 'FETCH_SIGNUP_FAILURE';
export const FETCH_SIGNUP_SUCCESS = 'FETCH_SIGNUP_SUCCESS';
@@ -16,6 +23,7 @@ export const FETCH_SIGNIN_FACEBOOK_REQUEST = 'FETCH_SIGNIN_FACEBOOK_REQUEST';
export const FETCH_SIGNIN_FACEBOOK_FAILURE = 'FETCH_SIGNIN_FACEBOOK_FAILURE';
export const FETCH_SIGNIN_FACEBOOK_SUCCESS = 'FETCH_SIGNIN_FACEBOOK_SUCCESS';
+export const FETCH_SIGNUP_FACEBOOK_REQUEST = 'FETCH_SIGNUP_FACEBOOK_REQUEST';
export const FETCH_FORGOT_PASSWORD_REQUEST = 'FETCH_FORGOT_PASSWORD_REQUEST';
export const FETCH_FORGOT_PASSWORD_SUCCESS = 'FETCH_FORGOT_PASSWORD_SUCCESS';
export const FETCH_FORGOT_PASSWORD_FAILURE = 'FETCH_FORGOT_PASSWORD_FAILURE';
@@ -33,6 +41,7 @@ export const CHECK_LOGIN_FAILURE = 'CHECK_LOGIN_FAILURE';
export const CHECK_CSRF_TOKEN = 'CHECK_CSRF_TOKEN';
+export const UPDATE_DISPLAYNAME = 'UPDATE_DISPLAYNAME';
export const EMAIL_CONFIRM_ERROR = 'EMAIL_CONFIRM_ERROR';
export const CONFIRM_EMAIL_REQUEST = 'CONFIRM_EMAIL_REQUEST';
export const CONFIRM_EMAIL_SUCCESS = 'CONFIRM_EMAIL_SUCCESS';
diff --git a/client/coral-framework/constants/user.js b/client/coral-framework/constants/user.js
index 9c6f508fe..57f4e706b 100644
--- a/client/coral-framework/constants/user.js
+++ b/client/coral-framework/constants/user.js
@@ -5,3 +5,4 @@ export const COMMENTS_BY_USER_REQUEST = 'COMMENTS_BY_USER_REQUEST';
export const COMMENTS_BY_USER_SUCCESS = 'COMMENTS_BY_USER_SUCCESS';
export const COMMENTS_BY_USER_FAILURE = 'COMMENTS_BY_USER_FAILURE';
export const LOGOUT_SUCCESS = 'LOGOUT_SUCCESS';
+export const UPDATE_DISPLAYNAME = 'UPDATE_DISPLAYNAME';
diff --git a/client/coral-framework/reducers/auth.js b/client/coral-framework/reducers/auth.js
index 6cb4e97cd..2da589507 100644
--- a/client/coral-framework/reducers/auth.js
+++ b/client/coral-framework/reducers/auth.js
@@ -7,6 +7,7 @@ const initialState = Map({
isAdmin: false,
user: null,
showSignInDialog: false,
+ showCreateDisplayNameDialog: false,
view: 'SIGNIN',
error: '',
passwordRequestSuccess: null,
@@ -14,7 +15,8 @@ const initialState = Map({
emailConfirmationFailure: false,
emailConfirmationLoading: false,
emailConfirmationSuccess: false,
- successSignUp: false
+ successSignUp: false,
+ fromSignUp: false
});
const purge = user => {
@@ -41,6 +43,21 @@ export default function auth (state = initialState, action) {
emailConfirmationLoading: false,
successSignUp: false
}));
+ case actions.SHOW_CREATEDISPLAYNAME_DIALOG :
+ return state
+ .set('showCreateDisplayNameDialog', true);
+ case actions.HIDE_CREATEDISPLAYNAME_DIALOG :
+ return state.merge(Map({
+ showCreateDisplayNameDialog: false
+ }));
+ case actions.CREATEDISPLAYNAME_SUCCESS :
+ return state.merge(Map({
+ showCreateDisplayNameDialog: false,
+ error: ''
+ }));
+ case actions.CREATEDISPLAYNAME_FAILURE :
+ return state
+ .set('error', action.error);
case actions.CHANGE_VIEW :
return state
.set('error', '')
@@ -72,6 +89,12 @@ export default function auth (state = initialState, action) {
.set('isLoading', false)
.set('error', action.error)
.set('user', null);
+ case actions.FETCH_SIGNUP_FACEBOOK_REQUEST:
+ return state
+ .set('fromSignUp', true);
+ case actions.FETCH_SIGNIN_FACEBOOK_REQUEST:
+ return state
+ .set('fromSignUp', false);
case actions.FETCH_SIGNIN_FACEBOOK_SUCCESS:
return state
.set('user', purge(action.user))
@@ -107,6 +130,9 @@ export default function auth (state = initialState, action) {
return state
.set('passwordRequestFailure', 'There was an error sending your password reset email. Please try again soon!')
.set('passwordRequestSuccess', null);
+ case actions.UPDATE_DISPLAYNAME:
+ return state
+ .set('user', purge(action.displayName));
case actions.EMAIL_CONFIRM_ERROR:
return state
.set('emailConfirmationFailure', true)
diff --git a/client/coral-sign-in/components/CreateDisplayNameDialog.js b/client/coral-sign-in/components/CreateDisplayNameDialog.js
new file mode 100644
index 000000000..4d8ef9fb3
--- /dev/null
+++ b/client/coral-sign-in/components/CreateDisplayNameDialog.js
@@ -0,0 +1,48 @@
+import React from 'react';
+import FormField from 'coral-ui/components/FormField';
+import Alert from './Alert';
+import Button from 'coral-ui/components/Button';
+import {Dialog} from 'coral-ui';
+import styles from './styles.css';
+import I18n from 'coral-framework/modules/i18n/i18n';
+import translations from '../translations';
+const lang = new I18n(translations);
+
+const CreateDisplayNameDialog = ({open, handleClose, offset, formData, handleSubmitDisplayName, handleChange, ...props}) => (
+
+);
+
+export default CreateDisplayNameDialog;
diff --git a/client/coral-sign-in/components/SignUpContent.js b/client/coral-sign-in/components/SignUpContent.js
index 246d9d884..80a6fcde4 100644
--- a/client/coral-sign-in/components/SignUpContent.js
+++ b/client/coral-sign-in/components/SignUpContent.js
@@ -14,7 +14,7 @@ const SignUpContent = ({handleChange, formData, ...props}) => (
-
diff --git a/client/coral-sign-in/containers/ChangeDisplayNameContainer.js b/client/coral-sign-in/containers/ChangeDisplayNameContainer.js
new file mode 100644
index 000000000..bc7fe8267
--- /dev/null
+++ b/client/coral-sign-in/containers/ChangeDisplayNameContainer.js
@@ -0,0 +1,135 @@
+import React, {Component} from 'react';
+import {connect} from 'react-redux';
+
+import validate from 'coral-framework/helpers/validate';
+import errorMsj from 'coral-framework/helpers/error';
+
+import CreateDisplayNameDialog from '../components/CreateDisplayNameDialog';
+
+import I18n from 'coral-framework/modules/i18n/i18n';
+import translations from '../translations';
+const lang = new I18n(translations);
+
+import {
+ showCreateDisplayNameDialog,
+ hideCreateDisplayNameDialog,
+ invalidForm,
+ validForm,
+ createDisplayName
+} from '../../coral-framework/actions/auth';
+
+class ChangeDisplayNameContainer extends Component {
+ initialState = {
+ formData: {
+ displayName: '',
+ },
+ errors: {},
+ showErrors: false
+ };
+
+ constructor(props) {
+ super(props);
+ this.state = this.initialState;
+ this.handleChange = this.handleChange.bind(this);
+ this.handleSubmitDisplayName = this.handleSubmitDisplayName.bind(this);
+ this.handleClose = this.handleClose.bind(this);
+ this.addError = this.addError.bind(this);
+ }
+
+ handleChange(e) {
+ const {name, value} = e.target;
+ this.setState(state => ({
+ ...state,
+ formData: {
+ ...state.formData,
+ [name]: value
+ }
+ }), () => {
+ this.validation(name, value);
+ });
+ }
+
+ addError(name, error) {
+ return this.setState(state => ({
+ errors: {
+ ...state.errors,
+ [name]: error
+ }
+ }));
+ }
+
+ validation(name, value) {
+ const {addError} = this;
+
+ if (!value.length) {
+ addError(name, lang.t('createdisplay.requiredField'));
+ } else if (!validate[name](value)) {
+ addError(name, errorMsj[name]);
+ } else {
+ const { [name]: prop, ...errors } = this.state.errors; // eslint-disable-line
+ // Removes Error
+ this.setState(state => ({...state, errors}));
+ }
+ }
+
+ isCompleted() {
+ const {formData} = this.state;
+ return !Object.keys(formData).filter(prop => !formData[prop].length).length;
+ }
+
+ displayErrors(show = true) {
+ this.setState({showErrors: show});
+ }
+
+ handleSubmitDisplayName(e) {
+ e.preventDefault();
+ const {errors} = this.state;
+ const {validForm, invalidForm} = this.props;
+ this.displayErrors();
+ if (this.isCompleted() && !Object.keys(errors).length) {
+ this.props.createDisplayName(this.props.user.id, this.state.formData);
+ validForm();
+ } else {
+ invalidForm(lang.t('createdisplay.checkTheForm'));
+ }
+ }
+
+ handleClose() {
+ this.props.hideCreateDisplayNameDialog();
+ }
+
+ render() {
+ const {loggedIn, auth, offset} = this.props;
+ return (
+
+
+
+ );
+ }
+}
+
+const mapStateToProps = state => ({
+ auth: state.auth.toJS()
+});
+
+const mapDispatchToProps = dispatch => ({
+ createDisplayName: (userid, formData) => dispatch(createDisplayName(userid, formData)),
+ showCreateDisplayNameDialog: () => dispatch(showCreateDisplayNameDialog()),
+ hideCreateDisplayNameDialog: () => dispatch(hideCreateDisplayNameDialog()),
+ invalidForm: error => dispatch(invalidForm(error)),
+ validForm: () => dispatch(validForm())
+});
+
+export default connect(
+ mapStateToProps,
+ mapDispatchToProps
+)(ChangeDisplayNameContainer);
diff --git a/client/coral-sign-in/containers/SignInContainer.js b/client/coral-sign-in/containers/SignInContainer.js
index 2435cdcfd..937c830c9 100644
--- a/client/coral-sign-in/containers/SignInContainer.js
+++ b/client/coral-sign-in/containers/SignInContainer.js
@@ -15,6 +15,7 @@ import {
showSignInDialog,
hideSignInDialog,
fetchSignInFacebook,
+ fetchSignUpFacebook,
fetchForgotPassword,
requestConfirmEmail,
facebookCallback,
@@ -177,6 +178,7 @@ const mapDispatchToProps = dispatch => ({
fetchSignUp: formData => dispatch(fetchSignUp(formData)),
fetchSignIn: formData => dispatch(fetchSignIn(formData)),
fetchSignInFacebook: () => dispatch(fetchSignInFacebook()),
+ fetchSignUpFacebook: () => dispatch(fetchSignUpFacebook()),
fetchForgotPassword: formData => dispatch(fetchForgotPassword(formData)),
requestConfirmEmail: email => dispatch(requestConfirmEmail(email)),
showSignInDialog: () => dispatch(showSignInDialog()),
diff --git a/client/coral-sign-in/translations.js b/client/coral-sign-in/translations.js
index 137141d34..5751370a6 100644
--- a/client/coral-sign-in/translations.js
+++ b/client/coral-sign-in/translations.js
@@ -26,7 +26,17 @@ export default {
passwordsDontMatch: 'Passwords don\'t match.',
specialCharacters: 'Display names can contain letters, numbers and _ only',
checkTheForm: 'Invalid Form. Please, check the fields'
- }
+ },
+ 'createdisplay': {
+ writeyourusername: 'Write your username',
+ yourusername: 'Your username is publicly visible on all comments you post. A username is needed before you can post your first comment.',
+ displayName: 'Display Name',
+ save: 'Save',
+ requiredField: 'Required field',
+ errorCreate: 'Error when changing display name',
+ checkTheForm: 'Invalid Form. Please, check the fields',
+ specialCharacters: 'Display names can contain letters, numbers and _ only'
+ },
},
es: {
'signIn': {
@@ -55,6 +65,16 @@ export default {
passwordsDontMatch: 'Las contraseñas no coinciden',
specialCharacters: 'Los nombres pueden contener letras, números y _',
checkTheForm: 'Formulario Inválido. Por favor, completa los campos'
- }
+ },
+ 'createdisplay': {
+ writeyourusername: 'Escribe tu nombre',
+ yourusername: 'Tu nombre es visible publicamente en todos los comentarios que publiques. Es necesario tener un nombre de usuario antes de poder publicar tu primer comentario.',
+ displayName: 'Nombre a mostrar',
+ save: 'Guardar',
+ requiredField: 'Campo necesario',
+ errorCreate: 'Hubo un error al cambiar el nombre de usuario',
+ checkTheForm: 'Formulario Invalido. Por favor, verifica los campos',
+ specialCharacters: 'Sólo pueden contener letras, números y _'
+ },
}
};
diff --git a/routes/api/users/index.js b/routes/api/users/index.js
index 6da7b9a4b..1c7bc7bb2 100644
--- a/routes/api/users/index.js
+++ b/routes/api/users/index.js
@@ -60,6 +60,14 @@ router.post('/:user_id/status', authorization.needed('ADMIN'), (req, res, next)
.catch(next);
});
+router.post('/:user_id/displayname', authorization.needed(), (req, res, next) => {
+ UsersService.setDisplayName(req.params.user_id, req.body.displayName)
+ .then((user) => {
+ res.status(201).json(user);
+ })
+ .catch(next);
+});
+
router.post('/:user_id/email', authorization.needed('admin'), (req, res, next) => {
UsersService.findById(req.params.user_id)
.then(user => {
diff --git a/services/users.js b/services/users.js
index 70472bea7..fd39a50c1 100644
--- a/services/users.js
+++ b/services/users.js
@@ -353,6 +353,25 @@ module.exports = class UsersService {
return UserModel.update({id}, {$set: {status}});
}
+ /**
+ * Set the display name of a user.
+ * @param {String} id id of a user
+ * @param {String} displayName display name to set
+ * @param {Function} done callback after the operation is complete
+ */
+ static setDisplayName(id, displayName) {
+
+ return UsersService.isValidDisplayName(displayName)
+ .then(() => { // displayName is valid
+ return UserModel.update(
+ {id},
+ {$set: {'displayName': displayName}})
+ .then(() => {
+ return UserModel.findOne({'id': id});
+ });
+ });
+ }
+
/**
* Finds a user with the id.
* @param {String} id user id (uuid)
diff --git a/test/services/users.js b/test/services/users.js
index e2e5655e4..ba2bedcc3 100644
--- a/test/services/users.js
+++ b/test/services/users.js
@@ -178,6 +178,25 @@ describe('services.UsersService', () => {
});
});
+ describe('#setDisplayName', () => {
+ it('should set the display name to a new unique one', () => {
+ return UsersService
+ .setDisplayName(mockUsers[0].id, 'maria')
+ .then(() => UsersService.findById(mockUsers[0].id))
+ .then((user) => {
+ expect(user).to.have.property('displayName', 'maria');
+ });
+ });
+
+ it('should return an error when the displayName is not unique', () => {
+ return UsersService
+ .setDisplayName(mockUsers[0].id, 'marvel')
+ .catch((error) => {
+ expect(error).to.not.be.null;
+ });
+ });
+ });
+
describe('#ban', () => {
it('should set the status to banned', () => {
return UsersService