Skip to content

Commit d193d67

Browse files
axios http requests
0 parents  commit d193d67

23 files changed

+2655
-0
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_API_URL=https://dummyjson.com

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

App.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import "tailwindcss";
2+
3+
#root {
4+
max-width: 700px;
5+
margin: auto;
6+
}

App.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import "./App.css";
2+
import Login from "./components/Login";
3+
4+
const App: React.FC = () => {
5+
return (
6+
<div className="container pt-10 mx-auto flex h-full items-center justify-center min-h-screen">
7+
<Login />
8+
</div>
9+
);
10+
};
11+
12+
export default App;

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# React + TypeScript + Vite
2+
3+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4+
5+
Currently, two official plugins are available:
6+
7+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9+
10+
## Expanding the ESLint configuration
11+
12+
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
13+
14+
- Configure the top-level `parserOptions` property like this:
15+
16+
```js
17+
export default tseslint.config({
18+
languageOptions: {
19+
// other options...
20+
parserOptions: {
21+
project: ['./tsconfig.node.json', './tsconfig.app.json'],
22+
tsconfigRootDir: import.meta.dirname,
23+
},
24+
},
25+
})
26+
```
27+
28+
- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
29+
- Optionally add `...tseslint.configs.stylisticTypeChecked`
30+
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:
31+
32+
```js
33+
// eslint.config.js
34+
import react from 'eslint-plugin-react'
35+
36+
export default tseslint.config({
37+
// Set the react version
38+
settings: { react: { version: '18.3' } },
39+
plugins: {
40+
// Add the react plugin
41+
react,
42+
},
43+
rules: {
44+
// other rules...
45+
// Enable its recommended rules
46+
...react.configs.recommended.rules,
47+
...react.configs['jsx-runtime'].rules,
48+
},
49+
})
50+
```

components/Login/index.tsx

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { useState } from "react";
2+
import axios, { AxiosError } from "axios";
3+
import Posts from "../Posts";
4+
5+
const Login: React.FC = () => {
6+
const API_URL = import.meta.env.VITE_API_URL;
7+
const [username, setUsername] = useState("");
8+
const [password, setPassword] = useState("");
9+
const [isAuthenticated, setIsAuthenticated] = useState(false);
10+
11+
const login = async (e: React.FormEvent<HTMLFormElement>) => {
12+
e.preventDefault();
13+
14+
try {
15+
const res = await axios.post(`${API_URL}/user/login`, {
16+
username,
17+
password,
18+
});
19+
20+
localStorage.setItem("auth-token", res.data.accessToken);
21+
setIsAuthenticated(true);
22+
} catch (err) {
23+
const error = err as AxiosError;
24+
25+
if (error.response?.data) {
26+
alert(error.response?.data?.message);
27+
return;
28+
}
29+
30+
alert("Error to sign in. Please, check your credentials.");
31+
console.error(error);
32+
}
33+
};
34+
35+
if (isAuthenticated) {
36+
return <Posts />;
37+
}
38+
39+
return (
40+
<div className="border border-gray-200 p-5 rounded">
41+
<h1 className="text-2xl mb-2">Login</h1>
42+
<p className="font-light text-sm text-gray-600 mb-5 mt-0">
43+
Add your credentials and sign in
44+
</p>
45+
<form onSubmit={login}>
46+
<input
47+
className="w-full mb-5 p-2 border rounded border-gray-200"
48+
value={username}
49+
required
50+
onChange={(e) => setUsername(e.target.value)}
51+
placeholder="Username"
52+
/>
53+
<input
54+
className="w-full mb-5 p-2 border rounded border-gray-200"
55+
value={password}
56+
required
57+
type="password"
58+
onChange={(e) => setPassword(e.target.value)}
59+
placeholder="Password"
60+
/>
61+
<button
62+
type="submit"
63+
className="bg-indigo-500 text-white p-2 rounded pl-5 pr-5 w-full cursor-pointer"
64+
>
65+
Sign in
66+
</button>
67+
</form>
68+
</div>
69+
);
70+
};
71+
72+
export default Login;

components/Posts/Post/index.tsx

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { useState } from "react";
2+
3+
interface PostProps {
4+
id: number;
5+
title: string;
6+
body: string;
7+
onDelete: (id: number) => void;
8+
onEdit: (id: number, title: string, body: string) => void;
9+
}
10+
11+
const Post: React.FC<PostProps> = ({ id, title, body, onDelete, onEdit }) => {
12+
const [isEditing, setIsEditing] = useState(false);
13+
const [editTitle, setEditTitle] = useState(title);
14+
const [editBody, setEditBody] = useState(body);
15+
16+
const handleSave = () => {
17+
onEdit(id, editTitle, editBody);
18+
setIsEditing(false);
19+
};
20+
21+
return (
22+
<div className="p-4 border rounded mb-4 border-gray-200">
23+
{isEditing ? (
24+
<>
25+
<input
26+
className="w-full mb-2 p-2 border rounded border-gray-200"
27+
value={editTitle}
28+
onChange={(e) => setEditTitle(e.target.value)}
29+
/>
30+
<textarea
31+
className="w-full mb-2 p-2 border rounded border-gray-200 min-h-30"
32+
value={editBody}
33+
onChange={(e) => setEditBody(e.target.value)}
34+
/>
35+
<button
36+
onClick={() => setIsEditing(false)}
37+
className=" text-indigo-500 p-2 rounded mr-5"
38+
>
39+
Cancel
40+
</button>
41+
<button
42+
onClick={handleSave}
43+
className="bg-indigo-500 p-2 rounded mr-2 text-white pl-5 pr-5"
44+
>
45+
Save
46+
</button>
47+
</>
48+
) : (
49+
<>
50+
<h3 className="text-lg font-bold">{title}</h3>
51+
<p className="mb-5">{body}</p>
52+
<button
53+
onClick={() => setIsEditing(true)}
54+
className="p-2 rounded mr-2 text-indigo-500 hover:bg-gray-100 cursor-pointer"
55+
>
56+
Edit
57+
</button>
58+
<button
59+
onClick={() => onDelete(id)}
60+
className="p-2 rounded cursor-pointer text-red-700 hover:bg-gray-100"
61+
>
62+
Delete
63+
</button>
64+
</>
65+
)}
66+
</div>
67+
);
68+
};
69+
70+
export default Post;

0 commit comments

Comments
 (0)