11/**
22 * Functions for handling user management
33 *
4- * @todo move fetch() functionality to api functions
5- *
64 * This Source Code Form is subject to the terms of the Mozilla Public License,
75 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
86 * obtain one at https://mozilla.org/MPL/2.0/.
1614 */
1715
1816import { addElement , pushErrorNotification , pushNotification } from '../../../../assets/src/utils' ;
19- import { deleteUser } from '../api' ;
17+ import { activateUser , deleteUser } from '../api' ;
2018import { Modal } from 'bootstrap' ;
21-
22- const activateUser = async ( userId : string , csrfToken : string ) : Promise < void > => {
23- try {
24- const response = await fetch ( './api/user/activate' , {
25- method : 'POST' ,
26- headers : {
27- Accept : 'application/json, text/plain, */*' ,
28- 'Content-Type' : 'application/json' ,
29- } ,
30- body : JSON . stringify ( {
31- csrfToken : csrfToken ,
32- userId : userId ,
33- } ) ,
34- } ) ;
35-
36- if ( response . status === 200 ) {
37- await response . json ( ) ;
38- const icon = document . querySelector ( `.icon_user_id_${ userId } ` ) as HTMLElement ;
39- icon . classList . remove ( 'bi-ban' ) ;
40- icon . classList . add ( 'bi-check-circle-o' ) ;
41- const button = document . getElementById ( `btn_activate_user_id_${ userId } ` ) as HTMLElement ;
42- button . remove ( ) ;
43- } else {
44- throw new Error ( 'Network response was not ok.' ) ;
45- }
46- } catch ( error ) {
47- const message = document . getElementById ( 'pmf-user-message' ) as HTMLElement ;
48- message . insertAdjacentElement (
49- 'afterend' ,
50- addElement ( 'div' , { classList : 'alert alert-danger' , innerText : ( error as Error ) . message } )
51- ) ;
52- }
53- } ;
19+ import { Response } from '../interfaces' ;
5420
5521export const handleUserList = ( ) : void => {
5622 const activateButtons = document . querySelectorAll ( '.btn-activate-user' ) ;
@@ -65,7 +31,21 @@ export const handleUserList = (): void => {
6531 const csrfToken = target . getAttribute ( 'data-csrf-token' ) ! ;
6632 const userId = target . getAttribute ( 'data-user-id' ) ! ;
6733
68- await activateUser ( userId , csrfToken ) ;
34+ const response = ( await activateUser ( userId , csrfToken ) ) as unknown as Response ;
35+
36+ if ( typeof response . success === 'string' ) {
37+ const icon = document . querySelector ( `.icon_user_id_${ userId } ` ) as HTMLElement ;
38+ icon . classList . remove ( 'bi-ban' ) ;
39+ icon . classList . add ( 'bi-check-circle-o' ) ;
40+ const button = document . getElementById ( `btn_activate_user_id_${ userId } ` ) as HTMLElement ;
41+ button . remove ( ) ;
42+ } else {
43+ const message = document . getElementById ( 'pmf-user-message' ) as HTMLElement ;
44+ message . insertAdjacentElement (
45+ 'afterend' ,
46+ addElement ( 'div' , { classList : 'alert alert-danger' , innerText : response . error } )
47+ ) ;
48+ }
6949 } ) ;
7050 } ) ;
7151 }
@@ -94,15 +74,14 @@ export const handleUserList = (): void => {
9474 if ( source . value === 'user-list' ) {
9575 const userId = ( document . getElementById ( 'pmf-user-id-delete' ) as HTMLInputElement ) . value ;
9676 const csrfToken = ( document . getElementById ( 'csrf-token-delete-user' ) as HTMLInputElement ) . value ;
97- const response = await deleteUser ( userId , csrfToken ) ;
98- const json = await response . json ( ) ;
99- if ( json . success ) {
100- pushNotification ( json . success ) ;
77+ const response = ( await deleteUser ( userId , csrfToken ) ) as unknown as Response ;
78+ if ( response . success ) {
79+ pushNotification ( response . success ) ;
10180 const row = document . getElementById ( 'row_user_id_' + userId ) as HTMLElement ;
10281 row . remove ( ) ;
10382 }
104- if ( json . error ) {
105- pushErrorNotification ( json . error ) ;
83+ if ( response . error ) {
84+ pushErrorNotification ( response . error ) ;
10685 }
10786 }
10887 } ) ;
0 commit comments