fetch group after modifying it

This commit is contained in:
Maren Süwer
2018-11-05 14:12:35 +01:00
parent bb92041e09
commit 40613744cf
4 changed files with 64 additions and 20 deletions

View File

@@ -2,7 +2,7 @@
import React from "react";
import { connect } from "react-redux";
import GroupForm from "../components/GroupForm";
import { modifyGroup, fetchGroup } from "../modules/groups";
import { modifyGroup } from "../modules/groups";
import type { History } from "history";
import { withRouter } from "react-router-dom";
import type { Group } from "@scm-manager/ui-types";
@@ -12,7 +12,6 @@ import { ErrorNotification } from "@scm-manager/ui-components";
type Props = {
group: Group,
modifyGroup: (group: Group, callback?: () => void) => void,
fetchGroup: (name: string) => void,
history: History,
loading?: boolean,
error: Error
@@ -20,7 +19,6 @@ type Props = {
class EditGroup extends React.Component<Props> {
groupModified = (group: Group) => () => {
this.props.fetchGroup(group.name);
this.props.history.push(`/group/${group.name}`);
};
@@ -58,9 +56,6 @@ const mapDispatchToProps = dispatch => {
return {
modifyGroup: (group: Group, callback?: () => void) => {
dispatch(modifyGroup(group, callback));
},
fetchGroup: (name: string) => {
dispatch(fetchGroup(name));
}
};
};

View File

@@ -16,7 +16,7 @@ import type { Group } from "@scm-manager/ui-types";
import type { History } from "history";
import {
deleteGroup,
fetchGroup,
fetchGroupByName,
getGroupByName,
isFetchGroupPending,
getFetchGroupFailure,
@@ -37,7 +37,7 @@ type Props = {
// dispatcher functions
deleteGroup: (group: Group, callback?: () => void) => void,
fetchGroup: (string, string) => void,
fetchGroupByName: (string, string) => void,
// context objects
t: string => string,
@@ -47,7 +47,7 @@ type Props = {
class SingleGroup extends React.Component<Props> {
componentDidMount() {
this.props.fetchGroup(this.props.groupLink, this.props.name);
this.props.fetchGroupByName(this.props.groupLink, this.props.name);
}
stripEndingSlash = (url: string) => {
@@ -147,8 +147,8 @@ const mapStateToProps = (state, ownProps) => {
const mapDispatchToProps = dispatch => {
return {
fetchGroup: (link: string, name: string) => {
dispatch(fetchGroup(link, name));
fetchGroupByName: (link: string, name: string) => {
dispatch(fetchGroupByName(link, name));
},
deleteGroup: (group: Group, callback?: () => void) => {
dispatch(deleteGroup(group, callback));

View File

@@ -84,12 +84,20 @@ export function fetchGroupsFailure(url: string, error: Error): Action {
}
//fetch group
export function fetchGroup(link: string, name: string) {
export function fetchGroupByLink(group: Group) {
return fetchGroup(group._links.self.href, group.name);
}
export function fetchGroupByName(link: string, name: string) {
const groupUrl = link.endsWith("/") ? link + name : link + "/" + name;
return fetchGroup(groupUrl, name);
}
function fetchGroup(link: string, name: string) {
return function(dispatch: any) {
dispatch(fetchGroupPending(name));
return apiClient
.get(groupUrl)
.get(link)
.then(response => {
return response.json();
})
@@ -189,6 +197,9 @@ export function modifyGroup(group: Group, callback?: () => void) {
callback();
}
})
.then(() => {
dispatch(fetchGroupByLink(group));
})
.catch(cause => {
dispatch(
modifyGroupFailure(
@@ -361,8 +372,6 @@ function byNamesReducer(state: any = {}, action: any = {}) {
};
case FETCH_GROUP_SUCCESS:
return reducerByName(state, action.payload.name, action.payload);
case MODIFY_GROUP_SUCCESS:
return reducerByName(state, action.payload.name, action.payload);
case DELETE_GROUP_SUCCESS:
const newGroupByNames = deleteGroupInGroupsByNames(
state,

View File

@@ -15,7 +15,8 @@ import reducer, {
getFetchGroupsFailure,
isFetchGroupsPending,
selectListAsCollection,
fetchGroup,
fetchGroupByLink,
fetchGroupByName,
FETCH_GROUP_PENDING,
FETCH_GROUP_SUCCESS,
FETCH_GROUP_FAILURE,
@@ -171,11 +172,40 @@ describe("groups fetch()", () => {
});
});
it("should sucessfully fetch single group", () => {
it("should sucessfully fetch single group by name", () => {
fetchMock.getOnce(GROUPS_URL + "/humanGroup", humanGroup);
const store = mockStore({});
return store.dispatch(fetchGroup(URL, "humanGroup")).then(() => {
return store.dispatch(fetchGroupByName(URL, "humanGroup")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_GROUP_PENDING);
expect(actions[1].type).toEqual(FETCH_GROUP_SUCCESS);
expect(actions[1].payload).toBeDefined();
});
});
it("should fail fetching single group by name on HTTP 500", () => {
fetchMock.getOnce(GROUPS_URL + "/humanGroup", {
status: 500
});
const store = mockStore({});
return store.dispatch(fetchGroupByName(URL, "humanGroup")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_GROUP_PENDING);
expect(actions[1].type).toEqual(FETCH_GROUP_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
it("should sucessfully fetch single group", () => {
fetchMock.getOnce(
"http://localhost:8081/api/v2/groups/humanGroup",
humanGroup
);
const store = mockStore({});
return store.dispatch(fetchGroupByLink(humanGroup)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_GROUP_PENDING);
expect(actions[1].type).toEqual(FETCH_GROUP_SUCCESS);
@@ -184,12 +214,12 @@ describe("groups fetch()", () => {
});
it("should fail fetching single group on HTTP 500", () => {
fetchMock.getOnce(GROUPS_URL + "/humanGroup", {
fetchMock.getOnce("http://localhost:8081/api/v2/groups/humanGroup", {
status: 500
});
const store = mockStore({});
return store.dispatch(fetchGroup(URL, "humanGroup")).then(() => {
return store.dispatch(fetchGroupByLink(humanGroup)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_GROUP_PENDING);
expect(actions[1].type).toEqual(FETCH_GROUP_FAILURE);
@@ -247,6 +277,10 @@ describe("groups fetch()", () => {
fetchMock.putOnce("http://localhost:8081/api/v2/groups/humanGroup", {
status: 204
});
fetchMock.getOnce(
"http://localhost:8081/api/v2/groups/humanGroup",
humanGroup
);
const store = mockStore({});
@@ -254,6 +288,7 @@ describe("groups fetch()", () => {
const actions = store.getActions();
expect(actions[0].type).toEqual(MODIFY_GROUP_PENDING);
expect(actions[1].type).toEqual(MODIFY_GROUP_SUCCESS);
expect(actions[2].type).toEqual(FETCH_GROUP_PENDING);
expect(actions[1].payload).toEqual(humanGroup);
});
});
@@ -262,6 +297,10 @@ describe("groups fetch()", () => {
fetchMock.putOnce("http://localhost:8081/api/v2/groups/humanGroup", {
status: 204
});
fetchMock.getOnce(
"http://localhost:8081/api/v2/groups/humanGroup",
humanGroup
);
let called = false;
const callback = () => {
@@ -273,6 +312,7 @@ describe("groups fetch()", () => {
const actions = store.getActions();
expect(actions[0].type).toEqual(MODIFY_GROUP_PENDING);
expect(actions[1].type).toEqual(MODIFY_GROUP_SUCCESS);
expect(actions[2].type).toEqual(FETCH_GROUP_PENDING);
expect(called).toBe(true);
});
});