Skip to content

Commit

Permalink
Code review issues
Browse files Browse the repository at this point in the history
Remove dialog-context, pass title/setTitle around
Remove commented-out stars and count
Stop re-shuffling partners every time you view details
Use Object.keys instead of Reflect.ownKeys
  • Loading branch information
RoyEJohnson committed Dec 3, 2024
1 parent b97bcf8 commit b9df3aa
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 46 deletions.
4 changes: 1 addition & 3 deletions src/app/pages/partners/partner-details/partner-context.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import buildContext from '~/components/jsx-helpers/build-context';
import useReviews from '~/models/reviews';
import useDialogContext from '../results/dialog-context';

// eslint-disable-next-line complexity
function useContextValue({id: partnerId, model}) {
function useContextValue({id: partnerId, model, title, setTitle}) {
const [ratings, postRating] = useReviews(partnerId);
const {title, setTitle} = useDialogContext();

if (!ratings) {
return {};
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/partners/partner-details/partner-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ function PartnerDetailsOrInfoRequestForm({model}) {
);
}

export default function PartnerDetailsWrapper({detailData: {id, ...model}}) {
export default function PartnerDetailsWrapper({detailData: {id, ...model}, title, setTitle}) {
return (
<PartnerContextProvider contextValueParameters={{id, model}}>
<PartnerContextProvider contextValueParameters={{id, model, title, setTitle}}>
<PartnerDetailsOrInfoRequestForm model={model} />
</PartnerContextProvider>
);
Expand Down
18 changes: 0 additions & 18 deletions src/app/pages/partners/results/dialog-context.tsx

This file was deleted.

6 changes: 0 additions & 6 deletions src/app/pages/partners/results/result-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ function ResultCard({entry}: {entry: PartnerEntry}) {
<div key={value}>{value}</div>
))}
</div>
{/*
<StarsAndCount
rating={summary.rating}
count={summary.count}
showNumber />
*/}
</a>
);
}
Expand Down
13 changes: 8 additions & 5 deletions src/app/pages/partners/results/results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ function filterBy(
// eslint-disable-next-line complexity
function useFilteredEntries(entries: PartnerEntry[]) {
const {books, types, advanced, sort, resultCount} = useSearchContext();
const unfilteredResults = React.useMemo(
() => shuffle(entries),
[entries]
);
const finalResult = React.useMemo(() => {
let result = shuffle(entries);
let result = filterByBooks(unfilteredResults, books);

result = filterByBooks(result, books);
result = filterByType(result, types);

if (advanced.value.length > 0) {
Expand All @@ -146,7 +149,7 @@ function useFilteredEntries(entries: PartnerEntry[]) {
}

return result;
}, [entries, advanced, books, types]);
}, [unfilteredResults, advanced, books, types]);

resultCount.setValue(finalResult.length);

Expand All @@ -173,7 +176,7 @@ function useFilteredEntries(entries: PartnerEntry[]) {
}

function advancedFilterKeys(partnerEntry: PartnerData) {
return (Reflect.ownKeys(partnerEntry) as Array<keyof PartnerData>).filter(
return (Object.keys(partnerEntry) as Array<keyof PartnerData>).filter(
(k) => ([false, true] as unknown[]).includes(partnerEntry[k])
);
}
Expand Down Expand Up @@ -222,7 +225,7 @@ function resultEntry(pd: PartnerData) {
ratingCount: pd.rating_count,
partnershipLevel: pd.partnership_level,
yearsAsPartner: pd.partner_anniversary_date
? differenceInYears(Date.now(), pd.partner_anniversary_date)
? differenceInYears(Date.now(), new Date(pd.partner_anniversary_date))
: null
};
}
Expand Down
29 changes: 17 additions & 12 deletions src/app/pages/partners/results/selected-partner-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import PartnerDetails from '../partner-details/partner-details';
import Dialog from '~/components/dialog/dialog';
import useDialogContext, {DialogContextProvider} from './dialog-context';
import {useNavigate, useLocation} from 'react-router-dom';
import type {LinkTexts, PartnerEntry} from './results';

Expand All @@ -14,13 +13,25 @@ export default function SelectedPartnerDialog({
}) {
const [partner, closePartner] = usePartnerFromLocation(entries);
const detailData = {...partner, ...linkTexts};
const [title, setTitle] = React.useState('');

// Reset between partners
React.useEffect(() => setTitle(''), [partner]);

return (
<DialogContextProvider contextValueParameters={partner}>
<DialogInContext isOpen={Boolean(partner)} onPutAway={closePartner}>
{Boolean(partner) && <PartnerDetails detailData={detailData} />}
</DialogInContext>
</DialogContextProvider>
<Dialog
title={title}
isOpen={Boolean(partner)}
onPutAway={closePartner}
>
{Boolean(partner) && (
<PartnerDetails
detailData={detailData}
title={title}
setTitle={setTitle}
/>
)}
</Dialog>
);
}

Expand All @@ -41,9 +52,3 @@ function usePartnerFromLocation(

return [partner, closePartner];
}

function DialogInContext(dialogProps: Parameters<typeof Dialog>[0]) {
const {title} = useDialogContext();

return <Dialog title={title} {...dialogProps} />;
}

0 comments on commit b9df3aa

Please sign in to comment.