React+Flux: Notify View/Component that action has failed?

Viewed 166

I'm writing a Registration Form Component. When form submits it triggers a create user action. createUser actions creates a new user via an ajax api call. If a user already exist, triggered user action fails. We know we cannot return a response from an ajax call, we need a callback for that. How do i tell me component that a user already exists?

// Register From Component
var Register    =   React.createClass({
    saveUser : function(event){
        event.preventDefault();
        userActions.createUser(this.state.user);
    },

    render : function(){
        return (
            <div className="register-form">
                {message}
                <RegisterForm
                    user={this.state.user}
                    onChange={this.saveUserState}
                    onSave={this.saveUser}
                    errors={this.state.errors} />
            </div>
        );
    }
});

// Api Ajax call
module.exports = {
    saveUser : function(user) {
        $.ajax({
            type : 'POST',
            data : {
                email : user.email,
                password : user.password
            },
            success: function(res){

            }
        });
    },
};

// Actions
var UserActions = {
    createUser : function(user){
        var newUser = UserApi.saveUser(user);

        Dispatcher.dispatch({
            actionType : ActionTypes.CREATE_USER,
            user : newUser
        });
    }
};

I was thinking of returning a boolean value from createUser method.

...
onSave : function(event){

    if(userActions.createUser(this.state.user)){
         // 
    }
}
...

But we cannot return a value from ajax request, how can i notify my component that api has failed to create a new user, so that it can render a error message to user.

1 Answers
Related