Commit 5eab8015 authored by Oleksii Savran's avatar Oleksii Savran Committed by Pavlov Viacheslav
Browse files

Add partner

fixed ssr at EditPartnerPage
parent 84f8dc96
......@@ -2,7 +2,13 @@ import { push } from 'react-router-redux';
import { SubmissionError } from 'redux-form';
// Constants
import { DASHBOARD_GET_PARTNER, DASHBOARD_RECEIVE_PARTNER, DASHBOARD_RECEIVE_PARTNER_INSTITUTES, DASHBOARD_REMOVE_PARTNER_INSTITUTES } from 'partners/constants';
import {
DASHBOARD_GET_PARTNER,
DASHBOARD_RECEIVE_PARTNER,
DASHBOARD_RECEIVE_PARTNER_INSTITUTES,
DASHBOARD_REMOVE_PARTNER_INSTITUTES,
DASHBOARD_LOADING_PARTNER_INSTITUTES,
} from 'partners/constants';
// Model
import Partner from 'model/genesys/Partner';
......@@ -28,19 +34,13 @@ const removePartnerInstitutesFromState = (institutes: string[]): IReducerAction
type: DASHBOARD_REMOVE_PARTNER_INSTITUTES, payload: institutes,
});
const showPartner = (uuid: string) => (dispatch) => {
log('Navigating to Partner details');
dispatch(push(`/partners/${uuid}`));
};
export const savePartner = (partner: Partner) => (dispatch, getState) => {
const saveOrUpdate = partner.id && partner.version ? PartnerService.updatePartner : PartnerService.createPartner;
return saveOrUpdate(partner)
.then((saved) => {
dispatch(receivePartner(saved));
dispatch(showPartner(saved.uuid));
return saved;
}).catch((error) => {
log('Save error', error);
throw new SubmissionError({ name: 'Name already used', _error: error.error });
......@@ -78,14 +78,16 @@ export const loadPartner = (uuid: string) => (dispatch, getState) => {
};
export const loadPartnerInstitutes = (uuid: string) => (dispatch) => {
dispatch({ type: DASHBOARD_LOADING_PARTNER_INSTITUTES, payload: uuid });
return PartnerService.listInstitutes(uuid)
.then((institutes) => dispatch(receivePartnerInstitutes(institutes)));
};
export const addPartnerInstitutes = (uuid: string, instCodes: string[]) => (dispatch) => {
dispatch(receivePartnerInstitutes(null));
return PartnerService.addInstitutes(uuid, instCodes)
.then((partner) => {
dispatch(receivePartnerInstitutes(null));
dispatch(loadPartnerInstitutes(uuid));
return partner;
});
};
......
......@@ -5,7 +5,7 @@ import navigateTo from 'actions/navigation';
import { showSnackbar } from 'actions/snackbar';
// Constants
import {GET_PARTNER, RECEIVE_PARTNER, RECEIVE_PARTNERS, APPEND_PARTNERS, RECEIVE_PARTNER_INSTITUTES} from 'partners/constants';
import {GET_PARTNER, RECEIVE_PARTNER, RECEIVE_PARTNERS, APPEND_PARTNERS, RECEIVE_PARTNER_INSTITUTES, LOADING_PARTNER_INSTITUTES} from 'partners/constants';
// Model
......@@ -96,6 +96,7 @@ const loadMorePartners = (paged: FilteredPage<Vocabulary>) => (dispatch, getStat
};
export const loadPartnerInstitutes = (uuid: string) => (dispatch) => {
dispatch({ type: LOADING_PARTNER_INSTITUTES, payload: uuid });
return PartnerService.listInstitutes(uuid)
.then((institutes) => dispatch(receivePartnerInstitutes(institutes)));
};
......
......@@ -4,6 +4,7 @@ export const PARTNER_FORM = 'Form/Partner/PARTNER_FORM';
export const GET_PARTNER = 'App/Partner/GET_PARTNER';
export const RECEIVE_PARTNER = 'App/Partner/RECEIVE_PARTNER';
export const RECEIVE_PARTNER_INSTITUTES = 'App/Partner/RECEIVE_PARTNER_INSTITUTES';
export const LOADING_PARTNER_INSTITUTES = 'App/Partner/LOADING_PARTNER_INSTITUTES';
// export const FINISH_EDIT_PARTNER = 'App/Partner/FINISH_EDIT_PARTNER';
// export const DELETE_PARTNER = 'App/DELETE_PARTNER';
......@@ -23,4 +24,5 @@ export const DASHBOARD_RECEIVE_PARTNER_INSTITUTES = 'partner/dashboard/RECEIVE_P
export const DASHBOARD_REMOVE_PARTNER_INSTITUTES = 'partner/dashboard/REMOVE_PARTNER_INSTITUTES';
export const DASHBOARD_CREATE_PARTNER = 'partner/dashboard/CREATE_PARTNER';
export const DASHBOARD_RECEIVE_MY_PARTNERLIST = 'partner/dashboard/RECEIVE_MY_PARTNERLIST';
export const DASHBOARD_LOADING_PARTNER_INSTITUTES = 'App/Partner/DASHBOARD_LOADING_PARTNER_INSTITUTES';
......@@ -2,7 +2,16 @@ import update from 'immutability-helper';
import { IReducerAction } from 'model/common.model';
import Partner from 'model/genesys/Partner';
import { DASHBOARD_REMOVE_PARTNER_INSTITUTES, DASHBOARD_CREATE_PARTNER, DASHBOARD_GET_PARTNER, DASHBOARD_RECEIVE_PARTNER, DASHBOARD_RECEIVE_MY_PARTNERLIST, DASHBOARD_RECEIVE_PARTNER_INSTITUTES } from 'partners/constants';
import {
DASHBOARD_REMOVE_PARTNER_INSTITUTES,
DASHBOARD_CREATE_PARTNER,
DASHBOARD_GET_PARTNER,
DASHBOARD_RECEIVE_PARTNER,
DASHBOARD_RECEIVE_MY_PARTNERLIST,
DASHBOARD_RECEIVE_PARTNER_INSTITUTES,
DASHBOARD_LOADING_PARTNER_INSTITUTES,
} from 'partners/constants';
const INITIAL_STATE = {
myPartners: [], // contains list of user's Partners
......@@ -14,6 +23,12 @@ const INITIAL_STATE = {
export default function partner(state = INITIAL_STATE, action: IReducerAction = { type: '' }) {
switch (action.type) {
case DASHBOARD_LOADING_PARTNER_INSTITUTES: {
const { uuid } = action.payload;
return update(state, {
loadingInstitutes: { $set: uuid },
});
}
case DASHBOARD_GET_PARTNER: {
return update(state, {
loading: { $set: { uuid: action.payload } },
......@@ -36,8 +51,7 @@ export default function partner(state = INITIAL_STATE, action: IReducerAction =
case DASHBOARD_REMOVE_PARTNER_INSTITUTES: {
const instCodesToRemove = action.payload;
const newInstitutes = state.partnerInstitutes.content.filter((inst) => !instCodesToRemove.includes(inst));
const newInstitutes = state.partnerInstitutes.content.filter((inst) => !instCodesToRemove.includes(inst.code));
return update(state, {
partnerInstitutes: {
content: {$set: newInstitutes},
......
import update from 'immutability-helper';
import { IReducerAction } from 'model/common.model';
import { GET_PARTNER, RECEIVE_PARTNER, LIST_PARTNERS, RECEIVE_PARTNERS, APPEND_PARTNERS, RECEIVE_PARTNER_INSTITUTES } from 'partners/constants';
import { GET_PARTNER, RECEIVE_PARTNER, LIST_PARTNERS, RECEIVE_PARTNERS, APPEND_PARTNERS, RECEIVE_PARTNER_INSTITUTES, LOADING_PARTNER_INSTITUTES } from 'partners/constants';
const INITIAL_STATE = {
currentPartner: null,
......@@ -14,6 +14,12 @@ const INITIAL_STATE = {
export default function partner(state = INITIAL_STATE, action: IReducerAction = { type: '' }) {
switch (action.type) {
case LOADING_PARTNER_INSTITUTES: {
const { uuid } = action.payload;
return update(state, {
loadingInstitutes: { $set: uuid },
});
}
case GET_PARTNER: {
return update(state, {
loading: { $set: { uuid: action.payload } },
......@@ -35,6 +41,7 @@ export default function partner(state = INITIAL_STATE, action: IReducerAction =
}
case RECEIVE_PARTNER_INSTITUTES: {
return update(state, {
loadingInstitutes: { $set: null },
partnerInstitutes: { $set: action.payload },
});
}
......
......@@ -8,6 +8,7 @@ import { parse } from 'query-string';
import { filterCodeToUrl } from 'actions/filterCode';
import Partner from 'model/genesys/Partner';
import { applyFilters, loadMorePartners, updateRoute } from 'partners/actions/public';
import { createPartner } from 'partners/actions/dashboard';
import PagedLoader from 'ui/common/PagedLoader';
import Loading from 'ui/common/Loading';
......@@ -15,12 +16,15 @@ import PaginationComponent from 'ui/common/pagination';
import PartnerFilters from './c/Filters';
import PartnerCard from './c/PartnerCard';
import PrettyFilters from 'ui/common/filter/PrettyFilters';
import Authorize from 'ui/common/authorized/Authorize';
import PageLayout, { PageContents } from 'ui/layout/PageLayout';
import { ScrollToTopOnMount } from 'ui/common/page/scrollers';
import ContentHeader from 'ui/common/heading/ContentHeader';
import BrowsePageTemplate from 'ui/pages/_base/BrowsePage';
import Page from 'model/Page';
import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import ActionButton from 'ui/common/buttons/ActionButton';
class PartnerListPage extends BrowsePageTemplate<Partner> {
......@@ -37,7 +41,7 @@ class PartnerListPage extends BrowsePageTemplate<Partner> {
public render() {
const { paged, t, loadMoreData } = this.props;
const { paged, t, loadMoreData, createPartner } = this.props;
const stillLoading: boolean = (!paged || !paged.content);
return (
......@@ -46,6 +50,13 @@ class PartnerListPage extends BrowsePageTemplate<Partner> {
}>
<ScrollToTopOnMount/>
<ContentHeader title={ t('partners.public.p.browse.title') } subtitle={ t('partners.public.p.browse.subtitle') }/>
<Authorize role="ROLE_ADMINISTRATOR">
<ContentHeaderWithButton buttons={
<div>
<ActionButton title={ t('common:action.add', {what: t('partners.common.modelName') }) } action={ createPartner }/>
</div>
}/>
</Authorize>
<PaginationComponent
pageObj={ paged }
onSortChange={ this.onSortChange }
......@@ -83,6 +94,7 @@ const mapDispatchToProps = (dispatch) => bindActionCreators({
loadMoreData: loadMorePartners,
filterCodeToUrl,
updateRoute,
createPartner,
}, dispatch);
export default translate()((connect(mapStateToProps, mapDispatchToProps)(PartnerListPage)));
......@@ -77,7 +77,7 @@ class PartnerPage extends React.Component<IPartnerPageProps, any> {
}
public componentWillReceiveProps(nextProps) {
const {uuid, partner, loading, loadPartner, partnerInstitutes, loadPartnerInstitutes} = nextProps;
const {uuid, partner, loading, loadPartner, partnerInstitutes, loadPartnerInstitutes, loadingInstitutes} = nextProps;
if (uuid && (!partner || partner.uuid !== uuid)) {
if (!loading || loading.uuid !== uuid) {
......@@ -85,7 +85,9 @@ class PartnerPage extends React.Component<IPartnerPageProps, any> {
loadPartner(uuid);
}
if (uuid && partner && (!partnerInstitutes || partnerInstitutes.content && partnerInstitutes.content.length > 0 && partnerInstitutes.content[0].owner.uuid !== partner.uuid)) {
loadPartnerInstitutes(uuid);
if (!loadingInstitutes) {
loadPartnerInstitutes(uuid);
}
}
}
}
......@@ -172,6 +174,7 @@ const mapStateToProps = (state, ownProps) => ({
partner: state.partner.public.currentPartner,
partnerInstitutes: state.partner.public.partnerInstitutes,
loading: state.partner.public.loading,
loadingInstitutes: state.partner.public.loadingInstitutes,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
......
......@@ -3,6 +3,7 @@ import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { translate } from 'react-i18next';
import navigateTo from 'actions/navigation';
import {savePartner, loadPartner, loadPartnerInstitutes, removePartnerInstitute, addPartnerInstitutes } from 'partners/actions/editor';
import Partner from 'model/genesys/Partner';
import Page from 'model/Page';
......@@ -25,6 +26,8 @@ interface IPartnerEditPageProps extends React.ClassAttributes<any> {
loadPartnerInstitutes: any;
removePartnerInstitute: any;
savePartner: any;
navigateTo: any;
loadingInstitutes: any;
t: any;
}
......@@ -32,27 +35,31 @@ interface IPartnerEditPageProps extends React.ClassAttributes<any> {
class PartnerEditPage extends React.Component<IPartnerEditPageProps, any> {
protected static needs = [
({ params: { uuid } }) => loadPartner(uuid),
({ params: { uuid } }) => loadPartnerInstitutes(uuid),
({ params: { uuid } }) => uuid && loadPartner(uuid),
({ params: { uuid } }) => uuid && loadPartnerInstitutes(uuid),
];
public onSave = (partner) => {
const {savePartner, addPartnerInstitutes} = this.props;
const {savePartner, addPartnerInstitutes, navigateTo} = this.props;
addPartnerInstitutes(partner.uuid, partner.institutes.map((inst) => inst.code))
savePartner(partner)
.then((newPartner) => {
partner.version = newPartner.version;
savePartner(partner);
return partner.institutes && partner.institutes.length > 0 ?
addPartnerInstitutes(newPartner.uuid, partner.institutes.map((inst) => inst.code))
.then((partnerWithSavedInstitutes) => navigateTo(`/partners/${partnerWithSavedInstitutes.uuid}`))
: navigateTo(`/partners/${newPartner.uuid}`);
});
}
public componentWillMount() {
const {uuid, partner, loadPartner, partnerInstitutes, loadPartnerInstitutes} = this.props;
const {uuid, partner, loadPartner, partnerInstitutes, loadPartnerInstitutes, loadingInstitutes} = this.props;
if (uuid && ! partner) {
loadPartner(uuid);
}
if (uuid && (!partnerInstitutes || (partnerInstitutes.content && partnerInstitutes.content.length > 0 && partnerInstitutes.content[0].owner.uuid !== uuid))) {
loadPartnerInstitutes(uuid);
if (!loadingInstitutes) {
loadPartnerInstitutes(uuid);
}
}
}
......@@ -74,14 +81,14 @@ class PartnerEditPage extends React.Component<IPartnerEditPageProps, any> {
if (!partner) {
partner = new Partner();
}
const institutes = partnerInstitutes && partnerInstitutes.content;
const institutes = partnerInstitutes && partnerInstitutes.content || [];
return (
<Grid item xs={ 12 }>
<Paper className="p-20">
{ partner && partnerInstitutes ?
{ partner ?
<PartnerForm
initialValues={ {...partner, institutes} }
initialInstitutes={ institutes }
onSubmit={ this.onSave }
removePartnerInstitute={ removePartnerInstitute }
t={ t }
......@@ -100,9 +107,11 @@ const mapStateToProps = (state, ownProps) => ({
loading: state.partner.dashboard.loading,
partner: state.partner.dashboard.currentPartner,
partnerInstitutes: state.partner.dashboard.partnerInstitutes,
loadingInstitutes: state.partner.dashboard.loadingInstitutes,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
navigateTo,
loadPartner,
loadPartnerInstitutes,
addPartnerInstitutes,
......
......@@ -35,11 +35,10 @@ class PartnerForm extends React.Component<any, void> {
}
private onInstituteDelete = (item) => {
const {initialValues, removePartnerInstitute} = this.props;
const {initialValues, removePartnerInstitute, initialInstitutes} = this.props;
const index = Number(item.substr(item.indexOf('[') + 1, item.length - item.lastIndexOf(']')));
if (initialValues && initialValues.institutes && initialValues.institutes[index]) {
log(`Removing institute ${initialValues.code} from partner`);
removePartnerInstitute(initialValues.uuid, initialValues.institutes[index].code);
if (initialValues && initialInstitutes && initialInstitutes[index]) {
removePartnerInstitute(initialValues.uuid, initialInstitutes[index].code);
}
}
......
......@@ -95,7 +95,6 @@ class LanguageList extends React.Component<ILanguageListProps, any> {
public render() {
const { classes, i18n, color, location } = this.props;
console.log(location);
let currentLang = i18n.language;
languages.some((item, i, arr) => {
if (item.short === i18n.language) {
......
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