Commit f10f185b authored by Ken's avatar Ken

feat: add loading component

parent a9990c84
import { createSlice } from "@reduxjs/toolkit";
const global = createSlice({
name: "global",
initialState: { isLoading: false },
reducers: {
handleLoading: (state, action) => {
state.isLoading = action.payload;
},
},
});
const { actions, reducer } = global;
export const { handleLoading } = actions;
export default reducer;
import { configureStore } from "@reduxjs/toolkit"; import { configureStore } from "@reduxjs/toolkit";
import headerReducer from "components/Header/headerSlice"; import headerReducer from "components/Header/headerSlice";
import globalReducer from "./globalSlice";
const rootReducer = { const rootReducer = {
header: headerReducer, header: headerReducer,
global: globalReducer,
}; };
const store = configureStore({ reducer: rootReducer }); const store = configureStore({ reducer: rootReducer });
......
...@@ -10,6 +10,7 @@ import Sidenav from "../Sidenav"; ...@@ -10,6 +10,7 @@ import Sidenav from "../Sidenav";
import { Typography } from "@mui/material"; import { Typography } from "@mui/material";
import { useAppDispatch, useAppSelector } from "app/hooks"; import { useAppDispatch, useAppSelector } from "app/hooks";
import { getCategories } from "./headerSlice"; import { getCategories } from "./headerSlice";
import { handleLoading } from "app/globalSlice";
export default function Header() { export default function Header() {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
...@@ -18,7 +19,19 @@ export default function Header() { ...@@ -18,7 +19,19 @@ export default function Header() {
const { data: categoryData } = useAppSelector((state) => state.header); const { data: categoryData } = useAppSelector((state) => state.header);
useEffect(() => { useEffect(() => {
dispatch(getCategories()); dispatch(handleLoading(true));
try {
const fetchData = async () => {
await dispatch(getCategories());
};
fetchData();
} catch (err) {
console.error("ERROR: ", err);
} finally {
dispatch(handleLoading(false));
}
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
...@@ -26,8 +39,6 @@ export default function Header() { ...@@ -26,8 +39,6 @@ export default function Header() {
setMobileOpen(!mobileOpen); setMobileOpen(!mobileOpen);
}; };
console.log(categoryData);
return ( return (
<Box sx={{ display: "flex" }} className="header"> <Box sx={{ display: "flex" }} className="header">
<AppBar <AppBar
......
.loading {
display: none;
&-open {
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: 1000;
display: flex;
justify-content: center;
align-items: center;
background-color: white;
opacity: 0.5;
cursor: not-allowed;
}
}
import { CircularProgress } from "@mui/material";
import React from "react";
type Props = {
isOpen: boolean;
};
const Loading = ({ isOpen }: Props) => {
return (
<div className={`loading${isOpen ? " loading-open" : ""}`}>
<CircularProgress />
</div>
);
};
export default Loading;
import { useAppSelector } from "app/hooks";
import Loading from "components/Loading";
import React from "react"; import React from "react";
import Newspaper from "../components/Newspaper"; import Newspaper from "../components/Newspaper";
import { INewspaper } from "../utils/interface"; import { INewspaper } from "../utils/interface";
...@@ -42,8 +44,11 @@ const newspapers: Array<INewspaper> = [ ...@@ -42,8 +44,11 @@ const newspapers: Array<INewspaper> = [
]; ];
const MainPage = () => { const MainPage = () => {
const { isLoading } = useAppSelector((state) => state.global);
return ( return (
<main className="container py-3"> <main className="container py-3">
<Loading isOpen={isLoading} />
{newspapers.map((newspaper, idx) => ( {newspapers.map((newspaper, idx) => (
<Newspaper key={`newspaper-${idx}`} data={newspaper} /> <Newspaper key={`newspaper-${idx}`} data={newspaper} />
))} ))}
......
@use "../components/Header/Header.scss"; @use "components/Header/Header.scss";
@use "../components/Newspaper/Newspaper.scss"; @use "components/Newspaper/Newspaper.scss";
@use "components/Loading/Loading.scss";
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