Skip to content

Commit

Permalink
Refactor advanced search results attribute processing
Browse files Browse the repository at this point in the history
Simplify and optimize the handling of base and joined attributes in AdvancedSearchResultsPage
- Use functional programming techniques to process attributes
- Reduce complexity of attribute mapping and filtering
- Improve code readability by using more concise array methods
  • Loading branch information
syucream committed Feb 2, 2025
1 parent 6d957e2 commit 24101c1
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions frontend/src/pages/AdvancedSearchResultsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,39 +278,33 @@ export const AdvancedSearchResultsPage: FC = () => {
// attrinfo and its related joined attrinfo
attrInfo
.map((info) => {
const results = [];

results.push({
const base = {
key: info.name,
val: {
filterKey:
info.filterKey ||
AdvancedSearchResultAttrInfoFilterKeyEnum.CLEARED,
keyword: info.keyword || "",
},
});
};

// weave joined attributes into the defaultAttrFilterArray
joinAttrs.forEach((join) => {
if (join.name === info.name) {
join.attrinfo.forEach((joinedInfo) => {
results.push({
key: `${join.name}.${joinedInfo.name}`,
val: {
filterKey:
joinedInfo.filterKey ||
AdvancedSearchResultAttrInfoFilterKeyEnum.CLEARED,
keyword: joinedInfo.keyword || "",
baseAttrname: join.name,
joinedAttrname: joinedInfo.name,
},
});
});
}
});
return results;
const joined = joinAttrs
.filter((join) => join.name === info.name)
.flatMap((join) =>
join.attrinfo.map((joinedInfo) => ({
key: `${join.name}.${joinedInfo.name}`,
val: {
filterKey:
joinedInfo.filterKey ||
AdvancedSearchResultAttrInfoFilterKeyEnum.CLEARED,
keyword: joinedInfo.keyword || "",
baseAttrname: join.name,
joinedAttrname: joinedInfo.name,
},
}))
);

// this convert array to dict using reduce()
return [base, ...joined];
})
.flat()
.reduce((a, x) => ({ ...a, [x.key]: x.val }), {})
Expand Down

0 comments on commit 24101c1

Please sign in to comment.