Skip to content

Commit

Permalink
7 basic data fetching (#24)
Browse files Browse the repository at this point in the history
* feat: basic axios implementation rick api

* feat: added api types

* feat: api implementation on ui

* feat: rename api response interface

* feat: api scafolding and card component

* feat: comment suggestions
  • Loading branch information
Donivanes authored Apr 25, 2023
1 parent 2777c10 commit c30c84a
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 258 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
dist
dist-ssr
*.local
.env

# Editor directories and files
.vscode/*
Expand Down
252 changes: 109 additions & 143 deletions COPYRIGHT.md

Large diffs are not rendered by default.

182 changes: 84 additions & 98 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"@stitches/react": "^1.2.8",
"cross-fetch": "^3.1.5",
"axios": "^1.3.5",
"i18next": "^22.4.9",
"localforage": "^1.10.0",
"match-sorter": "^6.3.1",
Expand Down
26 changes: 10 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import { useEffect, useState } from 'react'
import { useTranslation, Trans } from 'react-i18next'
import fetch from 'cross-fetch'

import reactLogo from './assets/react.svg'
import shieldjsLogo from './assets/shieldjs.svg'
import './App.css'
import { Button } from './components/Button'
import { Link } from 'react-router-dom'

interface User {
id: number;
name: string;
username: string;
}
import { getCharacter } from './api/rickApi'
import { Character } from './types'
import { Card } from './components/Card'

const languages = [
{ code: 'en', icon: '🇬🇧' },
{ code: 'es', icon: '🇪🇸' }
]

const fetchUser = async (): Promise<User> => {
const res = await fetch('https://jsonplaceholder.typicode.com/users/1')
const json: User = await res.json()
return json
}

function App () {
const [count, setCount] = useState(0)
const [user, setUser] = useState<User | null>(null)
const [character, setCharacter] = useState<Character | null>(null)
const { t, i18n } = useTranslation()

const handleOnLang = (code: string) => i18n.changeLanguage(code).then()

const CHARACTER_ID = 1

useEffect(() => {
fetchUser().then((user) => setUser(user))
getCharacter(CHARACTER_ID).then((character) => setCharacter(character))
}, [])

return (
Expand Down Expand Up @@ -61,6 +53,9 @@ function App () {
<Button primary onClick={() => setCount((count) => count + 1)}>
{t('TotalCount', { count })}
</Button>
<Card image={character?.image || ''} onClick={() => console.log('click')}>
<h2>{character?.name}</h2>
</Card>
<Trans
i18nKey="EditCode"
parent="p"
Expand All @@ -70,7 +65,6 @@ function App () {
</div>
<p className="read-the-docs">{t('LearnMore')}</p>
<Link to="/anyway">{t('GoWaste')}</Link><br/>
<span>{user?.username}</span>
</div>
)
}
Expand Down
Loading

0 comments on commit c30c84a

Please sign in to comment.