Browse Source

feat: added route loading spinner

hover-contact
ntasicc 3 years ago
parent
commit
aecc3c2e1e
5 changed files with 101 additions and 7 deletions
  1. 4
    1
      components/layout/navbar/Navbar.jsx
  2. 33
    0
      pages/_app.js
  3. 0
    5
      pages/api/hello.js
  4. 1
    1
      requests/dataIdsRequest.js
  5. 63
    0
      styles/globals.css

+ 4
- 1
components/layout/navbar/Navbar.jsx View File

} }


return ( return (
<AppBar position="static">
<AppBar
position="static"
sx={{ zIndex: 100, position: 'fixed', top: 0, left: 0 }}
>
<Container maxWidth="xl"> <Container maxWidth="xl">
<Toolbar disableGutters> <Toolbar disableGutters>
<AdbIcon sx={{ display: { xs: 'none', md: 'flex' }, mr: 1 }} /> <AdbIcon sx={{ display: { xs: 'none', md: 'flex' }, mr: 1 }} />

+ 33
- 0
pages/_app.js View File

import Layout from '../components/layout/base-layout/Layout'; import Layout from '../components/layout/base-layout/Layout';
import '../styles/globals.css'; import '../styles/globals.css';


import { useRouter } from 'next/router';
import { useEffect } from 'react';

const LoadingSpinner = () => {
const router = useRouter();

const [loading, setLoading] = useState(false);

useEffect(() => {
const handleStart = (url) => url !== router.asPath && setLoading(true);
const handleComplete = (url) => url === router.asPath && setLoading(false);

router.events.on('routeChangeStart', handleStart);
router.events.on('routeChangeComplete', handleComplete);
router.events.on('routeChangeError', handleComplete);

return () => {
router.events.off('routeChangeStart', handleStart);
router.events.off('routeChangeComplete', handleComplete);
router.events.off('routeChangeError', handleComplete);
};
});

return (
loading && (
<div className="spinner-wrapper">
<div className="spinner"></div>
</div>
)
);
};

function MyApp({ Component, pageProps: { session, ...pageProps } }) { function MyApp({ Component, pageProps: { session, ...pageProps } }) {
const [queryClient] = useState(() => new QueryClient()); const [queryClient] = useState(() => new QueryClient());


content="width=device-width, initial-scale=1" content="width=device-width, initial-scale=1"
/> />
</Head> </Head>
<LoadingSpinner />
<Component {...pageProps} /> <Component {...pageProps} />
</Layout> </Layout>
</SessionProvider> </SessionProvider>

+ 0
- 5
pages/api/hello.js View File

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' })
}

+ 1
- 1
requests/dataIdsRequest.js View File

import apiEndpoints from './apiEndpoints'; import apiEndpoints from './apiEndpoints';


export const getDataIds = async () => { export const getDataIds = async () => {
const response = await fetch(`${apiEndpoints.dataIds}`);
const response = await fetch(`http://localhost:3000${apiEndpoints.dataIds}`);


const data = await response.json(); const data = await response.json();



+ 63
- 0
styles/globals.css View File

h6 { h6 {
font-family: 'Lato', sans-serif; font-family: 'Lato', sans-serif;
} }

.spinner-wrapper {
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
position: fixed;
top: 0;
left: 0;
background-color: rgb(255, 255, 255);
z-index: 99;
}
.spinner {
position: absolute;
left: 50%;
top: 50%;
height: 60px;
width: 60px;
margin: 0px auto;
-webkit-animation: rotation 0.6s infinite linear;
-moz-animation: rotation 0.6s infinite linear;
-o-animation: rotation 0.6s infinite linear;
animation: rotation 0.6s infinite linear;
border-left: 6px solid rgba(0, 174, 239, 0.15);
border-right: 6px solid rgba(0, 174, 239, 0.15);
border-bottom: 6px solid rgba(0, 174, 239, 0.15);
border-top: 6px solid rgba(0, 174, 239, 0.8);
border-radius: 100%;
}

@-webkit-keyframes rotation {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(359deg);
}
}
@-moz-keyframes rotation {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(359deg);
}
}
@-o-keyframes rotation {
from {
-o-transform: rotate(0deg);
}
to {
-o-transform: rotate(359deg);
}
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}

Loading…
Cancel
Save