import axios from 'axios'; import { Fragment, useCallback, useEffect, useState } from 'react'; import moment from 'moment'; import HeadTagEditor from './head-tag-editor'; import ErrorPage from './error-page'; import ThemeChanger from './theme-changer'; import AvatarCard from './avatar-card'; import Details from './details'; import Skill from './skill'; import Experience from './experience'; import Education from './education'; import Project from './project'; import Blog from './blog'; import { constructConfigWithMissingValues, getInitialTheme, setupHotjar, } from '../helpers/utils'; import { HelmetProvider } from 'react-helmet-async'; import PropTypes from 'prop-types'; import '../assets/index.css'; const GitProfile = ({ config }) => { const [error, setError] = useState( typeof config === 'undefined' ? { status: 500, title: 'No Config is provided', subTitle: 'Pass the required config as prop.', } : null ); typeof config !== 'undefined' && constructConfigWithMissingValues(config); const [theme, setTheme] = useState( config ? getInitialTheme(config.themeConfig) : null ); const [loading, setLoading] = useState(true); const [profile, setProfile] = useState(null); const [repo, setRepo] = useState(null); useEffect(() => { if (theme) { document.documentElement.setAttribute('data-theme', theme); } }, [theme]); useEffect(() => { config && setupHotjar(config.hotjar); }, []); const loadData = useCallback(() => { axios .get(`https://api.github.com/users/${config.github.username}`) .then((response) => { let data = response.data; let profileData = { avatar: data.avatar_url, name: data.name ? data.name : '', bio: data.bio ? data.bio : '', location: data.location ? data.location : '', company: data.company ? data.company : '', }; setProfile(profileData); }) .then(() => { let excludeRepo = ``; config.github.exclude.projects.forEach((project) => { excludeRepo += `+-repo:${config.github.username}/${project}`; }); let query = `user:${config.github.username}+fork:${!config.github .exclude.forks}${excludeRepo}`; let url = `https://api.github.com/search/repositories?q=${query}&sort=${config.github.sortBy}&per_page=${config.github.limit}&type=Repositories`; axios .get(url, { headers: { 'Content-Type': 'application/vnd.github.v3+json', }, }) .then((response) => { let data = response.data; setRepo(data.items); }) .catch((error) => { handleError(error); }); }) .catch((error) => { handleError(error); }) .finally(() => { setLoading(false); }); }, [setLoading]); useEffect(() => { config && loadData(); }, [loadData]); const handleError = (error) => { console.error('Error:', error); try { let reset = moment( new Date(error.response.headers['x-ratelimit-reset'] * 1000) ).fromNow(); if (error.response.status === 403) { 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({ status: 404, title: 'The Github Username is Incorrect.', subTitle: (
Please provide correct github username in config.