Skip to content

Commit

Permalink
fix: 🐛 Tiki search
Browse files Browse the repository at this point in the history
  • Loading branch information
leephan2k1 committed Oct 20, 2022
1 parent 344c16a commit f4cd899
Showing 1 changed file with 36 additions and 56 deletions.
92 changes: 36 additions & 56 deletions src/components/shared/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,13 1,12 @@
import { useAtomValue, useSetAtom } from 'jotai';
import { ChangeEvent, memo, useEffect, useRef, useState } from 'react';
import useSWR from 'swr';
import { ProductPreview } from 'types';
import { Market, ProductPreview } from 'types';
import { useDebounce, useEffectOnce, useEventListener } from 'usehooks-ts';
import marketSearch from '~/atoms/marketSearch';
import searchResult from '~/atoms/searchResult';
import { MARKET_URL } from '~/constants';
import useAxiosClient from '~/services/axiosClient';
import { handleTikiSearch } from '~/utils/handleMarketSearch';

import { MagnifyingGlassIcon, XMarkIcon } from '@heroicons/react/24/outline';

Expand All @@ -20,7 19,9 @@ function SearchInput({ styles, focusOnMount }: SearchInputProps) {
const [value, setValue] = useState('');
const market = useAtomValue(marketSearch);
const setResult = useSetAtom(searchResult);
const axiosClient = useAxiosClient(MARKET_URL(market));
const axiosClient = useAxiosClient(
MARKET_URL('e-commerce-server' as Market),
);
const inputRef = useRef<HTMLInputElement | null>(null);
const debouncedValue = useDebounce<string>(value, 400);

Expand All @@ -36,63 37,42 @@ function SearchInput({ styles, focusOnMount }: SearchInputProps) {

if (!keyword) return [];

switch (market) {
case 'tiki':
const { data: tikiData } = await axiosClient.get(
`/api/v2/products?limit=10&aggregations=2&q=${encodeURIComponent(
keyword,
)}`,
{
headers: {
origin: 'https://tiki.vn',
Referer: 'https://tiki.vn/',
Host: 'tiki.vn',
},
},
);

const tikiResult = handleTikiSearch(tikiData?.data);

return tikiResult?.length ? tikiResult : [];

default:
const { data } = await axiosClient.get(`/products/search`, {
params: {
market: market,
keyword: keyword,
},
});

if (!data || !data?.products) {
return [];
}

if (market !== 'all' && data?.products?.length) {
return data?.products
.map((item: any) => {
return { ...item, market };
})
.slice(0, 10);
}
const { data } = await axiosClient.get(`/products/search`, {
params: {
market: market,
keyword: keyword,
},
});

if (!data || !data?.products) {
return [];
}

if (market !== 'all' && data?.products?.length) {
return data?.products
.map((item: any) => {
return { ...item, market };
})
.slice(0, 10);
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
let result = [];

for (const [key, value] of Object.entries(data?.products)) {
if (Array.isArray(value))
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
let result = [];

for (const [key, value] of Object.entries(data?.products)) {
if (Array.isArray(value))
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
result = result.concat([
...value?.slice(0, 10).map((item: any) => ({
...item,
market: key,
})),
]);
}

return result;
result = result.concat([
...value?.slice(0, 10).map((item: any) => ({
...item,
market: key,
})),
]);
}

return result;
},
);

Expand Down

0 comments on commit f4cd899

Please sign in to comment.