@@ -6,68 +6,95 @@ import FetchUtil from "../utils/fetchUtil";
66import sweetAlert from "../../../node_modules/bootstrap-sweetalert/dev/sweetalert.es6.js" ; // eslint-disable-line max-len
77
88export default class MajorProjectStatus {
9- constructor ( dropdown ) {
10- this . dropdown = dropdown ;
11- this . id = this . dropdown . dataset . id ;
9+ constructor ( control ) {
10+ this . control = control ;
11+ this . id = this . control . dataset . id ;
1212 this . endpoint = '/major_project/review' ;
13+ this . deleteEndpoint = '/major_project/delete/' ;
1314 this . render ( ) ;
1415 }
1516
1617 render ( ) {
17- const options = this . dropdown . querySelectorAll ( '[data-option]' ) ;
18- options . forEach ( option => {
19- option . addEventListener ( 'click' , e => this . _changeStatus ( e ) ) ;
20- } ) ;
18+ if ( this . control . tagName . toLowerCase ( ) === "div" ) {
19+ // Evals director dropdown
20+ const options = this . control . querySelectorAll ( '[data-option]' ) ;
21+ options . forEach ( option => {
22+ option . addEventListener ( 'click' , e => {
23+ e . preventDefault ( ) ;
24+ this . _changeStatus ( e . target . dataset . option ) ;
25+ } ) ;
26+ } ) ;
27+ } else {
28+ // Member self-delete button
29+ this . control . addEventListener ( 'click' ,
30+ ( ) => this . _changeStatus ( 'Delete' ) ) ;
31+ }
2132 }
2233
23- _changeStatus ( e ) {
24- e . preventDefault ( ) ;
25-
26- let option = e . target . dataset . option ;
27- let payload = {
28- id : this . id ,
29- status : option
30- } ;
31-
32- fetch ( this . endpoint , {
33- method : 'POST' ,
34- headers : {
35- 'Accept' : 'application/json' ,
36- 'Content-Type' : 'application/json'
37- } ,
38- credentials : "same-origin" ,
39- body : JSON . stringify ( payload )
40- } )
41- . then ( FetchUtil . checkStatus )
42- . then ( FetchUtil . parseJSON )
43- . then ( response => {
44- if ( response . hasOwnProperty ( 'success' ) && response . success === true ) {
45- let toggle = this . dropdown . querySelector ( '.dropdown-toggle' ) ;
46- [ "btn-success" , "btn-danger" , "btn-warning" ] . forEach ( classToRemove =>
47- toggle . classList . remove ( classToRemove ) ) ;
48-
49- const caret = document . createElement ( 'span' ) ;
50- caret . classList . add ( 'caret' ) ;
51- toggle . text = option + " " ;
52- toggle . appendChild ( caret ) ;
53-
54- if ( option === "Passed" ) {
55- toggle . classList . add ( "btn-success" ) ;
56- } else if ( option === "Failed" ) {
57- toggle . classList . add ( "btn-danger" ) ;
58- } else {
59- toggle . classList . add ( "btn-warning" ) ;
60- }
34+ _changeStatus ( option ) {
35+ if ( option === "Delete" ) {
36+ FetchUtil . fetchWithWarning ( this . deleteEndpoint + this . id , {
37+ method : 'DELETE' ,
38+ warningText : 'This action cannot be undone.' ,
39+ successText : 'Major project deleted.'
40+ } , ( ) => {
41+ let dashboardContainer = this . control . closest ( ".mp-container" ) ;
42+ if ( dashboardContainer ) {
43+ // Dashboard button
44+ $ ( dashboardContainer ) . hide ( ) ;
6145 } else {
62- sweetAlert ( "Uh oh..." , "We're having trouble updating this project " +
63- "right now. Please try again later." , "error" ) ;
64- throw new Exception ( FetchException . REQUEST_FAILED , response ) ;
46+ // Major projects page button
47+ $ ( this . control . closest ( ".panel" ) ) . fadeOut ( ) ;
6548 }
66- } )
67- . catch ( error => {
68- sweetAlert ( "Uh oh..." , "We're having trouble updating this project " +
69- "right now. Please try again later." , "error" ) ;
70- throw new Exception ( FetchException . REQUEST_FAILED , error ) ;
7149 } ) ;
50+ } else {
51+ let payload = {
52+ id : this . id ,
53+ status : option
54+ } ;
55+
56+ fetch ( this . endpoint , {
57+ method : 'POST' ,
58+ headers : {
59+ 'Accept' : 'application/json' ,
60+ 'Content-Type' : 'application/json'
61+ } ,
62+ credentials : "same-origin" ,
63+ body : JSON . stringify ( payload )
64+ } )
65+ . then ( FetchUtil . checkStatus )
66+ . then ( FetchUtil . parseJSON )
67+ . then ( response => {
68+ if ( response . hasOwnProperty ( 'success' ) &&
69+ response . success === true ) {
70+ let toggle = this . control . querySelector ( '.dropdown-toggle' ) ;
71+ [ "btn-success" , "btn-danger" , "btn-warning" ]
72+ . forEach ( classToRemove =>
73+ toggle . classList . remove ( classToRemove ) ) ;
74+
75+ const caret = document . createElement ( 'span' ) ;
76+ caret . classList . add ( 'caret' ) ;
77+ toggle . text = option + " " ;
78+ toggle . appendChild ( caret ) ;
79+
80+ if ( option === "Passed" ) {
81+ toggle . classList . add ( "btn-success" ) ;
82+ } else if ( option === "Failed" ) {
83+ toggle . classList . add ( "btn-danger" ) ;
84+ } else {
85+ toggle . classList . add ( "btn-warning" ) ;
86+ }
87+ } else {
88+ sweetAlert ( "Uh oh..." , "We're having trouble updating " +
89+ "this project right now. Please try again later." , "error" ) ;
90+ throw new Exception ( FetchException . REQUEST_FAILED , response ) ;
91+ }
92+ } )
93+ . catch ( error => {
94+ sweetAlert ( "Uh oh..." , "We're having trouble updating " +
95+ "this project right now. Please try again later." , "error" ) ;
96+ throw new Exception ( FetchException . REQUEST_FAILED , error ) ;
97+ } ) ;
98+ }
7299 }
73100}
0 commit comments