Commit 06d05a8e authored by Oleksii Savran's avatar Oleksii Savran Committed by Viacheslav Pavlov
Browse files

Bug: removing institute from partner

updated actions and reducers
parent a6867ad4
import { push } from 'react-router-redux';
// Actions
import {createApiCaller, createPureApiCaller} from 'actions/ApiCall';
import { removeInstitutesPublic, addInstitutesPublic } from 'partners/actions/public';
// Constants
import {
......@@ -31,8 +32,8 @@ const apiUpdatePartner = createApiCaller(PartnerService.updatePartner, DASHBOARD
const apiDeletePartner = createPureApiCaller(PartnerService.deletePartner);
const apiLoadPartner = createApiCaller(PartnerService.getPartner, DASHBOARD_RECEIVE_PARTNER);
const apiListPartnerInstitutes = createApiCaller(PartnerService.listInstitutes, DASHBOARD_RECEIVE_PARTNER_INSTITUTES);
const apiAddPartnerInstitutes = createApiCaller(PartnerService.addInstitutes, DASHBOARD_RECEIVE_PARTNER_INSTITUTES);
const apiRemovePartnerInstitutes = createApiCaller(PartnerService.removeInstitutes, DASHBOARD_REMOVE_PARTNER_INSTITUTES);
const apiAddPartnerInstitutes = createPureApiCaller(PartnerService.addInstitutes);
const apiRemovePartnerInstitutes = createPureApiCaller(PartnerService.removeInstitutes);
export const savePartner = (partner: Partner) => (dispatch, getState) => {
const saveOrUpdate = partner.id && partner.version ? apiUpdatePartner : apiSavePartner;
......@@ -62,15 +63,30 @@ export const loadPartnerInstitutes = (uuid: string) => (dispatch) => {
};
export const addPartnerInstitutes = (uuid: string, instCodes: string[]) => (dispatch) => {
return dispatch(apiAddPartnerInstitutes(uuid, instCodes));
return dispatch(apiAddPartnerInstitutes(uuid, instCodes))
.then((partner) => {
dispatch(loadPartnerInstitutes(uuid))
.then((institutes) => {
dispatch(addInstitutesPublic(institutes.content, partner.uuid));
});
return partner;
});
};
export const removePartnerInstitutes = (partnerUuid: string, instCodes: string[]) => (dispatch) => {
export const removePartnerInstitutes = (partnerUuid: string, instCodes: string[]) => (dispatch, getState) => {
return dispatch(apiRemovePartnerInstitutes(partnerUuid, instCodes))
.then((partner) => dispatch(receivePartner(ApiCall.success(partner))));
.then((partner) => {
dispatch(receivePartner(ApiCall.success(partner)));
dispatch(removeInstitutesFromState(instCodes));
dispatch(removeInstitutesPublic(instCodes, partner.uuid));
});
};
export const removePartnerInstitute = (partnerUuid: string, instCode: string) => (dispatch) => {
return dispatch(removePartnerInstitutes(partnerUuid, [instCode]));
};
const removeInstitutesFromState = (instCodes: string[]): IReducerAction => ({
type: DASHBOARD_REMOVE_PARTNER_INSTITUTES, payload: {instCodes},
});
......@@ -5,7 +5,13 @@ import navigateTo from 'actions/navigation';
import {createApiCaller} from 'actions/ApiCall';
// Constants
import {RECEIVE_PARTNER, APPEND_PARTNERS, RECEIVE_PARTNER_INSTITUTES } from 'partners/constants';
import {
RECEIVE_PARTNER,
APPEND_PARTNERS,
RECEIVE_PARTNER_INSTITUTES,
REMOVE_PARTNER_INSTITUTES,
ADD_PARTNER_INSTITUTES,
} from 'partners/constants';
// Model
......@@ -14,6 +20,7 @@ import Partner from 'model/genesys/Partner';
import PartnerFilter from 'model/genesys/PartnerFilter';
import FilteredPage, { IPageRequest } from 'model/FilteredPage';
import Vocabulary from 'model/vocabulary/Vocabulary';
import { IReducerAction } from 'model/common.model';
// Service
import PartnerService from 'service/catalog/PartnerService';
......@@ -81,3 +88,11 @@ export const applyFilters = (filters: string | PartnerFilter, page: IPageRequest
dispatch(updateRoute(paged));
});
};
export const removeInstitutesPublic = (instCodes: string[], uuid: string): IReducerAction => ({
type: REMOVE_PARTNER_INSTITUTES, payload: {instCodes, uuid},
});
export const addInstitutesPublic = (institutes, uuid: string): IReducerAction => ({
type: ADD_PARTNER_INSTITUTES, payload: {institutes, uuid},
});
......@@ -18,3 +18,5 @@ export const DASHBOARD_REMOVE_PARTNER_INSTITUTES = 'partner/dashboard/REMOVE_PAR
export const DASHBOARD_CREATE_PARTNER = 'partner/dashboard/CREATE_PARTNER';
export const DASHBOARD_RECEIVE_MY_PARTNERLIST = 'partner/dashboard/RECEIVE_MY_PARTNERLIST';
export const REMOVE_PARTNER_INSTITUTES = 'App/Partner/REMOVE_PARTNER_INSTITUTES';
export const ADD_PARTNER_INSTITUTES = 'App/Partner/ADD_PARTNER_INSTITUTES';
......@@ -44,8 +44,8 @@ export default function partner(state = INITIAL_STATE, action: IReducerAction =
}
case DASHBOARD_REMOVE_PARTNER_INSTITUTES: {
const {apiCall} = action.payload;
const newInstitutes = state.partnerInstitutes.data.content.filter((inst) => !apiCall.data.includes(inst.code));
const {instCodes} = action.payload;
const newInstitutes = state.partnerInstitutes.data.content.filter((inst) => !instCodes.includes(inst.code));
return update(state, {
partnerInstitutes: {
data: {
......
......@@ -2,7 +2,13 @@ import update from 'immutability-helper';
import { IReducerAction } from 'model/common.model';
import FilteredPage from 'model/FilteredPage';
import {RECEIVE_PARTNER, APPEND_PARTNERS, RECEIVE_PARTNER_INSTITUTES} from 'partners/constants';
import {
RECEIVE_PARTNER,
APPEND_PARTNERS,
RECEIVE_PARTNER_INSTITUTES,
REMOVE_PARTNER_INSTITUTES,
ADD_PARTNER_INSTITUTES,
} from 'partners/constants';
const INITIAL_STATE = {
currentPartner: null,
......@@ -42,6 +48,37 @@ export default function partner(state = INITIAL_STATE, action: IReducerAction =
});
}
case REMOVE_PARTNER_INSTITUTES: {
const {instCodes, uuid} = action.payload;
if (state.partnerInstitutes && state.partnerInstitutes.data && state.partnerInstitutes.data.content && state.currentPartner && state.currentPartner.data && state.currentPartner.data.uuid === uuid) {
const newInstitutes = state.partnerInstitutes.data.content.filter((inst) => !instCodes.includes(inst.code));
return update(state, {
partnerInstitutes: {
data: {
content: {$set: newInstitutes},
},
},
});
} else {
return state;
}
}
case ADD_PARTNER_INSTITUTES: {
const {institutes, uuid} = action.payload;
if (state.currentPartner && state.currentPartner.data && state.currentPartner.data.uuid === uuid) {
return update(state, {
partnerInstitutes: {
data: {
content: {$set: institutes},
},
},
});
} else {
return state;
}
}
default:
return state;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment