Skip to content

Commit 59d6064

Browse files
committed
feat: delete function, types moved to particular files
1 parent 457fbde commit 59d6064

7 files changed

Lines changed: 104 additions & 31 deletions

File tree

onlineshop/source/client/src/App.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import './App.css';
44
import { Button, Grid2, TextField } from '@mui/material';
55
import getProjects from './components/getProjects';
66
import getUsers from './components/getUsers';
7+
import deleteUser from './components/deleteUser';
8+
import deleteProject from './components/deleteProject';
79

810
const App: React.FC = () => (
911
<div className="App" style={{paddingTop: 30}}>
@@ -17,7 +19,7 @@ const App: React.FC = () => (
1719
<Button variant="contained" onClick={getUsers}>Get all users</Button>
1820
</div>
1921
<div style={{paddingBottom: 10}}>
20-
<Button variant="contained">Create User</Button>
22+
<Button variant="contained" onClick={deleteUser}>Delete User</Button>
2123
</div>
2224
</Grid2>
2325
<Grid2>
@@ -28,7 +30,7 @@ const App: React.FC = () => (
2830
<Button variant="contained" onClick={getProjects}>Get all projects</Button>
2931
</div>
3032
<div style={{paddingBottom: 10}}>
31-
<Button variant="contained">Create Project</Button>
33+
<Button variant="contained" onClick={deleteProject}>Delete Project</Button>
3234
</div>
3335
</Grid2>
3436
</Grid2>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import axios from "axios";
2+
import { GetProjectsResponse } from "../types/project";
3+
4+
5+
export default async function deleteProject() {
6+
try{
7+
let data
8+
const element =(document.getElementById('project_id') as HTMLInputElement).value;
9+
if (Number(element) < 0){
10+
throw new Error('Index cant be lower than zero');
11+
}
12+
else {
13+
data = await axios.delete<GetProjectsResponse>(`http://localhost:4000/api/projects/${Number(element)}`,
14+
{
15+
headers:{
16+
Accept:'application/json',
17+
},
18+
},
19+
);
20+
}
21+
console.log(JSON.stringify(data.data, null, 4));
22+
console.log('response status is: ', data.status);
23+
return data;
24+
}
25+
catch (error) {
26+
if (axios.isAxiosError(error)) {
27+
console.log('error message: ', error.message);
28+
return error.message;
29+
} else {
30+
console.log('unexpected error: ', error);
31+
return 'An unexpected error occurred';
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import axios from "axios";
2+
import { GetUsersResponse } from "../types/user";
3+
4+
5+
export default async function deleteUser() {
6+
try{
7+
let data
8+
const element =(document.getElementById('user_id') as HTMLInputElement).value;
9+
if (Number(element) < 0){
10+
throw new Error('Index cant be lower than zero');
11+
}
12+
else {
13+
data = await axios.delete<GetUsersResponse>(`http://localhost:4000/api/users/${Number(element)}`,
14+
{
15+
headers:{
16+
Accept:'application/json',
17+
},
18+
},
19+
);
20+
}
21+
console.log(JSON.stringify(data.data, null, 4));
22+
console.log('response status is: ', data.status);
23+
return data;
24+
}
25+
catch (error) {
26+
if (axios.isAxiosError(error)) {
27+
console.log('error message: ', error.message);
28+
return error.message;
29+
} else {
30+
console.log('unexpected error: ', error);
31+
return 'An unexpected error occurred';
32+
}
33+
}
34+
}

onlineshop/source/client/src/components/getProjects.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
import axios from "axios";
2-
import { User } from "./getUsers";
2+
import { GetProjectsResponse } from "../types/project";
33

4-
export type project = {
5-
id: number,
6-
projectName: string,
7-
description: string,
8-
feeAmount: number,
9-
alreadyCollected: number,
10-
user: User,
11-
likesCount: number,
12-
dislikesCount: number,
13-
};
14-
15-
type GetProjectsResponse = {
16-
data: project[]
17-
};
184

195
export default async function getProjects() {
206
try{

onlineshop/source/client/src/components/getUsers.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import axios from "axios";
2-
import { project as user } from "./getProjects";
3-
4-
export type User = {
5-
id: number,
6-
login: string,
7-
password: string,
8-
email: string,
9-
nickname: string,
10-
projects:user[];
11-
};
12-
13-
type GetUsersResponse = {
14-
data: User[];
15-
};
2+
import { GetUsersResponse } from "../types/user";
163

174
export default async function getUsers() {
185
try{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { User } from "./user";
2+
3+
export type project = {
4+
id: number,
5+
projectName: string,
6+
description: string,
7+
feeAmount: number,
8+
alreadyCollected: number,
9+
user: User,
10+
likesCount: number,
11+
dislikesCount: number,
12+
};
13+
14+
export type GetProjectsResponse = {
15+
data: project[]
16+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { project } from "./project";
2+
3+
export type User = {
4+
id: number,
5+
login: string,
6+
password: string,
7+
email: string,
8+
nickname: string,
9+
projects:project[];
10+
};
11+
12+
export type GetUsersResponse = {
13+
data: User[];
14+
};

0 commit comments

Comments
 (0)