improve typing and fixed import

This commit is contained in:
Sebastian Sdorra
2018-08-06 13:55:54 +02:00
parent dec6b3f17d
commit a3c00b9e12
3 changed files with 49 additions and 39 deletions

View File

@@ -2,7 +2,7 @@
import React from "react"; import React from "react";
import InputField from "../../components/forms/InputField"; import InputField from "../../components/forms/InputField";
import { SubmitButton, Button } from "../../components/buttons"; import { SubmitButton } from "../../components/buttons";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import type { Group } from "../types/Group"; import type { Group } from "../types/Group";
import * as validator from "./groupValidation"; import * as validator from "./groupValidation";
@@ -117,8 +117,7 @@ class GroupForm extends React.Component<Props, State> {
members: usernames members: usernames
} }
}); });
} };
addUser = (username: string) => { addUser = (username: string) => {
if (this.isMember(username)) { if (this.isMember(username)) {

View File

@@ -1,3 +1,4 @@
// @flow
import { apiClient } from "../../apiclient"; import { apiClient } from "../../apiclient";
import { isPending } from "../../modules/pending"; import { isPending } from "../../modules/pending";
import { getFailure } from "../../modules/failure"; import { getFailure } from "../../modules/failure";
@@ -5,7 +6,7 @@ import * as types from "../../modules/types";
import { combineReducers, Dispatch } from "redux"; import { combineReducers, Dispatch } from "redux";
import type { Action } from "../../types/Action"; import type { Action } from "../../types/Action";
import type { PagedCollection } from "../../types/Collection"; import type { PagedCollection } from "../../types/Collection";
import type { Groups } from "../types/Groups"; import type { Group } from "../types/Group";
export const FETCH_GROUPS = "scm/groups/FETCH_GROUPS"; export const FETCH_GROUPS = "scm/groups/FETCH_GROUPS";
export const FETCH_GROUPS_PENDING = `${FETCH_GROUPS}_${types.PENDING_SUFFIX}`; export const FETCH_GROUPS_PENDING = `${FETCH_GROUPS}_${types.PENDING_SUFFIX}`;
@@ -139,10 +140,11 @@ export function createGroup(group: Group, callback?: () => void) {
return apiClient return apiClient
.postWithContentType(GROUPS_URL, group, CONTENT_TYPE_GROUP) .postWithContentType(GROUPS_URL, group, CONTENT_TYPE_GROUP)
.then(() => { .then(() => {
dispatch(createGroupSuccess()) dispatch(createGroupSuccess());
if (callback) { if (callback) {
callback(); callback();
}}) }
})
.catch(error => { .catch(error => {
dispatch( dispatch(
createGroupFailure( createGroupFailure(
@@ -175,24 +177,29 @@ export function createGroupFailure(error: Error) {
export function createGroupReset() { export function createGroupReset() {
return { return {
type: CREATE_GROUP_RESET type: CREATE_GROUP_RESET
} };
} }
// modify group // modify group
export function modifyGroup(group: Group, callback?: () => void) { export function modifyGroup(group: Group, callback?: () => void) {
return function(dispatch: Dispatch) { return function(dispatch: Dispatch) {
dispatch(modifyGroupPending(group)); dispatch(modifyGroupPending(group));
return apiClient return apiClient
.putWithContentType(group._links.update.href, group, CONTENT_TYPE_GROUP) .putWithContentType(group._links.update.href, group, CONTENT_TYPE_GROUP)
.then(() => { .then(() => {
dispatch(modifyGroupSuccess(group)) dispatch(modifyGroupSuccess(group));
if (callback) { if (callback) {
callback() callback();
} }
}) })
.catch(cause => { .catch(cause => {
dispatch(modifyGroupFailure(group, new Error(`could not modify group ${group.name}: ${cause.message}`))) dispatch(
}) modifyGroupFailure(
group,
new Error(`could not modify group ${group.name}: ${cause.message}`)
)
);
});
}; };
} }
@@ -201,7 +208,7 @@ export function modifyGroupPending(group: Group): Action {
type: MODIFY_GROUP_PENDING, type: MODIFY_GROUP_PENDING,
payload: group, payload: group,
itemId: group.name itemId: group.name
} };
} }
export function modifyGroupSuccess(group: Group): Action { export function modifyGroupSuccess(group: Group): Action {
@@ -209,7 +216,7 @@ export function modifyGroupSuccess(group: Group): Action {
type: MODIFY_GROUP_SUCCESS, type: MODIFY_GROUP_SUCCESS,
payload: group, payload: group,
itemId: group.name itemId: group.name
} };
} }
export function modifyGroupFailure(group: Group, error: Error): Action { export function modifyGroupFailure(group: Group, error: Error): Action {
@@ -220,7 +227,7 @@ export function modifyGroupFailure(group: Group, error: Error): Action {
group group
}, },
itemId: group.name itemId: group.name
} };
} }
//delete group //delete group
@@ -274,7 +281,7 @@ export function deleteGroupFailure(group: Group, error: Error): Action {
//reducer //reducer
function extractGroupsByNames( function extractGroupsByNames(
groups: Groups[], groups: Group[],
groupNames: string[], groupNames: string[],
oldGroupsByNames: Object oldGroupsByNames: Object
) { ) {
@@ -332,14 +339,14 @@ function listReducer(state: any = {}, action: any = {}) {
}; };
// Delete single group actions // Delete single group actions
case DELETE_GROUP_SUCCESS: case DELETE_GROUP_SUCCESS:
const newGroupEntries = deleteGroupInEntries( const newGroupEntries = deleteGroupInEntries(
state.entries, state.entries,
action.payload.name action.payload.name
); );
return { return {
...state, ...state,
entries: newGroupEntries entries: newGroupEntries
}; };
default: default:
return state; return state;
} }
@@ -357,7 +364,7 @@ function byNamesReducer(state: any = {}, action: any = {}) {
}; };
case FETCH_GROUP_SUCCESS: case FETCH_GROUP_SUCCESS:
return reducerByName(state, action.payload.name, action.payload); return reducerByName(state, action.payload.name, action.payload);
case MODIFY_GROUP_SUCCESS: case MODIFY_GROUP_SUCCESS:
return reducerByName(state, action.payload.name, action.payload); return reducerByName(state, action.payload.name, action.payload);
case DELETE_GROUP_SUCCESS: case DELETE_GROUP_SUCCESS:
const newGroupByNames = deleteGroupInGroupsByNames( const newGroupByNames = deleteGroupInGroupsByNames(
@@ -436,11 +443,11 @@ export function getCreateGroupFailure(state: Object) {
} }
export function isModifyGroupPending(state: Object, name: string) { export function isModifyGroupPending(state: Object, name: string) {
return(isPending(state, MODIFY_GROUP, name)) return isPending(state, MODIFY_GROUP, name);
} }
export function getModifyGroupFailure(state: Object, name: string) { export function getModifyGroupFailure(state: Object, name: string) {
return(getFailure(state, MODIFY_GROUP, name)) return getFailure(state, MODIFY_GROUP, name);
} }
export function getGroupByName(state: Object, name: string) { export function getGroupByName(state: Object, name: string) {

View File

@@ -1,14 +1,18 @@
//@flow //@flow
import type { Collection } from "../../types/Collection";
import type { Links } from "../../types/hal"; import type { Links } from "../../types/hal";
import type { User } from "../../users/types/User";
export type Group = { export type Member = {
name: string,
_links: Links
};
export type Group = Collection & {
name: string, name: string,
description: string, description: string,
type: string, type: string,
members: string[], members: string[],
_links: Links,
_embedded: { _embedded: {
members: User[] members: Member[]
} }
}; };