File tree Expand file tree Collapse file tree
onlineshop/source/client/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import './App.css';
44import { Button , Grid2 , TextField } from '@mui/material' ;
55import getProjects from './components/getProjects' ;
66import getUsers from './components/getUsers' ;
7+ import deleteUser from './components/deleteUser' ;
8+ import deleteProject from './components/deleteProject' ;
79
810const 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11import 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
195export default async function getProjects ( ) {
206 try {
Original file line number Diff line number Diff line change 11import 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
174export default async function getUsers ( ) {
185 try {
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments