mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 23:15:43 +01:00
reset create form on load
This commit is contained in:
@@ -4,7 +4,7 @@ import { connect } from "react-redux";
|
|||||||
import UserForm from "./../components/UserForm";
|
import UserForm from "./../components/UserForm";
|
||||||
import type { User } from "../types/User";
|
import type { User } from "../types/User";
|
||||||
import type { History } from "history";
|
import type { History } from "history";
|
||||||
import { createUser } from "../modules/users";
|
import { createUser, createUserReset } from "../modules/users";
|
||||||
import { Page } from "../../components/layout";
|
import { Page } from "../../components/layout";
|
||||||
import { translate } from "react-i18next";
|
import { translate } from "react-i18next";
|
||||||
|
|
||||||
@@ -13,10 +13,15 @@ type Props = {
|
|||||||
addUser: (user: User, callback?: () => void) => void,
|
addUser: (user: User, callback?: () => void) => void,
|
||||||
loading?: boolean,
|
loading?: boolean,
|
||||||
error?: Error,
|
error?: Error,
|
||||||
history: History
|
history: History,
|
||||||
|
resetForm: () => void
|
||||||
};
|
};
|
||||||
|
|
||||||
class AddUser extends React.Component<Props> {
|
class AddUser extends React.Component<Props> {
|
||||||
|
componentDidMount() {
|
||||||
|
this.props.resetForm();
|
||||||
|
}
|
||||||
|
|
||||||
userCreated = () => {
|
userCreated = () => {
|
||||||
const { history } = this.props;
|
const { history } = this.props;
|
||||||
history.push("/users");
|
history.push("/users");
|
||||||
@@ -46,6 +51,9 @@ const mapDispatchToProps = dispatch => {
|
|||||||
return {
|
return {
|
||||||
addUser: (user: User, callback?: () => void) => {
|
addUser: (user: User, callback?: () => void) => {
|
||||||
dispatch(createUser(user, callback));
|
dispatch(createUser(user, callback));
|
||||||
|
},
|
||||||
|
resetForm: () => {
|
||||||
|
dispatch(createUserReset());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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_PENDING = "scm/users/CREATE_USER_PENDING";
|
||||||
export const CREATE_USER_SUCCESS = "scm/users/CREATE_USER_SUCCESS";
|
export const CREATE_USER_SUCCESS = "scm/users/CREATE_USER_SUCCESS";
|
||||||
export const CREATE_USER_FAILURE = "scm/users/CREATE_USER_FAILURE";
|
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_PENDING = "scm/users/MODIFY_USER_PENDING";
|
||||||
export const MODIFY_USER_SUCCESS = "scm/users/MODIFY_USER_SUCCESS";
|
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
|
//modify user
|
||||||
|
|
||||||
export function modifyUser(user: User, callback?: () => void) {
|
export function modifyUser(user: User, callback?: () => void) {
|
||||||
@@ -436,17 +443,15 @@ function createReducer(state: any = {}, action: any = {}) {
|
|||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case CREATE_USER_PENDING:
|
case CREATE_USER_PENDING:
|
||||||
return {
|
return {
|
||||||
...state,
|
|
||||||
loading: true
|
loading: true
|
||||||
};
|
};
|
||||||
case CREATE_USER_SUCCESS:
|
case CREATE_USER_SUCCESS:
|
||||||
|
case CREATE_USER_RESET:
|
||||||
return {
|
return {
|
||||||
...state,
|
|
||||||
loading: false
|
loading: false
|
||||||
};
|
};
|
||||||
case CREATE_USER_FAILURE:
|
case CREATE_USER_FAILURE:
|
||||||
return {
|
return {
|
||||||
...state,
|
|
||||||
loading: false,
|
loading: false,
|
||||||
error: action.payload
|
error: action.payload
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import reducer, {
|
|||||||
createUserFailure,
|
createUserFailure,
|
||||||
createUserPending,
|
createUserPending,
|
||||||
createUserSuccess,
|
createUserSuccess,
|
||||||
|
createUserReset,
|
||||||
DELETE_USER_FAILURE,
|
DELETE_USER_FAILURE,
|
||||||
DELETE_USER_PENDING,
|
DELETE_USER_PENDING,
|
||||||
DELETE_USER_SUCCESS,
|
DELETE_USER_SUCCESS,
|
||||||
@@ -506,6 +507,15 @@ describe("users reducer", () => {
|
|||||||
expect(newState.create.error).toEqual(new Error("kaputt kaputt"));
|
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", () => {
|
it("should update state according to FETCH_USER_PENDING action", () => {
|
||||||
const newState = reducer({}, fetchUserPending("zaphod"));
|
const newState = reducer({}, fetchUserPending("zaphod"));
|
||||||
expect(newState.byNames["zaphod"].loading).toBeTruthy();
|
expect(newState.byNames["zaphod"].loading).toBeTruthy();
|
||||||
|
|||||||
Reference in New Issue
Block a user