Skip to content

Commit

Permalink
Merge pull request #29 from zainbinfurqan/add-context-api
Browse files Browse the repository at this point in the history
update loading issue by adding context api
  • Loading branch information
Mridul2820 authored Oct 22, 2022
2 parents f3cf84f + 57e644e commit 9b0e3a2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
9 changes: 6 additions & 3 deletions components/reuse/Search.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, { useState } from 'react';
import React, { useContext, useState } from 'react';
import { FaSearch, FaSpinner } from 'react-icons/fa';
import { useRouter } from 'next/router';
import { AppContext } from '../../pages/_app';

const Search = ({ height }) => {
const { isLoading, toggleLoading } = useContext(AppContext)
const router = useRouter();
const [loading, setLoading] = useState(false);

Expand All @@ -15,12 +17,13 @@ const Search = ({ height }) => {

if (username && !hasWhiteSpaceText(username)) {
setLoading(true);
toggleLoading(true)
router.push(`/user/${username}`);
}
};

const hasWhiteSpaceText = (text) => {
return /\s/g.test(text);
return /\s/g.test(text);
};

return (
Expand All @@ -39,7 +42,7 @@ const Search = ({ height }) => {
type="submit"
className="bg-purple-mid text-white h-full px-4 rounded-r-md"
>
{loading ? (
{isLoading ? (
<div className="animate-spin">
<FaSpinner />
</div>
Expand Down
30 changes: 26 additions & 4 deletions pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React, { useReducer } from 'react';
import { ThemeProvider } from 'next-themes';
import { RecoilRoot } from 'recoil';
import '../styles/globals.css';
Expand All @@ -12,19 +13,40 @@ import { ApolloProvider } from '@apollo/client';
import { client } from '../client';

import Footer from '../components/footer/Footer';
export const AppContext = React.createContext();

const reducer = (state, action) => {
if (action.type === 'LOADING_TOGGLE') {
return { ...state, isLoading: action.payload };
}
return state;
};
const initialStates = { isLoading: false }

function MyApp({ Component, pageProps }) {

const [state, dispatch] = useReducer(reducer, initialStates)

const toggleLoading = async (flag) => {
dispatch({
type: 'LOADING_TOGGLE',
payload: flag
});
}

return (
<ApolloProvider client={client}>
<RecoilRoot>
<DefaultSeo {...SEO} />
<ThemeProvider attribute="class">
<Component {...pageProps} />
<ThemeButton />
<Footer />
<AppContext.Provider value={{ ...state, toggleLoading }}>
<Component {...pageProps} />
<ThemeButton />
<Footer />
</AppContext.Provider>
</ThemeProvider>
</RecoilRoot>
</ApolloProvider>
</ApolloProvider >
);
}

Expand Down
29 changes: 16 additions & 13 deletions pages/user/[username].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import dynamic from 'next/dynamic';
import { v2 as cloudinary } from 'cloudinary';

Expand All @@ -8,6 +8,7 @@ import { client } from '../../client';
import { NextSeo } from 'next-seo';
import { useRouter } from 'next/router';
import NotFound from '../../components/404/NotFound';
import { AppContext } from '../_app';

const Navbar = dynamic(() => import('../../components/nav/Navbar'));
const SocialCard = dynamic(() => import('../../components/social/SocialCard'));
Expand All @@ -34,32 +35,33 @@ const MostStar = dynamic(() => import('../../components/graphs/MostStar'));
const ContributionGraph = dynamic(() =>
import('../../components/graphs/ContributionGraph')
);

const { SITE_URL } = process.env;

const UserName = ({ user, ogImageUrl }) => {
const { toggleLoading } = useContext(AppContext)
const router = useRouter();
const [loading, setLoading] = useState(true);

useEffect(() => {
setLoading(true);

setTimeout(() => {
setLoading(false);
}, 2500);
}, [router.query.username]);

useEffect(() => {
toggleLoading(false)
}, [user])

const SEO = {
title: `${user?.name ? user.name : 'User'} (@${
user?.login
}) : Github Profile Stats and Graphs in One Place`,
title: `${user?.name ? user.name : 'User'} (@${user?.login
}) : Github Profile Stats and Graphs in One Place`,
description: `${user?.name} (@${user?.login}) Github Profile Stats, Languge Graph, Social Card, Contribution Graph, Repository Stats, Graphs and more`,
canonical: `${SITE_URL}/user/${user?.login}`,

openGraph: {
title: `${user?.name ? user.name : 'User'} (@${
user?.login
}) : Github Profile Stats and Graphs in One Place`,
title: `${user?.name ? user.name : 'User'} (@${user?.login
}) : Github Profile Stats and Graphs in One Place`,
description: `${user?.name} (@${user?.login}) Github Profile Stats, Languge Graph, Social Card, Contribution Graph, Repository Stats, Graphs and more`,
url: `${SITE_URL}/user/${user?.login}`,

Expand Down Expand Up @@ -130,10 +132,11 @@ export async function getServerSideProps({ params }) {
username: params.username,
},
});

const errorCode = data.user === null && true;

const user = data.user;
const errorCode = null;
let user = []
if (data.user !== null) {
user = data.user;
}

cloudinary.config({
cloud_name: CLOUD_NAME,
Expand Down

1 comment on commit 9b0e3a2

@vercel
Copy link

@vercel vercel bot commented on 9b0e3a2 Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

git-to-get – ./

git-to-get-mridul28.vercel.app
git-to-get-git-main-mridul28.vercel.app
git-o-get.mridul.tech

Please sign in to comment.