diff --git a/src/components/GitProfile.jsx b/src/components/GitProfile.jsx index e928956..6859b97 100644 --- a/src/components/GitProfile.jsx +++ b/src/components/GitProfile.jsx @@ -17,12 +17,21 @@ import PropTypes from 'prop-types'; import '../assets/index.css'; const GitProfile = ({ config }) => { - const [theme, setTheme] = useState(getInitialTheme(config.themeConfig)); + const [theme, setTheme] = useState( + config ? getInitialTheme(config.themeConfig) : null + ); const [loading, setLoading] = useState(true); const [profile, setProfile] = useState(null); const [repo, setRepo] = useState(null); - const [error, setError] = useState(null); - const [rateLimit, setRateLimit] = useState(null); + const [error, setError] = useState( + typeof config === 'undefined' + ? { + status: 500, + title: 'No Config is provided', + subTitle: 'Pass the required config as prop.', + } + : null + ); useEffect(() => { if (theme) { @@ -31,7 +40,7 @@ const GitProfile = ({ config }) => { }, [theme]); useEffect(() => { - setupHotjar(config.hotjar); + config && setupHotjar(config.hotjar); }, []); const loadData = useCallback(() => { @@ -86,71 +95,76 @@ const GitProfile = ({ config }) => { }, [setLoading]); useEffect(() => { - loadData(); + config && loadData(); }, [loadData]); const handleError = (error) => { console.error('Error:', error); try { - setRateLimit({ - remaining: error.response.headers['x-ratelimit-remaining'], - reset: moment( - new Date(error.response.headers['x-ratelimit-reset'] * 1000) - ).fromNow(), - }); + let reset = moment( + new Date(error.response.headers['x-ratelimit-reset'] * 1000) + ).fromNow(); if (error.response.status === 403) { - setError(429); + setError({ + status: 429, + title: 'Too Many Requests.', + subTitle: ( +

+ Oh no, you hit the{' '} + + rate limit. + + ! Try again later{` ${reset}`}. +

+ ), + }); } else if (error.response.status === 404) { - setError(404); + setError({ + status: 404, + title: 'The Github Username is Incorrect.', + subTitle: ( +

+ Please provide correct github username in config. +

+ ), + }); } else { - setError(500); + setError({ + status: 500, + title: 'Ops!!', + subTitle: 'Something went wrong.', + }); } } catch (error2) { - setError(500); + setError({ + status: 500, + title: 'Ops!!', + subTitle: 'Something went wrong.', + }); } }; return ( - + {!error && ( + + )}
{error ? ( - Please provide correct github username in config -

- ) : error === 429 ? ( -

- Oh no, you hit the{' '} - - rate limit - - ! Try again later{rateLimit && ` ${rateLimit.reset}`}. -

- ) : ( - `Something went wrong` - ) - } + status={`${error.status}`} + title={error.title} + subTitle={error.subTitle} /> ) : ( @@ -158,7 +172,7 @@ const GitProfile = ({ config }) => {
- {!config.themeConfig.disableSwitch && ( + {!error && !config.themeConfig.disableSwitch && ( { {props.status}

{props.title}

-

+

{props.subTitle} -

+
@@ -25,7 +25,7 @@ const ErrorPage = (props) => { ErrorPage.propTypes = { status: PropTypes.string.isRequired, title: PropTypes.string.isRequired, - subTitle: PropTypes.string.isRequired, + subTitle: PropTypes.node.isRequired, }; export default ErrorPage;