Last change
on this file since a2e5735 was a2e5735, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 23 months ago |
Final Version
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | import axios from "axios";
|
---|
2 | import React, { useEffect, useReducer } from "react";
|
---|
3 | import { Link } from "react-router-dom";
|
---|
4 |
|
---|
5 | const reducer = (state, action) => {
|
---|
6 | switch (action.type) {
|
---|
7 | case "FETCH_REQUEST":
|
---|
8 | return { ...state, loading: true };
|
---|
9 | case "FETCH_SUCCESS":
|
---|
10 | return {
|
---|
11 | ...state,
|
---|
12 | getCategories: action.payload,
|
---|
13 | loading: false,
|
---|
14 | };
|
---|
15 | case "FETCH_FAIL":
|
---|
16 | return { ...state, loading: false, error: action.payload };
|
---|
17 | default:
|
---|
18 | return state;
|
---|
19 | }
|
---|
20 | };
|
---|
21 |
|
---|
22 | function SubCategoryMenu(props) {
|
---|
23 | const [{ loading, getCategories }, dispatch] = useReducer(reducer, {
|
---|
24 | loading: true,
|
---|
25 | error: "",
|
---|
26 | });
|
---|
27 |
|
---|
28 | useEffect(() => {
|
---|
29 | const fetchData = async () => {
|
---|
30 | try {
|
---|
31 | dispatch({ type: "FETCH_REQUEST" });
|
---|
32 | const { data } = await axios.get(
|
---|
33 | `/api/category/getSubCategory?subCategory=${props.subCategory}`
|
---|
34 | );
|
---|
35 | dispatch({ type: "FETCH_SUCCESS", payload: data });
|
---|
36 | } catch (err) {
|
---|
37 | dispatch({ type: "FETCH_FAIL", payload: err });
|
---|
38 | }
|
---|
39 | };
|
---|
40 | fetchData();
|
---|
41 | }, [props.subCategory]);
|
---|
42 |
|
---|
43 | return (
|
---|
44 | <li>
|
---|
45 | {getCategories && (
|
---|
46 | <Link
|
---|
47 | to={`/products/${props.category.categorySlug}/${getCategories[0].subCategorySlug}`}
|
---|
48 | >
|
---|
49 | {getCategories && getCategories[0].subCategoryName}
|
---|
50 | </Link>
|
---|
51 | )}
|
---|
52 | </li>
|
---|
53 | );
|
---|
54 | }
|
---|
55 |
|
---|
56 | export default SubCategoryMenu;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.