Skip to content

Commit cf38a15

Browse files
committed
chore: add remaining requests
1 parent bdfcc37 commit cf38a15

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

src/lib/user-builder.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ export const getReposData = async (username) => {
160160
}
161161
};
162162

163-
const getIsGithubRateLimited = async () => {
163+
export const getIsGithubRateLimited = async (showLimit = false) => {
164164
try {
165165
const response = await fetch(`${GITHUB_API_URL}/rate_limit`);
166166
const limit = await response.json();
167+
if (showLimit) {
168+
return limit;
169+
}
167170
if (limit.resources.core.remaining < 1) {
168171
return true;
169172
}

src/views/HomeForm/index.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { useEffect, useState } from 'react';
12
import { withTheme } from 'styled-components';
23
import { ArrowRight } from 'react-iconly';
34
import PropTypes from 'prop-types';
4-
import { Corner } from '@components';
5+
import { Tooltip, Corner } from '@components';
56
import { useForm } from 'react-hook-form';
67
import rules from '@common/rules';
78
import { isEmpty } from 'lodash';
89
import { toLowerCase } from '@utils';
10+
import { useToasts } from '@contexts/toasts';
11+
import { getIsGithubRateLimited } from '@lib/user-builder';
912
import {
1013
StyledContainer,
1114
StyledForm,
@@ -15,17 +18,33 @@ import {
1518
StyledButton,
1619
StyledErrorMessage,
1720
ProductHuntContainer,
21+
RemainingPortfolios,
1822
} from './styles';
1923

2024
function HomeForm({ theme }) {
25+
const [remainingPortfolios, setRemainingPortfolios] = useState(0);
2126
const { register, handleSubmit, formState, errors } = useForm({
2227
mode: 'onChange',
2328
});
29+
const { ToastsType, addToastWithTimeout } = useToasts();
30+
31+
useEffect(async () => {
32+
try {
33+
const { resources } = await getIsGithubRateLimited(true);
34+
const { remaining } = resources.core;
35+
setRemainingPortfolios(remaining);
36+
if (remaining === 0) {
37+
addToastWithTimeout(ToastsType.ERROR, 'Github API rate limit exceeded try again in 1 hour');
38+
}
39+
} catch (error) {
40+
console.log(error);
41+
}
42+
}, []);
2443

2544
const { isValid } = formState;
2645

2746
const onSubmit = ({ username }) => {
28-
if (!username) return;
47+
if (!username || remainingPortfolios === 0) return;
2948
const formattedUsername = toLowerCase(username);
3049
if (window !== undefined) window.location = `/portfolio/${formattedUsername}`;
3150
};
@@ -37,19 +56,47 @@ function HomeForm({ theme }) {
3756
<HeroTitle>Just type your username and watch the magic</HeroTitle>
3857
<StyledForm onSubmit={handleSubmit(onSubmit)}>
3958
<StyledInput
59+
disabled={remainingPortfolios === 0}
4060
placeholder="Github username"
4161
name="username"
4262
type="text"
4363
ref={register(rules.username)}
4464
error={!isEmpty(errors.username)}
4565
/>
46-
<StyledButton type="submit" disabled={!isValid}>
66+
<StyledButton type="submit" disabled={!isValid || remainingPortfolios === 0}>
4767
<ArrowRight set="light" primaryColor={theme.bg.default} />
4868
</StyledButton>
4969
</StyledForm>
5070
<StyledErrorMessage>
5171
{!isEmpty(errors.username) && errors.username.message}
5272
</StyledErrorMessage>
73+
<RemainingPortfolios>
74+
<Tooltip
75+
size="big"
76+
position="bottom"
77+
content="GitHub limits the number of requests per hour to 60 for unauthenticated users"
78+
>
79+
<RemainingPortfolios>
80+
<p>Available requests</p>
81+
<p>
82+
{remainingPortfolios}
83+
/60
84+
</p>
85+
</RemainingPortfolios>
86+
</Tooltip>
87+
{remainingPortfolios === 0 && (
88+
<p className="github-rate">
89+
Github API rate limit exceeded try again in 1 hour&nbsp;
90+
<a
91+
target="_blank"
92+
rel="noreferrer"
93+
href="https://docs.github.com/es/github-ae@latest/rest/reference/rate-limit"
94+
>
95+
Docs
96+
</a>
97+
</p>
98+
)}
99+
</RemainingPortfolios>
53100
<ProductHuntContainer>
54101
<a
55102
href="https://www.producthunt.com/posts/devcover?utm_source=badge-review&utm_medium=badge&utm_souce=badge-devcover#discussion-body"

src/views/HomeForm/styles.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import styled, { css } from 'styled-components';
22
import { Gradient } from '@common/styles';
3+
import { prop } from 'styled-tools';
34

45
export const StyledContainer = styled.div`
56
${({ theme }) => theme.mixins.flexCenter};
@@ -108,3 +109,25 @@ export const ProductHuntContainer = styled.div`
108109
top: 1rem;
109110
left: 1.5rem;
110111
`;
112+
113+
export const RemainingPortfolios = styled.div`
114+
display: flex;
115+
flex-direction: column;
116+
justify-content: center;
117+
align-items: center;
118+
top: 2rem;
119+
left: 46%;
120+
p {
121+
color: ${prop('theme.text.accent')};
122+
}
123+
.github-rate {
124+
margin-top: 0.5rem;
125+
color: ${prop('theme.brand.danger')};
126+
}
127+
a {
128+
color: ${prop('theme.text.accent')};
129+
&:hover {
130+
color: ${prop('theme.brand.primary')};
131+
}
132+
}
133+
`;

0 commit comments

Comments
 (0)