@@ -35,119 +35,109 @@ class AdminPage extends Component {
35
35
this . setState ( { [ name ] : value } )
36
36
}
37
37
38
- handleGetUsers = ( ) => {
39
- const Auth = this . context
40
- const user = Auth . getUser ( )
38
+ handleGetUsers = async ( ) => {
39
+ try {
40
+ const user = this . context . getUser ( )
41
41
42
- this . setState ( { isUsersLoading : true } )
43
- bookApi . getUsers ( user )
44
- . then ( response => {
45
- this . setState ( { users : response . data } )
46
- } )
47
- . catch ( error => {
48
- handleLogError ( error )
49
- } )
50
- . finally ( ( ) => {
51
- this . setState ( { isUsersLoading : false } )
52
- } )
53
- }
42
+ this . setState ( { isUsersLoading : true } )
54
43
55
- handleDeleteUser = ( username ) => {
56
- const Auth = this . context
57
- const user = Auth . getUser ( )
44
+ const response = await bookApi . getUsers ( user )
45
+ const users = response . data
58
46
59
- bookApi . deleteUser ( user , username )
60
- . then ( ( ) => {
61
- this . handleGetUsers ( )
62
- } )
63
- . catch ( error => {
64
- handleLogError ( error )
65
- } )
47
+ this . setState ( { users } )
48
+ } catch ( error ) {
49
+ handleLogError ( error )
50
+ } finally {
51
+ this . setState ( { isUsersLoading : false } )
52
+ }
66
53
}
67
54
68
- handleSearchUser = ( ) => {
69
- const Auth = this . context
70
- const user = Auth . getUser ( )
55
+ handleDeleteUser = async ( username ) => {
56
+ try {
57
+ const user = this . context . getUser ( )
71
58
72
- const username = this . state . userUsernameSearch
73
- bookApi . getUsers ( user , username )
74
- . then ( response => {
75
- const data = response . data
76
- const users = data instanceof Array ? data : [ data ]
77
- this . setState ( { users } )
78
- } )
79
- . catch ( error => {
80
- handleLogError ( error )
81
- this . setState ( { users : [ ] } )
82
- } )
59
+ await bookApi . deleteUser ( user , username )
60
+ await this . handleGetUsers ( )
61
+ } catch ( error ) {
62
+ handleLogError ( error )
63
+ }
83
64
}
84
65
85
- handleGetBooks = ( ) => {
86
- const Auth = this . context
87
- const user = Auth . getUser ( )
66
+ handleSearchUser = async ( ) => {
67
+ try {
68
+ const user = this . context . getUser ( )
69
+
70
+ const username = this . state . userUsernameSearch
71
+ const response = await bookApi . getUsers ( user , username )
88
72
89
- this . setState ( { isBooksLoading : true } )
90
- bookApi . getBooks ( user )
91
- . then ( response => {
92
- this . setState ( { books : response . data } )
93
- } )
94
- . catch ( error => {
95
- handleLogError ( error )
96
- } )
97
- . finally ( ( ) => {
98
- this . setState ( { isBooksLoading : false } )
99
- } )
73
+ const data = response . data
74
+ const users = data instanceof Array ? data : [ data ]
75
+ this . setState ( { users } )
76
+ } catch ( error ) {
77
+ handleLogError ( error )
78
+ this . setState ( { users : [ ] } )
79
+ }
100
80
}
101
81
102
- handleDeleteBook = ( isbn ) => {
103
- const Auth = this . context
104
- const user = Auth . getUser ( )
82
+ handleGetBooks = async ( ) => {
83
+ try {
84
+ const user = this . context . getUser ( )
105
85
106
- bookApi . deleteBook ( user , isbn )
107
- . then ( ( ) => {
108
- this . handleGetBooks ( )
109
- } )
110
- . catch ( error => {
111
- handleLogError ( error )
112
- } )
86
+ this . setState ( { isBooksLoading : true } )
87
+ const response = await bookApi . getBooks ( user )
88
+
89
+ this . setState ( { books : response . data , isBooksLoading : false } )
90
+ } catch ( error ) {
91
+ handleLogError ( error )
92
+ this . setState ( { isBooksLoading : false } )
93
+ }
113
94
}
114
95
115
- handleAddBook = ( ) => {
116
- const Auth = this . context
117
- const user = Auth . getUser ( )
96
+ handleDeleteBook = async ( isbn ) => {
97
+ try {
98
+ const user = this . context . getUser ( )
118
99
119
- let { bookIsbn, bookTitle } = this . state
120
- bookIsbn = bookIsbn . trim ( )
121
- bookTitle = bookTitle . trim ( )
122
- if ( ! ( bookIsbn && bookTitle ) ) {
123
- return
100
+ await bookApi . deleteBook ( user , isbn )
101
+ this . handleGetBooks ( )
102
+ } catch ( error ) {
103
+ handleLogError ( error )
124
104
}
105
+ }
106
+
107
+ handleAddBook = async ( ) => {
108
+ try {
109
+ const user = this . context . getUser ( )
110
+
111
+ let { bookIsbn, bookTitle } = this . state
112
+ bookIsbn = bookIsbn . trim ( )
113
+ bookTitle = bookTitle . trim ( )
125
114
126
- const book = { isbn : bookIsbn , title : bookTitle }
127
- bookApi . addBook ( user , book )
128
- . then ( ( ) => {
129
- this . clearBookForm ( )
130
- this . handleGetBooks ( )
131
- } )
132
- . catch ( error => {
133
- handleLogError ( error )
134
- } )
115
+ if ( ! ( bookIsbn && bookTitle ) ) {
116
+ return
117
+ }
118
+
119
+ const book = { isbn : bookIsbn , title : bookTitle }
120
+ await bookApi . addBook ( user , book )
121
+ this . clearBookForm ( )
122
+ this . handleGetBooks ( )
123
+ } catch ( error ) {
124
+ handleLogError ( error )
125
+ }
135
126
}
136
127
137
- handleSearchBook = ( ) => {
138
- const Auth = this . context
139
- const user = Auth . getUser ( )
128
+ handleSearchBook = async ( ) => {
129
+ try {
130
+ const user = this . context . getUser ( )
131
+ const text = this . state . bookTextSearch
140
132
141
- const text = this . state . bookTextSearch
142
- bookApi . getBooks ( user , text )
143
- . then ( response => {
144
- const books = response . data
145
- this . setState ( { books } )
146
- } )
147
- . catch ( error => {
148
- handleLogError ( error )
149
- this . setState ( { books : [ ] } )
150
- } )
133
+ const response = await bookApi . getBooks ( user , text )
134
+ const books = response . data
135
+
136
+ this . setState ( { books } )
137
+ } catch ( error ) {
138
+ handleLogError ( error )
139
+ this . setState ( { books : [ ] } )
140
+ }
151
141
}
152
142
153
143
clearBookForm = ( ) => {
@@ -161,7 +151,7 @@ class AdminPage extends Component {
161
151
if ( ! this . state . isAdmin ) {
162
152
return < Navigate to = '/' />
163
153
}
164
-
154
+
165
155
const { isUsersLoading, users, userUsernameSearch, isBooksLoading, books, bookIsbn, bookTitle, bookTextSearch } = this . state
166
156
return (
167
157
< Container >
0 commit comments