Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Persistindo dados após atualizar página. resolves #20 #35

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/pages/Dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import api from '../../services/api';
import logoImage from '../../assets/logo.svg';
import loadingImage from '../../assets/loading.svg';

import { Title, Form, Badges, Error, LoadingIcon, Center } from './styles';
import {
Title,
RepositoryName,
Form,
Badges,
Error,
LoadingIcon,
Center,
} from './styles';

interface Badge {
name: string;
Expand All @@ -18,7 +26,17 @@ interface Badge {

const Dashboard: React.FC = () => {
const [text, setText] = useState('');
const [badges, setBadges] = useState<Badge[]>([]);
const [prevRepoName, setPrevRepoName] = useState(
localStorage.getItem('@repo'),
lucasarieiv marked this conversation as resolved.
Show resolved Hide resolved
);
const [badges, setBadges] = useState<Badge[]>(() => {
const hasBadges = JSON.parse(localStorage.getItem('@badges')!);
lucasarieiv marked this conversation as resolved.
Show resolved Hide resolved

if (hasBadges && hasBadges.length > 0) {
return hasBadges;
}
return [];
});
const [inputError, setInputError] = useState('');
const [loading, setLoading] = useState(false);

Expand All @@ -44,6 +62,9 @@ const Dashboard: React.FC = () => {
const response = generateBadges(`${username}/${repository}`);

setBadges(response);
localStorage.setItem('@badges', JSON.stringify(response));
lucasarieiv marked this conversation as resolved.
Show resolved Hide resolved
localStorage.setItem('@repo', `${username}/${repository}`);
lucasarieiv marked this conversation as resolved.
Show resolved Hide resolved
setPrevRepoName(`${username}/${repository}`);
setText('');
setInputError('');
} catch (err) {
Expand Down Expand Up @@ -76,6 +97,8 @@ const Dashboard: React.FC = () => {

{inputError && <Error>{inputError}</Error>}

<RepositoryName>{prevRepoName}</RepositoryName>

<Center>
<LoadingIcon isLoading={loading} src={loadingImage} alt="Loading" />
</Center>
Expand Down
9 changes: 9 additions & 0 deletions src/pages/Dashboard/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export const Title = styled.h1`
margin-top: 80px;
`;

export const RepositoryName = styled.h1`
font-size: 32px;
color: #3a3a3a;
max-width: 735px;
line-height: 56px;

margin-top: 40px;
`;

export const Form = styled.form<FormProp>`
margin-top: 40px;
max-width: 700px;
Expand Down