Skip to content
83 changes: 47 additions & 36 deletions src/actions/sponsor-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,48 +611,54 @@ 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());

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 =
Expand Down Expand Up @@ -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;
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/CustomTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const theme = createTheme(MuiBaseCustomTheme, {
...(ownerState.size === "medium" && {
fontSize: "14px",
lineHeight: "20px",
padding: "8px 12px"
padding: "8px 12px",
height: "36px"
}),
...(ownerState.size === "large" && {
fontSize: "16px",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from "react";
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import SponsorHeader from "../sponsor-header";

const buildSponsor = (overrides = {}) => ({
id: 5,
is_published: false,
company: {
name: "Acme Corp",
city: "Austin",
state: "TX",
country: "USA",
contact_email: "contact@acme.com"
},
...overrides
});

const deferred = () => {
let resolve;
const promise = new Promise((res) => {
resolve = res;
});
return { promise, resolve };
};

describe("SponsorHeader", () => {
it("renders sponsor info and the current publication state", () => {
const sponsor = buildSponsor({ is_published: true });

render(<SponsorHeader sponsor={sponsor} onSave={jest.fn()} />);

expect(screen.getByText("Acme Corp")).toBeInTheDocument();
expect(screen.getByText("edit_sponsor.is_published")).toBeInTheDocument();
expect(screen.getByRole("checkbox")).toBeChecked();
});

it("saves the toggled value, disables controls while pending, blocks a second submit, and re-enables on resolve", async () => {
const { promise, resolve } = deferred();
const onSave = jest.fn(() => promise);
const sponsor = buildSponsor({ id: 5, is_published: false });

render(<SponsorHeader sponsor={sponsor} onSave={onSave} />);

await userEvent.click(screen.getByRole("checkbox"));
const saveButton = screen.getByRole("button", { name: "general.save" });
await userEvent.click(saveButton);

expect(onSave).toHaveBeenCalledWith({ id: 5, is_published: true });
expect(saveButton).toBeDisabled();
expect(screen.getByRole("checkbox")).toBeDisabled();

// the button is disabled now, so a second click can't invoke the handler again
fireEvent.click(saveButton);
expect(onSave).toHaveBeenCalledTimes(1);

resolve();
await waitFor(() => expect(saveButton).toBeEnabled());
expect(screen.getByRole("checkbox")).toBeEnabled();
});

it("re-enables controls and does not blow up when the save fails", async () => {
const onSave = jest.fn().mockRejectedValue(new Error("save failed"));
const sponsor = buildSponsor({ is_published: false });

render(<SponsorHeader sponsor={sponsor} onSave={onSave} />);

const saveButton = screen.getByRole("button", { name: "general.save" });
await userEvent.click(saveButton);

await waitFor(() => expect(saveButton).toBeEnabled());
expect(screen.getByRole("checkbox")).toBeEnabled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
removeTierFromSponsor,
resetSponsorExtraQuestionForm,
saveAddonsToSponsorship,
saveSponsor,
saveSponsorExtraQuestion,
saveSponsorExtraQuestionValue,
setSelectedSponsorship,
Expand All @@ -52,15 +53,16 @@ const SponsorGeneralForm = ({
saveSponsorExtraQuestionValue,
resetSponsorExtraQuestionForm,
deleteExtraQuestion,
updateExtraQuestionOrder
updateExtraQuestionOrder,
saveSponsor
}) => {
const handleSponsorshipPaginate = (page, perPage, order, orderDir) => {
getSponsorSponsorships(sponsor.id, page, perPage, order, orderDir);
};

return (
<Box sx={{ mt: 2 }}>
<SponsorHeader sponsor={sponsor} />
<SponsorHeader sponsor={sponsor} onSave={saveSponsor} />
<Sponsorship
sponsor={sponsor}
summitId={currentSummit.id}
Expand Down Expand Up @@ -117,5 +119,6 @@ export default connect(mapStateToProps, {
saveSponsorExtraQuestionValue,
resetSponsorExtraQuestionForm,
deleteExtraQuestion,
updateExtraQuestionOrder
updateExtraQuestionOrder,
saveSponsor
})(SponsorGeneralForm);
Loading
Loading