🐛 fix(API/index.ts): add missing type annotations and handle null values in getStoreComponents and searchComponent functions
✨ feat(API/index.ts): add support for additional query parameters in getStoreComponents and searchComponent functions to enable filtering and sorting of results 🐛 fix(StorePage/index.tsx): handle null values and update searchData and totalRowsCount in StorePage component to prevent errors and ensure accurate data display ✨ feat(StorePage/index.tsx): add support for sorting by popularity and alphabetical order in handleOrderPage function in StorePage component
This commit is contained in:
parent
f9b57f90a7
commit
225d6da419
2 changed files with 48 additions and 27 deletions
|
|
@ -593,11 +593,36 @@ export async function getFlowsStore(): Promise<AxiosResponse<FlowType[]>> {
|
||||||
export async function getStoreComponents(
|
export async function getStoreComponents(
|
||||||
page: number = 1,
|
page: number = 1,
|
||||||
limit: number = 10,
|
limit: number = 10,
|
||||||
is_component?: boolean | null
|
is_component?: boolean | null,
|
||||||
|
sort?: string | null,
|
||||||
|
tags?: string[] | null,
|
||||||
|
filter_by_user?: boolean | null,
|
||||||
|
status?: string | null,
|
||||||
|
search?: string | null
|
||||||
): Promise<StoreComponentResponse | undefined> {
|
): Promise<StoreComponentResponse | undefined> {
|
||||||
try {
|
try {
|
||||||
let url = `${BASE_URL_API}store/components/`;
|
let url = `${BASE_URL_API}store/components/`;
|
||||||
const queryParams: any = [];
|
const queryParams: any = [];
|
||||||
|
if (search !== undefined && search !== null) {
|
||||||
|
queryParams.push(`search=${search}`);
|
||||||
|
}
|
||||||
|
if (status !== undefined && status !== null) {
|
||||||
|
queryParams.push(`status=${status}`);
|
||||||
|
}
|
||||||
|
if (tags !== undefined && tags !== null) {
|
||||||
|
queryParams.push(`tags=${tags}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sort !== undefined && sort !== null) {
|
||||||
|
queryParams.push(`sort=${sort}`);
|
||||||
|
} else {
|
||||||
|
queryParams.push(`sort=-count(liked_by)`); // default sort
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter_by_user !== undefined && filter_by_user !== null) {
|
||||||
|
queryParams.push(`filter_by_user=${filter_by_user}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (page !== undefined) {
|
if (page !== undefined) {
|
||||||
queryParams.push(`page=${page}`);
|
queryParams.push(`page=${page}`);
|
||||||
}
|
}
|
||||||
|
|
@ -670,23 +695,23 @@ export async function searchComponent(
|
||||||
limit?: number | null,
|
limit?: number | null,
|
||||||
status?: string | null,
|
status?: string | null,
|
||||||
tags?: string[]
|
tags?: string[]
|
||||||
) {
|
): Promise<StoreComponentResponse | undefined> {
|
||||||
try {
|
try {
|
||||||
let url = `${BASE_URL_API}store/search/`;
|
let url = `${BASE_URL_API}store/components/`;
|
||||||
const queryParams: any = [];
|
const queryParams: any = [];
|
||||||
if (query !== undefined) {
|
if (query !== undefined && query !== null) {
|
||||||
queryParams.push(`query=${query}`);
|
queryParams.push(`search=${query}`);
|
||||||
}
|
}
|
||||||
if (page !== undefined) {
|
if (page !== undefined && page !== null) {
|
||||||
queryParams.push(`page=${page}`);
|
queryParams.push(`page=${page}`);
|
||||||
}
|
}
|
||||||
if (limit !== undefined) {
|
if (limit !== undefined && limit !== null) {
|
||||||
queryParams.push(`limit=${limit}`);
|
queryParams.push(`limit=${limit}`);
|
||||||
}
|
}
|
||||||
if (status !== undefined) {
|
if (status !== undefined && status !== null) {
|
||||||
queryParams.push(`status=${status}`);
|
queryParams.push(`status=${status}`);
|
||||||
}
|
}
|
||||||
if (tags !== undefined) {
|
if (tags !== undefined && tags !== null) {
|
||||||
queryParams.push(`tags=${tags}`);
|
queryParams.push(`tags=${tags}`);
|
||||||
}
|
}
|
||||||
if (queryParams.length > 0) {
|
if (queryParams.length > 0) {
|
||||||
|
|
|
||||||
|
|
@ -109,10 +109,11 @@ export default function StorePage(): JSX.Element {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
searchComponent(inputText).then(
|
searchComponent(inputText).then(
|
||||||
(res) => {
|
(res) => {
|
||||||
setSearchData(res);
|
setSearchData(res?.results ?? []);
|
||||||
|
setTotalRowsCount(Number(res?.count ?? 0));
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRenderPagination(false);
|
setRenderPagination(false);
|
||||||
setTotalRowsCount(res.length);
|
setTotalRowsCount(Number(res?.count ?? 0));
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
@ -125,7 +126,7 @@ export default function StorePage(): JSX.Element {
|
||||||
setRenderPagination(true);
|
setRenderPagination(true);
|
||||||
getStoreComponents(pageIndex, pageSize, filterComponent.current)
|
getStoreComponents(pageIndex, pageSize, filterComponent.current)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
setSearchData(res);
|
setSearchData(res?.results ?? []);
|
||||||
setPageIndex(pageIndex);
|
setPageIndex(pageIndex);
|
||||||
setPageSize(pageSize);
|
setPageSize(pageSize);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
@ -148,9 +149,9 @@ export default function StorePage(): JSX.Element {
|
||||||
setRenderPagination(false);
|
setRenderPagination(false);
|
||||||
searchComponent(null, 1, 10000, null, filterArray).then(
|
searchComponent(null, 1, 10000, null, filterArray).then(
|
||||||
(res) => {
|
(res) => {
|
||||||
setSearchData(res);
|
setSearchData(res?.results ?? []);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setTotalRowsCount(res.length);
|
setTotalRowsCount(Number(res?.count ?? 0));
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
@ -173,17 +174,12 @@ export default function StorePage(): JSX.Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleOrderPage = (e) => {
|
const handleOrderPage = (e) => {
|
||||||
let sortedData = cloneDeep(searchData);
|
let sort;
|
||||||
|
|
||||||
if (e === "Popular") {
|
if (e === "Popular") {
|
||||||
sortedData = sortedData.sort(
|
sort = "-count(liked_by)";
|
||||||
(a, b) => Number(b.liked_by_count) - Number(a.liked_by_count)
|
|
||||||
);
|
|
||||||
} else if (e === "Alphabetical") {
|
} else if (e === "Alphabetical") {
|
||||||
sortedData = sortedData.sort((a, b) => a.name.localeCompare(b.name));
|
sort = "name";
|
||||||
}
|
}
|
||||||
|
|
||||||
setSearchData([...sortedData]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -290,13 +286,13 @@ export default function StorePage(): JSX.Element {
|
||||||
|
|
||||||
<div className="flex items-center gap-2 px-2">
|
<div className="flex items-center gap-2 px-2">
|
||||||
{!loading &&
|
{!loading &&
|
||||||
tags.map((i, idx) => (
|
tags.map((tag, idx) => (
|
||||||
<Badge
|
<Badge
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const index = filteredCategories?.indexOf(i.id);
|
const index = filteredCategories?.indexOf(tag.name);
|
||||||
const copyFilterArray = cloneDeep(filteredCategories);
|
const copyFilterArray = cloneDeep(filteredCategories);
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
copyFilterArray.push(i.id);
|
copyFilterArray.push(tag.name);
|
||||||
} else {
|
} else {
|
||||||
copyFilterArray.splice(index, 1);
|
copyFilterArray.splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
@ -307,12 +303,12 @@ export default function StorePage(): JSX.Element {
|
||||||
size="sq"
|
size="sq"
|
||||||
className={cn(
|
className={cn(
|
||||||
"cursor-pointer",
|
"cursor-pointer",
|
||||||
filteredCategories.some((category) => category === i.id)
|
filteredCategories.some((category) => category === tag.name)
|
||||||
? "bg-beta-foreground text-background hover:bg-beta-foreground"
|
? "bg-beta-foreground text-background hover:bg-beta-foreground"
|
||||||
: ""
|
: ""
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{i.name}
|
{tag.name}
|
||||||
</Badge>
|
</Badge>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue