Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/Broken Api URL #1390

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
12 changes: 12 additions & 0 deletions modules/marketplace/component/filter/helper/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const formatToNigerianNaira = (num: number | string): string => {
// Convert input to a number
const parsedNum = typeof num === 'string' ? parseFloat(num) : num;

// Format to Nigerian Naira currency format
const formatter = new Intl.NumberFormat('en-NG', {
style: 'currency',
currency: 'NGN',
});

return formatter.format(parsedNum);
};
54 changes: 26 additions & 28 deletions modules/marketplace/component/filter/hooks/useCategory.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
import http from '@modules/marketplace/http';
import { ProductList } from '@modules/marketplace/types/filter-types';
import axios, { isAxiosError } from 'axios';
import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';

export type CategoryType = {
name: string;
subcategories: { name?: string }[];
};

const useCategory = () => {
const [categories, setCategories] = useState<CategoryType[]>([]);
const [products, setProducts] = useState<ProductList[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
setLoading(true);
getCategories();
}, []);
async function getCategories() {
try {
const { data } = await axios.get<{ data: CategoryType[] }>('/category-name/');
const {
isLoading: category_loading,
error: category_error,
data: category_data,
} = useQuery({
queryKey: ['categoryNameData'],
queryFn: async () => await http.get<{ data: CategoryType[] }>('/category-name'),
});

setCategories(data.data ? data.data : []);
await getProducts();
} catch (error) {
if (error instanceof isAxiosError) {
console.log(error);
}
} finally {
setLoading(false);
}
}
const {
isLoading: product_loading,
error: product_error,
data: product_data,
} = useQuery({
queryKey: ['productListData'],
queryFn: async () => await http.get<{ data: ProductList[] }>('/product-list'),
});

async function getProducts() {
const { data } = await axios.get<{ data: ProductList[] }>('product-list/');
setProducts(data.data);
}

return { categories, loading, products };
return {
categories: category_data?.data.data || [],
products: product_data?.data.data || [],
p_loading: product_loading,
c_loading: category_loading,
p_error: product_error,
c_error: category_error
};
};

export default useCategory;
8 changes: 5 additions & 3 deletions modules/marketplace/component/filter/search-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import FilterSection from './filter-section';
import Button from '@ui/Button';
import { CancelIcon } from './icons';
import useSearchFilter from './hooks/useSearchFilter';
import { formatToNigerianNaira } from '../../../../helpers/formatCurrency';
import useCategory from './hooks/useCategory';
import { ProductList } from '@modules/marketplace/types/filter-types';
import Modal from '../../../../components/ui/Modal';
import { formatToNigerianNaira } from './helper/utils';

const SearchFilter = ({ isOpen, toggle }: { isOpen?: boolean; toggle: () => void }) => {
const { resetFilter, handleSearch, loading } = useSearchFilter();
const { categories, loading: isLoading, products } = useCategory();
const { categories, c_loading: isLoading, p_loading, products } = useCategory();
const sub_categories = categories.flatMap((category) => category.subcategories).map((sub) => sub?.name);
function getLowestAndHighestPrices(products: ProductList[]) {
const sortedPrices = products
Expand All @@ -29,7 +29,9 @@ const SearchFilter = ({ isOpen, toggle }: { isOpen?: boolean; toggle: () => void
}
const results = getLowestAndHighestPrices(products);
const prices = [...results.lowest, ...results.highest];
const uniquePrices = Array.from(new Set(prices)).map((price) => formatToNigerianNaira(price));
const uniquePrices = Array.from(new Set(prices)).map((price) => {
return !isNaN(price) ? formatToNigerianNaira(price) : 0
});
const discount_price = [5, 10, 20, 30, 40, 50];

return (
Expand Down