Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 191 additions & 97 deletions src/actions/sponsor-pages-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
} from "openstack-uicore-foundation/lib/utils/actions";
import T from "i18n-react/dist/i18n-react";
import moment from "moment-timezone";
import { getAccessTokenSafely } from "../utils/methods";
import {
getAccessTokenSafely,
normalizeSelectAllField
} from "../utils/methods";
import { snackbarErrorHandler, snackbarSuccessHandler } from "./base-actions";
import {
DEFAULT_CURRENT_PAGE,
Expand All @@ -37,8 +40,10 @@ export const RESET_EDIT_PAGE = "RESET_EDIT_PAGE";

export const REQUEST_SPONSOR_MANAGED_PAGES = "REQUEST_SPONSOR_MANAGED_PAGES";
export const RECEIVE_SPONSOR_MANAGED_PAGES = "RECEIVE_SPONSOR_MANAGED_PAGES";
export const RECEIVE_SPONSOR_MANAGED_PAGE = "RECEIVE_SPONSOR_MANAGED_PAGE";
export const SPONSOR_MANAGED_PAGE_ADDED = "SPONSOR_MANAGED_PAGE_ADDED";
export const SPONSOR_MANAGED_PAGE_DELETED = "SPONSOR_MANAGED_PAGE_DELETED";
export const SPONSOR_MANAGED_PAGE_UPDATED = "SPONSOR_MANAGED_PAGE_UPDATED";

export const REQUEST_SPONSOR_CUSTOMIZED_PAGES =
"REQUEST_SPONSOR_CUSTOMIZED_PAGES";
Expand Down Expand Up @@ -111,53 +116,75 @@ export const getSponsorManagedPages =
orderDir = DEFAULT_ORDER_DIR,
hideArchived = false
) =>
async (dispatch, getState) => {
const { currentSummitState, currentSponsorState } = getState();
const { currentSummit } = currentSummitState;
const {
entity: { id: sponsorId }
} = currentSponsorState;
const accessToken = await getAccessTokenSafely();
const summitTZ = currentSummit.time_zone.name;
const filter = [];

dispatch(startLoading());

if (term) {
const escapedTerm = escapeFilterValue(term);
filter.push(`name=@${escapedTerm},code=@${escapedTerm}`);
}

const params = {
page,
fields: "id,code,name,kind,modules_count,allowed_add_ons,assigned_type",
per_page: perPage,
access_token: accessToken
};

if (hideArchived) filter.push("is_archived==0");

if (filter.length > 0) {
params["filter[]"] = filter;
}

// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "" : "-";
params.order = `${orderDirSign}${order}`;
}

return getRequest(
createAction(REQUEST_SPONSOR_MANAGED_PAGES),
createAction(RECEIVE_SPONSOR_MANAGED_PAGES),
`${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/sponsors/${sponsorId}/managed-pages`,
snackbarErrorHandler,
{ order, orderDir, page, perPage, term, hideArchived, summitTZ }
)(params)(dispatch).finally(() => {
dispatch(stopLoading());
});
async (dispatch, getState) => {
const { currentSummitState, currentSponsorState } = getState();
const { currentSummit } = currentSummitState;
const {
entity: { id: sponsorId }
} = currentSponsorState;
const accessToken = await getAccessTokenSafely();
const summitTZ = currentSummit.time_zone.name;
const filter = [];

dispatch(startLoading());

if (term) {
const escapedTerm = escapeFilterValue(term);
filter.push(`name=@${escapedTerm},code=@${escapedTerm}`);
}

const params = {
page,
fields: "id,code,name,kind,modules_count,allowed_add_ons,assigned_type",
per_page: perPage,
access_token: accessToken
};

if (hideArchived) filter.push("is_archived==0");

if (filter.length > 0) {
params["filter[]"] = filter;
}

// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "" : "-";
params.order = `${orderDirSign}${order}`;
}

return getRequest(
createAction(REQUEST_SPONSOR_MANAGED_PAGES),
createAction(RECEIVE_SPONSOR_MANAGED_PAGES),
`${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/sponsors/${sponsorId}/managed-pages`,
snackbarErrorHandler,
{ order, orderDir, page, perPage, term, hideArchived, summitTZ }
)(params)(dispatch).finally(() => {
dispatch(stopLoading());
});
};

export const getSponsorManagedPage = (pageId) => async (dispatch, getState) => {
const { currentSummitState } = getState();
const { currentSummit } = currentSummitState;
const accessToken = await getAccessTokenSafely();

dispatch(startLoading());

const params = {
access_token: accessToken,
expand: "modules"
};

return getRequest(
null,
createAction(RECEIVE_SPONSOR_MANAGED_PAGE),
`${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/show-pages/${pageId}`,
snackbarErrorHandler
)(params)(dispatch).finally(() => {
dispatch(stopLoading());
});
};

export const saveSponsorManagedPage =
(entity) => async (dispatch, getState) => {
const { currentSummitState, currentSponsorState } = getState();
Expand All @@ -169,13 +196,37 @@ export const saveSponsorManagedPage =

dispatch(startLoading());

const normalizedEntity = normalizeSponsorManagedPage(entity);

const params = {
access_token: accessToken,
fields: "id,code,name,kind,modules_count,allowed_add_ons"
};

if (entity.id) {
const normalizedEntity = normalizeSponsorManagedPageToCustomize(entity);

return putRequest(
null,
createAction(SPONSOR_CUSTOMIZED_PAGE_UPDATED),
`${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/sponsors/${sponsorId}/managed-pages/${entity.id}`,
normalizedEntity,
snackbarErrorHandler,
entity
)(params)(dispatch)
.then(() => {
dispatch(
snackbarSuccessHandler({
title: T.translate("general.success"),
html: T.translate("edit_sponsor.pages_tab.managed_page_saved")
})
);
})
.finally(() => {
dispatch(stopLoading());
});
}

const normalizedEntity = normalizeSponsorManagedPage(entity);

return postRequest(
null,
createAction(SPONSOR_MANAGED_PAGE_ADDED),
Expand All @@ -187,7 +238,7 @@ export const saveSponsorManagedPage =
dispatch(
snackbarSuccessHandler({
title: T.translate("general.success"),
html: T.translate("edit_sponsor.pages_tab.managed_page_saved")
html: T.translate("edit_sponsor.pages_tab.managed_page_created")
})
);
})
Expand Down Expand Up @@ -233,7 +284,7 @@ const normalizeSponsorManagedPage = (entity) => {
show_page_ids: entity.pages
};

if (entity.add_ons.includes("all")) {
if (normalizedEntity.allowed_add_ons.includes("all")) {
normalizedEntity.apply_to_all_add_ons = true;
normalizedEntity.allowed_add_ons = [];
} else {
Expand All @@ -243,6 +294,49 @@ const normalizeSponsorManagedPage = (entity) => {

return normalizedEntity;
};

const normalizeSponsorManagedPageToCustomize = (entity) => {
const normalizedEntity = {
...entity,
...normalizeSelectAllField(
entity.allowed_add_ons,
"apply_to_all_add_ons",
"allowed_add_ons"
)
};

normalizedEntity.modules = entity.modules.map((module) => {
const normalizedModule = { ...module };

if (module.kind === PAGES_MODULE_KINDS.MEDIA && module.upload_deadline) {
normalizedModule.upload_deadline = moment
.utc(module.upload_deadline)
.unix();
}

if (module.kind === PAGES_MODULE_KINDS.MEDIA && module.file_type_id) {
normalizedModule.file_type_id =
module.file_type_id?.value || module.file_type_id;
}

if (module.kind === PAGES_MODULE_KINDS.DOCUMENT && module.file) {
normalizedModule.file = module.file[0] || null;
}

delete normalizedModule._tempId;

return normalizedModule;
});

delete normalizedEntity.page_ptr_id;
delete normalizedEntity.sponsorship_types;
delete normalizedEntity.summit_id;
delete normalizedEntity.template_id;
delete normalizedEntity.modules_count;
delete normalizedEntity.id;

return normalizedEntity;
};
/* ************************************************************************ */
/* CUSTOMIZED PAGES */
/* ************************************************************************ */
Expand All @@ -256,55 +350,55 @@ export const getSponsorCustomizedPages =
orderDir = DEFAULT_ORDER_DIR,
hideArchived = false
) =>
async (dispatch, getState) => {
const { currentSummitState, currentSponsorState } = getState();
const { currentSummit } = currentSummitState;
const {
entity: { id: sponsorId }
} = currentSponsorState;
const accessToken = await getAccessTokenSafely();
const summitTZ = currentSummit.time_zone.name;
const filter = [];

dispatch(startLoading());

if (term) {
const escapedTerm = escapeFilterValue(term);
filter.push(`name=@${escapedTerm},code=@${escapedTerm}`);
}

const params = {
page,
fields:
"id,code,name,allowed_add_ons,is_archived,modules,allowed_add_ons.type,allowed_add_ons.name,allowed_add_ons.id",
expand: "allowed_add_ons",
per_page: perPage,
access_token: accessToken
};

if (hideArchived) filter.push("is_archived==0");

if (filter.length > 0) {
params["filter[]"] = filter;
}

// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "" : "-";
params.order = `${orderDirSign}${order}`;
}

return getRequest(
createAction(REQUEST_SPONSOR_CUSTOMIZED_PAGES),
createAction(RECEIVE_SPONSOR_CUSTOMIZED_PAGES),
`${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/sponsors/${sponsorId}/sponsor-pages`,
snackbarErrorHandler,
{ order, orderDir, page, perPage, term, hideArchived, summitTZ }
)(params)(dispatch).finally(() => {
dispatch(stopLoading());
});
async (dispatch, getState) => {
const { currentSummitState, currentSponsorState } = getState();
const { currentSummit } = currentSummitState;
const {
entity: { id: sponsorId }
} = currentSponsorState;
const accessToken = await getAccessTokenSafely();
const summitTZ = currentSummit.time_zone.name;
const filter = [];

dispatch(startLoading());

if (term) {
const escapedTerm = escapeFilterValue(term);
filter.push(`name=@${escapedTerm},code=@${escapedTerm}`);
}

const params = {
page,
fields:
"id,code,name,allowed_add_ons,is_archived,modules,allowed_add_ons.type,allowed_add_ons.name,allowed_add_ons.id",
expand: "allowed_add_ons",
per_page: perPage,
access_token: accessToken
};

if (hideArchived) filter.push("is_archived==0");

if (filter.length > 0) {
params["filter[]"] = filter;
}

// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "" : "-";
params.order = `${orderDirSign}${order}`;
}

return getRequest(
createAction(REQUEST_SPONSOR_CUSTOMIZED_PAGES),
createAction(RECEIVE_SPONSOR_CUSTOMIZED_PAGES),
`${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/sponsors/${sponsorId}/sponsor-pages`,
snackbarErrorHandler,
{ order, orderDir, page, perPage, term, hideArchived, summitTZ }
)(params)(dispatch).finally(() => {
dispatch(stopLoading());
});
};

export const getSponsorCustomizedPage =
(pageId) => async (dispatch, getState) => {
const { currentSummitState, currentSponsorState } = getState();
Expand Down
13 changes: 8 additions & 5 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2493,11 +2493,13 @@
"no_pages": "There are no pages that match the criteria",
"add_page_using_template": "Add Page Template",
"add_selected_page_template": "Add Selected Page Template",
"managed_page_saved": "Managed Page updated successfully.",
"custom_page_saved": "Custom Sponsor Page saved",
"custom_page_created": "Custom Sponsor Page created",
"title_customize": "Customize Managed Page",
"customized_page_archived": "Customized Sponsor Page successfully archived.",
"customized_page_unarchived": "Customized Sponsor Page successfully unarchived."
"customized_page_unarchived": "Customized Sponsor Page successfully unarchived.",
"custom_page_saved": "Custom Sponsor Page saved successfully",
"custom_page_created": "Custom Sponsor Page created successfully",
"managed_page_saved": "Sponsor Managed Page saved successfully",
"managed_page_created": "Sponsor Managed Page created successfully"
},
"cart_tab": {
"new_form": "New Form",
Expand Down Expand Up @@ -4046,7 +4048,8 @@
"sponsorship_type": "Always apply to"
},
"page_crud": {
"title": "Create New Page",
"title_create": "Create New Page",
"title_edit": "Edit Page",
"add_info": "Add Info",
"add_doc": "Add Document Download",
"add_media": "Add Media Request",
Expand Down
Loading
Loading