Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimised code and added support for image type object closes #1467

Merged
merged 1 commit into from
Nov 28, 2024
Merged
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
57 changes: 22 additions & 35 deletions src/components/Accordion/CalendarAccordion/CalendarAccordion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ function CalendarAccordion(props) {

const calendarContentLanguage = currentCalendarData?.contentLanguage;

const processEntities = (entities) => {
if (!entities) return [];
return entities.map((v) => ({
...v,
image: Array.isArray(v?.image)
? v?.image.find((image) => image?.isMain) || null
: typeof v?.image === 'object' && v?.image !== null
? v?.image
: null,
}));
};

const updateList = (entities, setter) => {
setter(treeEntitiesOption(processEntities(entities), user, calendarContentLanguage, sourceOptions.CMS));
};

const organizationPersonSearch = (value, type) => {
const queryMap = {
organizers: query,
Expand All @@ -95,25 +111,10 @@ function CalendarAccordion(props) {
getEntities({ searchKey: value, classes: decodeURIComponent(currentQuery.toString()), calendarId })
.unwrap()
.then((response) => {
if (type == 'organizers') {
setOrganizersList(
treeEntitiesOption(
response?.map((v) => ({ ...v, image: v?.image?.find((image) => image?.isMain) })),
user,
calendarContentLanguage,
sourceOptions.CMS,
),
);
}
if (type == 'people') {
setPeopleList(
treeEntitiesOption(
response?.map((v) => ({ ...v, image: v?.image?.find((image) => image?.isMain) })),
user,
calendarContentLanguage,
sourceOptions.CMS,
),
);
if (type === 'organizers') {
updateList(response, setOrganizersList);
} else if (type === 'people') {
updateList(response, setPeopleList);
}
})
.catch((error) => console.log(error));
Expand All @@ -131,27 +132,13 @@ function CalendarAccordion(props) {

useEffect(() => {
if (initialEntities && currentCalendarData) {
setOrganizersList(
treeEntitiesOption(
initialEntities?.map((v) => ({ ...v, image: v?.image?.find((image) => image?.isMain) })),
user,
calendarContentLanguage,
sourceOptions.CMS,
),
);
updateList(initialEntities, setOrganizersList);
}
}, [initialEntityLoading, currentCalendarData]);

useEffect(() => {
if (initialPersonEntities && currentCalendarData) {
setPeopleList(
treeEntitiesOption(
initialPersonEntities?.map((v) => ({ ...v, image: v?.image?.find((image) => image?.isMain) })),
user,
calendarContentLanguage,
sourceOptions.CMS,
),
);
updateList(initialPersonEntities, setPeopleList);
}
}, [initialPersonEntityLoading, currentCalendarData]);

Expand Down