Skip to content

Commit

Permalink
fix: normalize comparison query ids
Browse files Browse the repository at this point in the history
  • Loading branch information
allohamora committed Mar 18, 2022
1 parent 66b66f2 commit cf5b6d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/views/Comparison/ComparisonContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const ComparisonContent: FC<Props> = ({ pizzasIds, removePizzas, isReady,
};

const shareHandler = async () => {
const searchParams = new URLSearchParams([['isUrl', '1'], ...pizzasIds.map((id) => [id, '1'])]);
const searchParams = new URLSearchParams([['isExternal', '1'], ...pizzasIds.map((id) => ['ids', id])]);
const query = searchParams.toString();

await navigator.clipboard.writeText(join(basePath, `${language}/comparison?${query}`));
Expand Down
10 changes: 5 additions & 5 deletions src/views/Comparison/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import { ComparisonContent } from './ComparisonContent';
export const Comparison: FC = () => {
const router = useRouter();
const {
query: { isUrl, ...query },
query: { isExternal, ids: queryIds },
} = router;
const comparison = useComparison();
const {
config: { language },
} = useConfig();

const queryPizzasIds = Object.keys(query);
const isUrlVariant = isUrl === '1';
const queryPizzasIds = (typeof queryIds === 'string' ? [queryIds] : queryIds) ?? [];
const isExternalVariant = isExternal === '1';

const urlRemovePizzas = (...ids: string[]) => {
const newIds = queryPizzasIds.filter((id) => !ids.includes(id));
const urlSearchParams = new URLSearchParams([['isUrl', '1'], ...newIds.map((id) => [id, '1'])]);
const urlSearchParams = new URLSearchParams([['isExternal', '1'], ...newIds.map((id) => ['ids', id])]);

router.replace(`${language}/comparison?${urlSearchParams.toString()}`);
};

if (isUrlVariant) {
if (isExternalVariant) {
return <ComparisonContent pizzasIds={queryPizzasIds} removePizzas={urlRemovePizzas} isReady type="external" />;
}

Expand Down

0 comments on commit cf5b6d6

Please sign in to comment.