mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
fetch user again after modification
This commit is contained in:
@@ -15,7 +15,7 @@ import EditUser from "./EditUser";
|
|||||||
import type { User } from "@scm-manager/ui-types";
|
import type { User } from "@scm-manager/ui-types";
|
||||||
import type { History } from "history";
|
import type { History } from "history";
|
||||||
import {
|
import {
|
||||||
fetchUser,
|
fetchUserByName,
|
||||||
deleteUser,
|
deleteUser,
|
||||||
getUserByName,
|
getUserByName,
|
||||||
isFetchUserPending,
|
isFetchUserPending,
|
||||||
@@ -37,7 +37,7 @@ type Props = {
|
|||||||
|
|
||||||
// dispatcher functions
|
// dispatcher functions
|
||||||
deleteUser: (user: User, callback?: () => void) => void,
|
deleteUser: (user: User, callback?: () => void) => void,
|
||||||
fetchUser: (string, string) => void,
|
fetchUserByName: (string, string) => void,
|
||||||
|
|
||||||
// context objects
|
// context objects
|
||||||
t: string => string,
|
t: string => string,
|
||||||
@@ -47,7 +47,7 @@ type Props = {
|
|||||||
|
|
||||||
class SingleUser extends React.Component<Props> {
|
class SingleUser extends React.Component<Props> {
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.fetchUser(this.props.usersLink, this.props.name);
|
this.props.fetchUserByName(this.props.usersLink, this.props.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
userDeleted = () => {
|
userDeleted = () => {
|
||||||
@@ -138,8 +138,8 @@ const mapStateToProps = (state, ownProps) => {
|
|||||||
|
|
||||||
const mapDispatchToProps = dispatch => {
|
const mapDispatchToProps = dispatch => {
|
||||||
return {
|
return {
|
||||||
fetchUser: (link: string, name: string) => {
|
fetchUserByName: (link: string, name: string) => {
|
||||||
dispatch(fetchUser(link, name));
|
dispatch(fetchUserByName(link, name));
|
||||||
},
|
},
|
||||||
deleteUser: (user: User, callback?: () => void) => {
|
deleteUser: (user: User, callback?: () => void) => {
|
||||||
dispatch(deleteUser(user, callback));
|
dispatch(deleteUser(user, callback));
|
||||||
|
|||||||
@@ -87,12 +87,20 @@ export function fetchUsersFailure(url: string, error: Error): Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//fetch user
|
//fetch user
|
||||||
export function fetchUser(link: string, name: string) {
|
export function fetchUserByName(link: string, name: string) {
|
||||||
const userUrl = link.endsWith("/") ? link + name : link + "/" + name;
|
const userUrl = link.endsWith("/") ? link + name : link + "/" + name;
|
||||||
|
return fetchUser(userUrl, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fetchUserByLink(user: User) {
|
||||||
|
return fetchUser(user._links.self.href, user.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchUser(link: string, name: string) {
|
||||||
return function(dispatch: any) {
|
return function(dispatch: any) {
|
||||||
dispatch(fetchUserPending(name));
|
dispatch(fetchUserPending(name));
|
||||||
return apiClient
|
return apiClient
|
||||||
.get(userUrl)
|
.get(link)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
@@ -195,6 +203,9 @@ export function modifyUser(user: User, callback?: () => void) {
|
|||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.then(() => {
|
||||||
|
dispatch(fetchUserByLink(user));
|
||||||
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
dispatch(modifyUserFailure(user, err));
|
dispatch(modifyUserFailure(user, err));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ import reducer, {
|
|||||||
FETCH_USERS_FAILURE,
|
FETCH_USERS_FAILURE,
|
||||||
FETCH_USERS_PENDING,
|
FETCH_USERS_PENDING,
|
||||||
FETCH_USERS_SUCCESS,
|
FETCH_USERS_SUCCESS,
|
||||||
fetchUser,
|
fetchUserByLink,
|
||||||
|
fetchUserByName,
|
||||||
fetchUserSuccess,
|
fetchUserSuccess,
|
||||||
getFetchUserFailure,
|
getFetchUserFailure,
|
||||||
fetchUsers,
|
fetchUsers,
|
||||||
@@ -166,11 +167,37 @@ describe("users fetch()", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should sucessfully fetch single user", () => {
|
it("should sucessfully fetch single user by name", () => {
|
||||||
fetchMock.getOnce(USERS_URL + "/zaphod", userZaphod);
|
fetchMock.getOnce(USERS_URL + "/zaphod", userZaphod);
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(fetchUser(URL, "zaphod")).then(() => {
|
return store.dispatch(fetchUserByName(URL, "zaphod")).then(() => {
|
||||||
|
const actions = store.getActions();
|
||||||
|
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
||||||
|
expect(actions[1].type).toEqual(FETCH_USER_SUCCESS);
|
||||||
|
expect(actions[1].payload).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should fail fetching single user by name on HTTP 500", () => {
|
||||||
|
fetchMock.getOnce(USERS_URL + "/zaphod", {
|
||||||
|
status: 500
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = mockStore({});
|
||||||
|
return store.dispatch(fetchUserByName(URL, "zaphod")).then(() => {
|
||||||
|
const actions = store.getActions();
|
||||||
|
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
||||||
|
expect(actions[1].type).toEqual(FETCH_USER_FAILURE);
|
||||||
|
expect(actions[1].payload).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should sucessfully fetch single user", () => {
|
||||||
|
fetchMock.getOnce("http://localhost:8081/api/v2/users/zaphod", userZaphod);
|
||||||
|
|
||||||
|
const store = mockStore({});
|
||||||
|
return store.dispatch(fetchUserByLink(userZaphod)).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
||||||
expect(actions[1].type).toEqual(FETCH_USER_SUCCESS);
|
expect(actions[1].type).toEqual(FETCH_USER_SUCCESS);
|
||||||
@@ -179,12 +206,12 @@ describe("users fetch()", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should fail fetching single user on HTTP 500", () => {
|
it("should fail fetching single user on HTTP 500", () => {
|
||||||
fetchMock.getOnce(USERS_URL + "/zaphod", {
|
fetchMock.getOnce("http://localhost:8081/api/v2/users/zaphod", {
|
||||||
status: 500
|
status: 500
|
||||||
});
|
});
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(fetchUser(URL, "zaphod")).then(() => {
|
return store.dispatch(fetchUserByLink(userZaphod)).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
||||||
expect(actions[1].type).toEqual(FETCH_USER_FAILURE);
|
expect(actions[1].type).toEqual(FETCH_USER_FAILURE);
|
||||||
@@ -245,13 +272,15 @@ describe("users fetch()", () => {
|
|||||||
fetchMock.putOnce("http://localhost:8081/api/v2/users/zaphod", {
|
fetchMock.putOnce("http://localhost:8081/api/v2/users/zaphod", {
|
||||||
status: 204
|
status: 204
|
||||||
});
|
});
|
||||||
|
fetchMock.getOnce("http://localhost:8081/api/v2/users/zaphod", userZaphod);
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(modifyUser(userZaphod)).then(() => {
|
return store.dispatch(modifyUser(userZaphod)).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions.length).toBe(2);
|
expect(actions.length).toBe(3);
|
||||||
expect(actions[0].type).toEqual(MODIFY_USER_PENDING);
|
expect(actions[0].type).toEqual(MODIFY_USER_PENDING);
|
||||||
expect(actions[1].type).toEqual(MODIFY_USER_SUCCESS);
|
expect(actions[1].type).toEqual(MODIFY_USER_SUCCESS);
|
||||||
|
expect(actions[2].type).toEqual(FETCH_USER_PENDING);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user