diff --git a/src/actions/sponsor-actions.js b/src/actions/sponsor-actions.js index b32b68b40..13d6614fa 100644 --- a/src/actions/sponsor-actions.js +++ b/src/actions/sponsor-actions.js @@ -611,7 +611,11 @@ export const saveSponsor = (entity) => async (dispatch, getState) => { const { currentSummit } = currentSummitState; const params = { - access_token: accessToken + access_token: accessToken, + expand: + "company,members,sponsorships,sponsorships.type,sponsorships.type.type,featured_event,extra_questions,extra_questions.values,lead_report_setting", + fields: + "featured_event.id,featured_event.title,sponsorships.id,sponsorships.type.id,sponsorships.type.type.id,sponsorships.type.type.name" }; dispatch(startLoading()); @@ -619,40 +623,42 @@ export const saveSponsor = (entity) => async (dispatch, getState) => { const normalizedEntity = normalizeSponsor(entity); if (entity.id) { - putRequest( + return putRequest( createAction(UPDATE_SPONSOR), createAction(SPONSOR_UPDATED), `${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/sponsors/${entity.id}`, normalizedEntity, - authErrorHandler, + snackbarErrorHandler, entity - )(params)(dispatch).then(() => { - dispatch(showSuccessMessage(T.translate("edit_sponsor.sponsor_saved"))); - }); - } else { - const success_message = { - title: T.translate("general.done"), - html: T.translate("edit_sponsor.sponsor_created"), - type: "success" - }; + )(params)(dispatch) + .then(() => { + dispatch( + snackbarSuccessHandler({ + title: T.translate("general.success"), + html: T.translate("edit_sponsor.sponsor_saved") + }) + ); + }) + .finally(() => dispatch(stopLoading())); + } - postRequest( - createAction(UPDATE_SPONSOR), - createAction(SPONSOR_ADDED), - `${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/sponsors`, - normalizedEntity, - authErrorHandler, - entity - )(params)(dispatch).then((payload) => { + return postRequest( + createAction(UPDATE_SPONSOR), + createAction(SPONSOR_ADDED), + `${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/sponsors`, + normalizedEntity, + snackbarErrorHandler, + entity + )(params)(dispatch) + .then(() => { dispatch( - showMessage(success_message, () => { - history.push( - `/app/summits/${currentSummit.id}/sponsors/${payload.response.id}` - ); + snackbarSuccessHandler({ + title: T.translate("general.success"), + html: T.translate("edit_sponsor.sponsor_created") }) ); - }); - } + }) + .finally(() => dispatch(stopLoading())); }; export const addMemberToSponsor = @@ -746,16 +752,21 @@ export const updateSponsorOrder = const normalizeSponsor = (entity) => { const normalizedEntity = { ...entity }; - normalizedEntity.company_id = normalizedEntity.company?.id || 0; - normalizedEntity.sponsorship_id = normalizedEntity.sponsorship?.id || 0; - normalizedEntity.featured_event_id = - normalizedEntity.featured_event && normalizedEntity.featured_event.id - ? normalizedEntity.featured_event.id - : 0; - - delete normalizedEntity.featured_event; - delete normalizedEntity.company; - delete normalizedEntity.sponsorship; + if (normalizedEntity.hasOwnProperty("company")) { + normalizedEntity.company_id = normalizedEntity.company?.id || 0; + delete normalizedEntity.company; + } + + if (normalizedEntity.hasOwnProperty("sponsorship")) { + normalizedEntity.sponsorship_id = normalizedEntity.sponsorship?.id || 0; + delete normalizedEntity.sponsorship; + } + + if (normalizedEntity.hasOwnProperty("featured_event")) { + normalizedEntity.featured_event_id = + normalizedEntity.featured_event?.id || 0; + delete normalizedEntity.featured_event; + } return normalizedEntity; }; diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/index.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/index.js index ee86dd44f..9ad9868a9 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/index.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/index.js @@ -28,6 +28,7 @@ import { removeTierFromSponsor, resetSponsorExtraQuestionForm, saveAddonsToSponsorship, + saveSponsor, saveSponsorExtraQuestion, saveSponsorExtraQuestionValue, setSelectedSponsorship, @@ -52,7 +53,8 @@ const SponsorGeneralForm = ({ saveSponsorExtraQuestionValue, resetSponsorExtraQuestionForm, deleteExtraQuestion, - updateExtraQuestionOrder + updateExtraQuestionOrder, + saveSponsor }) => { const handleSponsorshipPaginate = (page, perPage, order, orderDir) => { getSponsorSponsorships(sponsor.id, page, perPage, order, orderDir); @@ -60,7 +62,7 @@ const SponsorGeneralForm = ({ return ( - + ( - - - - - {T.translate("edit_sponsor.general_information")} - +const SponsorHeader = ({ sponsor, onSave }) => { + const [isPublished, setIsPublished] = useState(sponsor.is_published); + const [isSaving, setIsSaving] = useState(false); + + useEffect(() => { + setIsPublished(sponsor.is_published); + }, [sponsor.id, sponsor.is_published]); + + const handlePublishSave = () => { + const entity = { id: sponsor.id, is_published: isPublished }; + setIsSaving(true); + Promise.resolve(onSave(entity)) + .catch(() => {}) + .finally(() => setIsSaving(false)); + }; + + return ( + + + + + {T.translate("edit_sponsor.general_information")} + + - - - - - - {T.translate("edit_sponsor.sponsor_name")} - + + + + + {T.translate("edit_sponsor.sponsor_name")} + + + {sponsor.company?.name} - {sponsor.company?.name} - - - - - - {T.translate("edit_sponsor.sponsor_address")} - + + + + + {T.translate("edit_sponsor.sponsor_address")} + + + + {[ + sponsor.company?.city, + sponsor.company?.state, + sponsor.company?.country + ] + // filter empty or undefined fields + .filter(Boolean) + .join(" - ")} + {sponsor.company?.contact_email && ( + <> +   + + {sponsor.company.contact_email} + + + )} + - - {[ - sponsor.company?.city, - sponsor.company?.state, - sponsor.company?.country - ] - // filter empty or undefined fields - .filter(Boolean) - .join(" - ")} - {sponsor.company?.contact_email && ( - <> -   - - {sponsor.company.contact_email} - - - )} + + + + + + + setIsPublished(!isPublished)} + inputProps={{ "aria-labelledby": "sponsor-is-published-label" }} + /> + + - - -); + + ); +}; export default SponsorHeader; diff --git a/src/reducers/sponsors/sponsor-reducer.js b/src/reducers/sponsors/sponsor-reducer.js index 9736a35db..a26dd8705 100644 --- a/src/reducers/sponsors/sponsor-reducer.js +++ b/src/reducers/sponsors/sponsor-reducer.js @@ -153,8 +153,9 @@ const sponsorReducer = (state = DEFAULT_STATE, action) => { return { ...state, entity: { ...DEFAULT_ENTITY }, errors: {} }; case UPDATE_SPONSOR: - return { ...state, entity: { ...payload }, errors: {} }; + return { ...state, entity: { ...state.entity, ...payload }, errors: {} }; case SPONSOR_ADDED: + case SPONSOR_UPDATED: case RECEIVE_SPONSOR: { const entity = { ...payload.response }; @@ -175,8 +176,6 @@ const sponsorReducer = (state = DEFAULT_STATE, action) => { entity: { ...state.entity, ...entity } }; } - case SPONSOR_UPDATED: - return state; case MEMBER_ADDED_TO_SPONSOR: { const { member } = payload; return {