Skip to content

Commit

Permalink
show error when remove group failed
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Sep 26, 2017
1 parent 9f44a47 commit 580b952
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/client/components/GroupCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GroupCreator extends Component {
value="Create LDAP group"
/>
</form>
{error ? <div style={{marginTop: 8}} className="alert alert-danger">{error}</div> : null }
{error ? <div style={{marginTop: 20}} className="alert alert-danger">{error}</div> : null }
</div>
</div>
);
Expand Down
32 changes: 23 additions & 9 deletions src/client/reducers/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,21 @@ const dbReducer = (state = initialState, action) => {
return Object.assign({}, state, {userGroups: newGroupList});
}
case `${REMOVE_GROUP}_FULFILLED`: {
if (action.payload.error) {
return getNewStateOnGroupError(state, action);
}
const newGroupList = state.userGroups.filter(group => group.name !== action.meta);
return Object.assign({}, state, {userGroups: newGroupList});
}
case `${UPDATE_GROUP}_FULFILLED`: {
const index = state.userGroups.findIndex(group => group.name === action.meta);
if (index === -1) {
throw new Error('should not happen');
}
const newGroupList = state.userGroups.slice();
if (action.payload.error) {
newGroupList[index] = Object.assign({}, newGroupList[index], {error: action.payload.error, success: null});
return Object.assign({}, state, {
userGroups: newGroupList
});
return getNewStateOnGroupError(state, action);
} else {
if (action.payload.name !== action.meta) {
throw new Error('should not happen');
}
const index = getGroupIndex(state.userGroups, action);
const newGroupList = state.userGroups.slice();
newGroupList[index] = action.payload;
newGroupList[index].success = 'Group sucessfully updated';
newGroupList[index].error = null;
Expand All @@ -79,6 +76,23 @@ const dbReducer = (state = initialState, action) => {
}
};

function getGroupIndex(userGroups, action) {
const index = userGroups.findIndex(group => group.name === action.meta);
if (index === -1) {
throw new Error('should not happen');
}
return index;
}

function getNewStateOnGroupError(state, action) {
const index = getGroupIndex(state.userGroups, action);
const newGroupList = state.userGroups.slice();
newGroupList[index] = Object.assign({}, newGroupList[index], {error: action.payload.error, success: null});
return Object.assign({}, state, {
userGroups: newGroupList
});
}

export default dbReducer;

function sortByName(group1, group2) {
Expand Down

0 comments on commit 580b952

Please sign in to comment.