fix(API/index.ts): change parameter types in searchComponent function to allow null values for query, page, limit, status, and tags

refactor(StorePage/index.tsx): rename filteredCategories state variable to filteredTags for better semantics
feat(StorePage/index.tsx): add functionality to filter components by tags and update search results accordingly
This commit is contained in:
cristhianzl 2023-10-30 22:41:58 -03:00
commit a7120259e0
2 changed files with 48 additions and 39 deletions

View file

@ -644,11 +644,11 @@ export async function getComponent(component_id: string) {
} }
export async function searchComponent( export async function searchComponent(
query: string, query: string | null,
page?: number, page?: number | null,
limit?: number, limit?: number | null,
status?: string, status?: string | null,
tags?: [string] tags?: string[]
) { ) {
try { try {
let url = `${BASE_URL_API}store/search/`; let url = `${BASE_URL_API}store/search/`;

View file

@ -37,8 +37,7 @@ export default function StorePage(): JSX.Element {
}, []); }, []);
const [data, setData] = useState<storeComponent[]>([]); const [data, setData] = useState<storeComponent[]>([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [search, setSearch] = useState(false); const [filteredCategories, setFilterCategories] = useState<any[]>([]);
const [filteredCategories, setFilteredCategories] = useState(new Set());
const [inputText, setInputText] = useState<string>(""); const [inputText, setInputText] = useState<string>("");
const [searchData, setSearchData] = useState(data); const [searchData, setSearchData] = useState(data);
const { setErrorData } = useContext(alertContext); const { setErrorData } = useContext(alertContext);
@ -47,24 +46,17 @@ export default function StorePage(): JSX.Element {
const [index, setPageIndex] = useState(1); const [index, setPageIndex] = useState(1);
const [errorApiKey, setErrorApiKey] = useState(false); const [errorApiKey, setErrorApiKey] = useState(false);
const { setSavedFlows, savedFlows } = useContext(StoreContext); const { setSavedFlows, savedFlows } = useContext(StoreContext);
const [tags, setTags] = useState<string[]>([]); const [tags, setTags] = useState<{ id: string; name: string }[]>([]);
const tagListId = useRef<{ id: string; name: string }[]>([]); const tagListId = useRef<{ id: string; name: string }[]>([]);
const [renderPagination, setRenderPagination] = useState( const [renderPagination, setRenderPagination] = useState(false);
searchData?.length > 0 && !loading && !search
);
useEffect(() => { useEffect(() => {
getStoreTags().then((res) => { getStoreTags().then((res) => {
tagListId.current = res; tagListId.current = res;
let tags = res.map((tag) => tag.name); setTags(res);
setTags(tags);
}); });
}, []); }, []);
useEffect(() => {
setRenderPagination(searchData?.length > 0 && !loading && !search);
}, [loading, search]);
async function getSavedComponents() { async function getSavedComponents() {
setLoading(true); setLoading(true);
const result = await getStoreSavedComponents(); const result = await getStoreSavedComponents();
@ -89,6 +81,7 @@ export default function StorePage(): JSX.Element {
const handleGetComponents = () => { const handleGetComponents = () => {
setLoading(true); setLoading(true);
setRenderPagination(true);
getStoreComponents(index - 1, size) getStoreComponents(index - 1, size)
.then((res) => { .then((res) => {
setSearchData(res); setSearchData(res);
@ -109,17 +102,15 @@ export default function StorePage(): JSX.Element {
const handleSearch = (inputText: string) => { const handleSearch = (inputText: string) => {
if (inputText === "") { if (inputText === "") {
handleGetComponents(); handleGetComponents();
setSearch(false);
return; return;
} }
setSearch(true);
setLoading(true); setLoading(true);
searchComponent(inputText).then( searchComponent(inputText).then(
(res) => { (res) => {
setSearchData(res); setSearchData(res);
setData(res); setData(res);
setRenderPagination(false);
setLoading(false); setLoading(false);
setRenderPagination(false);
}, },
(error) => { (error) => {
setLoading(false); setLoading(false);
@ -129,6 +120,7 @@ export default function StorePage(): JSX.Element {
function handleChangePagination(pageIndex: number, pageSize: number) { function handleChangePagination(pageIndex: number, pageSize: number) {
setLoading(true); setLoading(true);
setRenderPagination(true);
getStoreComponents(pageIndex, pageSize) getStoreComponents(pageIndex, pageSize)
.then((res) => { .then((res) => {
setData(res); setData(res);
@ -136,7 +128,6 @@ export default function StorePage(): JSX.Element {
setPageIndex(pageIndex); setPageIndex(pageIndex);
setPageSize(pageSize); setPageSize(pageSize);
setLoading(false); setLoading(false);
setRenderPagination(true);
}) })
.catch((err) => { .catch((err) => {
setSearchData([]); setSearchData([]);
@ -148,6 +139,25 @@ export default function StorePage(): JSX.Element {
}); });
} }
function handleFilterByTags(filterArray) {
if (filterArray.length === 0) {
handleGetComponents();
return;
}
setRenderPagination(false);
searchComponent(null, 1, 10000, null, filterArray).then(
(res) => {
setSearchData(res);
setData(res);
setLoading(false);
},
(error) => {
setLoading(false);
setSearchData([]);
}
);
}
return ( return (
<> <>
<Header /> <Header />
@ -207,7 +217,6 @@ export default function StorePage(): JSX.Element {
<div className="flex w-[13%] items-center justify-center gap-3 text-sm"> <div className="flex w-[13%] items-center justify-center gap-3 text-sm">
<Select <Select
onValueChange={(value) => { onValueChange={(value) => {
setFilteredCategories(new Set());
if (value === "Flow") { if (value === "Flow") {
setSearchData(data.filter((f) => f.is_component === false)); setSearchData(data.filter((f) => f.is_component === false));
setRenderPagination(false); setRenderPagination(false);
@ -224,9 +233,9 @@ export default function StorePage(): JSX.Element {
<SelectValue placeholder="Both" /> <SelectValue placeholder="Both" />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
<SelectItem value="">Both</SelectItem>
<SelectItem value="Flow">Flows</SelectItem> <SelectItem value="Flow">Flows</SelectItem>
<SelectItem value="Component">Components</SelectItem> <SelectItem value="Component">Components</SelectItem>
<SelectItem value="">Both</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
@ -236,28 +245,26 @@ export default function StorePage(): JSX.Element {
tags.map((i, idx) => ( tags.map((i, idx) => (
<Badge <Badge
onClick={() => { onClick={() => {
filteredCategories.has(i) const index = filteredCategories?.indexOf(i.id);
? setFilteredCategories((old) => { const copyFilterArray = cloneDeep(filteredCategories);
let newFilteredCategories = cloneDeep(old); if (index === -1) {
newFilteredCategories.delete(i); copyFilterArray.push(i.id);
return newFilteredCategories; } else {
}) copyFilterArray.splice(index, 1);
: setFilteredCategories((old) => { }
let newFilteredCategories = cloneDeep(old); setFilterCategories(copyFilterArray);
newFilteredCategories.add(i); handleFilterByTags(copyFilterArray);
return newFilteredCategories;
});
}} }}
variant="gray" variant="gray"
size="md" size="md"
className={cn( className={cn(
"cursor-pointer border-none", "cursor-pointer border-none",
filteredCategories.has(i) filteredCategories.some((category) => category === i.id)
? "bg-beta-foreground text-background hover:bg-beta-foreground" ? "bg-beta-foreground text-background hover:bg-beta-foreground"
: "" : ""
)} )}
> >
{i} {i.name}
</Badge> </Badge>
))} ))}
</div> </div>
@ -272,8 +279,10 @@ export default function StorePage(): JSX.Element {
searchData searchData
.filter( .filter(
(f) => (f) =>
Array.from(filteredCategories).length === 0 || filteredCategories?.length === 0 ||
filteredCategories.has(f.is_component) filteredCategories.some(
(category) => category === f.is_component
)
) )
.map((item, idx) => ( .map((item, idx) => (
<MarketCardComponent key={idx} data={item} /> <MarketCardComponent key={idx} data={item} />
@ -282,7 +291,7 @@ export default function StorePage(): JSX.Element {
</div> </div>
</div> </div>
{renderPagination && ( {!loading && renderPagination && (
<div className="relative my-3"> <div className="relative my-3">
<PaginatorComponent <PaginatorComponent
storeComponent={true} storeComponent={true}