@@ -16,6 +16,7 @@ import { PhaseService } from '../../core/services/phase.service';
1616import { UserService } from '../../core/services/user.service' ;
1717import { UndoActionComponent } from '../../shared/action-toasters/undo-action/undo-action.component' ;
1818import { IssuesDataTable } from './IssuesDataTable' ;
19+ import { Observable } from 'rxjs' ;
1920
2021export enum ACTION_BUTTONS {
2122 VIEW_IN_WEB ,
@@ -44,8 +45,7 @@ export class IssueTablesComponent implements OnInit, AfterViewInit {
4445 @ViewChild ( MatPaginator , { static : true } ) paginator : MatPaginator ;
4546
4647 issues : IssuesDataTable ;
47- issuesPendingDeletion : { [ id : number ] : boolean } ;
48- issuesPendingRestore : { [ id : number ] : boolean } ;
48+ issuesPendingAction : { [ id : number ] : boolean } ;
4949
5050 public tableSettings : TableSettings ;
5151
@@ -66,8 +66,7 @@ export class IssueTablesComponent implements OnInit, AfterViewInit {
6666
6767 ngOnInit ( ) {
6868 this . issues = new IssuesDataTable ( this . issueService , this . sort , this . paginator , this . headers , this . filters ) ;
69- this . issuesPendingDeletion = { } ;
70- this . issuesPendingRestore = { } ;
69+ this . issuesPendingAction = { } ;
7170 this . tableSettings = this . issueTableSettingsService . getTableSettings ( this . table_name ) ;
7271 }
7372
@@ -158,69 +157,85 @@ export class IssueTablesComponent implements OnInit, AfterViewInit {
158157 this . githubService . viewIssueInBrowser ( id , event ) ;
159158 }
160159
161- private handleIssueDeletionSuccess ( id : number , event : Event , actionUndoable : boolean ) {
162- if ( ! actionUndoable ) {
163- return ;
164- }
165- let snackBarRef = null ;
166- snackBarRef = this . snackBar . openFromComponent ( UndoActionComponent , {
167- data : { message : `Deleted issue ${ id } ` } ,
168- duration : this . snackBarAutoCloseTime
169- } ) ;
170- snackBarRef . onAction ( ) . subscribe ( ( ) => {
171- this . undeleteIssue ( id , event , false ) ;
172- } ) ;
173- }
160+ deleteOrRestoreIssue ( isDeleteAction : boolean , id : number , event : Event , actionUndoable : boolean = true ) {
161+ const deletingKeyword = 'Deleting' ;
162+ const undeletingKeyword = 'Undeleting' ;
163+ this . logger . info ( `IssueTablesComponent: ${ isDeleteAction ? deletingKeyword : undeletingKeyword } Issue ${ id } ` ) ;
174164
175- deleteIssue ( id : number , event : Event , actionUndoable : boolean = true ) {
176- this . logger . info ( `IssueTablesComponent: Deleting Issue ${ id } ` ) ;
165+ this . issuesPendingAction = { ...this . issuesPendingAction , [ id ] : true } ;
177166
178- this . issuesPendingDeletion = { ...this . issuesPendingDeletion , [ id ] : true } ;
179- this . issueService
180- . deleteIssue ( id )
167+ let observableActionedIssue : Observable < Issue > ;
168+ if ( isDeleteAction ) {
169+ observableActionedIssue = this . issueService . deleteIssue ( id ) ;
170+ } else {
171+ observableActionedIssue = this . issueService . undeleteIssue ( id ) ;
172+ }
173+ observableActionedIssue
181174 . pipe (
182175 finalize ( ( ) => {
183- const { [ id ] : issueRemoved , ...theRest } = this . issuesPendingDeletion ;
184- this . issuesPendingDeletion = theRest ;
176+ const { [ id ] : issueDeletedOrRestored , ...theRest } = this . issuesPendingAction ;
177+ this . issuesPendingAction = theRest ;
185178 } )
186179 )
187180 . subscribe (
188- ( removedIssue ) => this . handleIssueDeletionSuccess ( id , event , actionUndoable ) ,
181+ ( ) => this . handleIssueActionPerformedSuccess ( isDeleteAction , id , event , actionUndoable ) ,
189182 ( error ) => this . errorHandlingService . handleError ( error )
190183 ) ;
191184 event . stopPropagation ( ) ;
192185 }
193186
194- private handleIssueRestorationSuccess ( id : number , event : Event , actionUndoable : boolean ) {
187+ private handleIssueActionPerformedSuccess ( isDeleteAction : boolean , id : number , event : Event , actionUndoable : boolean ) {
188+ const deletedKeyword = 'Deleted' ;
189+ const restoredKeyword = 'Restored' ;
195190 if ( ! actionUndoable ) {
196191 return ;
197192 }
198193 let snackBarRef = null ;
199194 snackBarRef = this . snackBar . openFromComponent ( UndoActionComponent , {
200- data : { message : `Restored issue ${ id } ` } ,
195+ data : { message : `${ isDeleteAction ? deletedKeyword : restoredKeyword } issue ${ id } ` } ,
201196 duration : this . snackBarAutoCloseTime
202197 } ) ;
203198 snackBarRef . onAction ( ) . subscribe ( ( ) => {
204- this . deleteIssue ( id , event , false ) ;
199+ this . deleteOrRestoreIssue ( ! isDeleteAction , id , event , false ) ;
205200 } ) ;
206201 }
207202
208- undeleteIssue ( id : number , event : Event , actionUndoable : boolean = true ) {
209- this . logger . info ( `IssueTablesComponent: Undeleting Issue ${ id } ` ) ;
203+ isActionPerformAllowed ( isDeleteAction : boolean , id : number ) {
204+ const actionButton = isDeleteAction ? this . action_buttons . DELETE_ISSUE : this . action_buttons . RESTORE_ISSUE ;
205+ const isPermissionGranted = this . isIssueActionPermitted ( isDeleteAction ) ;
206+ return isPermissionGranted && ! this . issuesPendingAction [ id ] && this . isActionVisible ( actionButton ) ;
207+ }
210208
211- this . issuesPendingRestore = { ...this . issuesPendingRestore , [ id ] : true } ;
212- this . issueService
213- . undeleteIssue ( id )
214- . pipe (
215- finalize ( ( ) => {
216- const { [ id ] : issueRestored , ...theRest } = this . issuesPendingRestore ;
217- this . issuesPendingRestore = theRest ;
218- } )
219- )
220- . subscribe (
221- ( restoredIssue ) => this . handleIssueRestorationSuccess ( id , event , actionUndoable ) ,
222- ( error ) => this . errorHandlingService . handleError ( error )
223- ) ;
224- event . stopPropagation ( ) ;
209+ private isIssueActionPermitted ( isDeleteAction : boolean ) {
210+ if ( isDeleteAction ) {
211+ return this . permissions . isIssueDeletable ( ) ;
212+ }
213+ return this . permissions . isIssueRestorable ( ) ;
214+ }
215+
216+ shouldEnablePendingButton ( ) {
217+ return (
218+ ( this . userService . currentUser . role === 'Student' || this . userService . currentUser . role === 'Admin' ) &&
219+ this . isActionVisible ( this . action_buttons . MARK_AS_PENDING )
220+ ) ;
221+ }
222+
223+ shouldEnablePendingActionSpinner ( id : number ) {
224+ return (
225+ this . issuesPendingAction [ id ] &&
226+ ( this . isActionVisible ( this . action_buttons . DELETE_ISSUE ) || this . isActionVisible ( this . action_buttons . RESTORE_ISSUE ) )
227+ ) ;
228+ }
229+
230+ shouldEnableRespondToIssue ( issue : Issue ) {
231+ return this . isResponseEditable ( ) && ! issue . status && this . isActionVisible ( this . action_buttons . RESPOND_TO_ISSUE ) ;
232+ }
233+
234+ shouldEnableMarkAsResponded ( issue : Issue ) {
235+ return this . isResponseEditable ( ) && issue . status && this . isActionVisible ( this . action_buttons . MARK_AS_RESPONDED ) ;
236+ }
237+
238+ shouldEnableEditIssue ( ) {
239+ return this . permissions . isIssueEditable ( ) && this . isActionVisible ( this . action_buttons . FIX_ISSUE ) ;
225240 }
226241}
0 commit comments