reset create form on load

This commit is contained in:
Sebastian Sdorra
2018-07-27 10:38:35 +02:00
parent 387aa23cd4
commit 26e3525d4c
3 changed files with 28 additions and 5 deletions

View File

@@ -4,7 +4,7 @@ import { connect } from "react-redux";
import UserForm from "./../components/UserForm";
import type { User } from "../types/User";
import type { History } from "history";
import { createUser } from "../modules/users";
import { createUser, createUserReset } from "../modules/users";
import { Page } from "../../components/layout";
import { translate } from "react-i18next";
@@ -13,10 +13,15 @@ type Props = {
addUser: (user: User, callback?: () => void) => void,
loading?: boolean,
error?: Error,
history: History
history: History,
resetForm: () => void
};
class AddUser extends React.Component<Props> {
componentDidMount() {
this.props.resetForm();
}
userCreated = () => {
const { history } = this.props;
history.push("/users");
@@ -46,6 +51,9 @@ const mapDispatchToProps = dispatch => {
return {
addUser: (user: User, callback?: () => void) => {
dispatch(createUser(user, callback));
},
resetForm: () => {
dispatch(createUserReset());
}
};
};

View File

@@ -16,6 +16,7 @@ export const FETCH_USER_FAILURE = "scm/users/FETCH_USER_FAILURE";
export const CREATE_USER_PENDING = "scm/users/CREATE_USER_PENDING";
export const CREATE_USER_SUCCESS = "scm/users/CREATE_USER_SUCCESS";
export const CREATE_USER_FAILURE = "scm/users/CREATE_USER_FAILURE";
export const CREATE_USER_RESET = "scm/users/CREATE_USER_RESET";
export const MODIFY_USER_PENDING = "scm/users/MODIFY_USER_PENDING";
export const MODIFY_USER_SUCCESS = "scm/users/MODIFY_USER_SUCCESS";
@@ -173,6 +174,12 @@ export function createUserFailure(user: User, err: Error): Action {
};
}
export function createUserReset() {
return {
type: CREATE_USER_RESET
};
}
//modify user
export function modifyUser(user: User, callback?: () => void) {
@@ -436,17 +443,15 @@ function createReducer(state: any = {}, action: any = {}) {
switch (action.type) {
case CREATE_USER_PENDING:
return {
...state,
loading: true
};
case CREATE_USER_SUCCESS:
case CREATE_USER_RESET:
return {
...state,
loading: false
};
case CREATE_USER_FAILURE:
return {
...state,
loading: false,
error: action.payload
};

View File

@@ -11,6 +11,7 @@ import reducer, {
createUserFailure,
createUserPending,
createUserSuccess,
createUserReset,
DELETE_USER_FAILURE,
DELETE_USER_PENDING,
DELETE_USER_SUCCESS,
@@ -506,6 +507,15 @@ describe("users reducer", () => {
expect(newState.create.error).toEqual(new Error("kaputt kaputt"));
});
it("should reset the user create form", () => {
const newState = reducer(
{ create: { loading: true, error: new Error("kaputt kaputt") } },
createUserReset()
);
expect(newState.create.loading).toBeFalsy();
expect(newState.create.error).toBeFalsy();
});
it("should update state according to FETCH_USER_PENDING action", () => {
const newState = reducer({}, fetchUserPending("zaphod"));
expect(newState.byNames["zaphod"].loading).toBeTruthy();