Commit f511a2a1 authored by Ken's avatar Ken

fetch category api

parent 24ed1500
@use "~bootstrap/scss/bootstrap"; @use "~bootstrap/scss/bootstrap";
@use "./styles/components.scss"; @use "styles/components.scss";
@use "./styles/pages.scss"; @use "styles/pages.scss";
@use "./styles/common.scss"; @use "styles/common.scss";
import axiosClient from "./axiosClient";
const categoryApi = {
getCategories: () => {
const url = "/api/category";
return axiosClient.get(url);
},
};
export default categoryApi;
import { configureStore } from "@reduxjs/toolkit"; import { configureStore } from "@reduxjs/toolkit";
import headerReducer from "components/Header/headerSlice";
const rootReducer = {}; const rootReducer = {
header: headerReducer,
};
const store = configureStore({ reducer: rootReducer }); const store = configureStore({ reducer: rootReducer });
......
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import categoryApi from "api/categoryApi";
import { ICategory } from "components/interface";
const initialState: { data: ICategory[] } = {
data: [],
};
export const getCategories = createAsyncThunk("header/categories", async () => {
const res = await categoryApi.getCategories();
return res;
});
const header = createSlice({
name: "header",
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(
getCategories.fulfilled,
(state, action: PayloadAction<any>) => {
state.data = action.payload.data;
}
);
},
});
const { reducer } = header;
export default reducer;
import React, { useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import AppBar from "@mui/material/AppBar"; import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton"; import IconButton from "@mui/material/IconButton";
...@@ -8,35 +8,26 @@ import Button from "@mui/material/Button"; ...@@ -8,35 +8,26 @@ import Button from "@mui/material/Button";
import HomeIcon from "@mui/icons-material/Home"; import HomeIcon from "@mui/icons-material/Home";
import Sidenav from "../Sidenav"; import Sidenav from "../Sidenav";
import { Typography } from "@mui/material"; import { Typography } from "@mui/material";
import { useAppDispatch, useAppSelector } from "app/hooks";
const navItems = [ import { getCategories } from "./headerSlice";
"Media",
"thời sự",
"thế giới",
"pháp luật",
"kinh doanh",
"công nghệ",
"xe",
"du lịch",
"nhịp sống trẻ",
"văn hóa",
"giải trí",
"thể thao",
"giáo dục",
"khoa học",
"sức khỏe",
"giả - thật",
"bạn đọc",
];
export default function Header() { export default function Header() {
const dispatch = useAppDispatch();
const headerRef = useRef<HTMLElement>(null); const headerRef = useRef<HTMLElement>(null);
const [mobileOpen, setMobileOpen] = useState<boolean>(false); const [mobileOpen, setMobileOpen] = useState<boolean>(false);
const { data: categoryData } = useAppSelector((state) => state.header);
useEffect(() => {
dispatch(getCategories());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const handleDrawerToggle = () => { const handleDrawerToggle = () => {
setMobileOpen(!mobileOpen); setMobileOpen(!mobileOpen);
}; };
console.log(categoryData);
return ( return (
<Box sx={{ display: "flex" }} className="header"> <Box sx={{ display: "flex" }} className="header">
<AppBar <AppBar
...@@ -81,10 +72,10 @@ export default function Header() { ...@@ -81,10 +72,10 @@ export default function Header() {
alignItems: "center", alignItems: "center",
}} }}
> >
{navItems.map((item) => ( {categoryData.map((item) => (
<Box <Box
component="li" component="li"
key={item} key={item.id}
sx={{ sx={{
color: "#000", color: "#000",
fontWeight: 700, fontWeight: 700,
...@@ -94,7 +85,7 @@ export default function Header() { ...@@ -94,7 +85,7 @@ export default function Header() {
}} }}
className="header-list__item" className="header-list__item"
> >
{item} {item.label}
</Box> </Box>
))} ))}
</Box> </Box>
...@@ -123,7 +114,7 @@ export default function Header() { ...@@ -123,7 +114,7 @@ export default function Header() {
</AppBar> </AppBar>
<Sidenav <Sidenav
handleDrawerToggle={handleDrawerToggle} handleDrawerToggle={handleDrawerToggle}
navItems={navItems} navItems={categoryData}
mobileOpen={mobileOpen} mobileOpen={mobileOpen}
/> />
</Box> </Box>
......
...@@ -9,10 +9,11 @@ import { ...@@ -9,10 +9,11 @@ import {
ListItemText, ListItemText,
Typography, Typography,
} from "@mui/material"; } from "@mui/material";
import { ICategory } from "components/interface";
type Props = { type Props = {
handleDrawerToggle: () => void; handleDrawerToggle: () => void;
navItems: Array<string>; navItems: Array<ICategory>;
mobileOpen: boolean; mobileOpen: boolean;
}; };
...@@ -43,9 +44,9 @@ const Sidenav = (props: Props) => { ...@@ -43,9 +44,9 @@ const Sidenav = (props: Props) => {
<Divider /> <Divider />
<List> <List>
{navItems.map((item) => ( {navItems.map((item) => (
<ListItem key={item} disablePadding> <ListItem key={item.id} disablePadding>
<ListItemButton sx={{ textAlign: "center" }}> <ListItemButton sx={{ textAlign: "center" }}>
<ListItemText primary={item} /> <ListItemText primary={item.label} />
</ListItemButton> </ListItemButton>
</ListItem> </ListItem>
))} ))}
......
export interface ICategory {
id: string;
label: string;
}
const enviromentConfig = { const enviromentConfig = {
development: { development: {
endPoint: "http://localhost:5223", endPoint: "https://localhost:44386",
}, },
production: { production: {
endPoint: "", endPoint: "",
......
...@@ -44,8 +44,8 @@ const newspapers: Array<INewspaper> = [ ...@@ -44,8 +44,8 @@ const newspapers: Array<INewspaper> = [
const MainPage = () => { const MainPage = () => {
return ( return (
<main className="container py-3"> <main className="container py-3">
{newspapers.map((newspaper) => ( {newspapers.map((newspaper, idx) => (
<Newspaper key={newspaper.label} data={newspaper} /> <Newspaper key={`newspaper-${idx}`} data={newspaper} />
))} ))}
</main> </main>
); );
......
@forward "./mixins";
@forward "./variables";
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "es5",
"lib": [ "lib": ["dom", "dom.iterable", "esnext"],
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true, "allowJs": true,
"skipLibCheck": true, "skipLibCheck": true,
"esModuleInterop": true, "esModuleInterop": true,
...@@ -18,9 +14,8 @@ ...@@ -18,9 +14,8 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"noEmit": true, "noEmit": true,
"jsx": "react-jsx" "jsx": "react-jsx",
"baseUrl": "./src"
}, },
"include": [ "include": ["src"]
"src"
]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment