Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cancel dialog confirmation check to service #1135

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/app/core/services/issue.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import { LoggingService } from './logging.service';
import { PhaseService } from './phase.service';
import { UserService } from './user.service';

/**
* An interface that provides abstraction for components that can cancel
* edits.
*/
export interface EditCancellable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid exporting other interfaces/classes in service files.

openCancelDialog(): void;
cancelEditMode(): void;
}

@Injectable({
providedIn: 'root'
})
Expand Down Expand Up @@ -49,6 +58,22 @@ export class IssueService {
this.issues$ = new BehaviorSubject(new Array<Issue>());
}

/**
* Method that cancels the display of the confirmation dialog box if issue is
* not modified.
* @param component Component that implements `EditCancellable`
* @param isModified Callback function that checks if the field has actually been modified
*/
openCancelDialogIfModified<T extends EditCancellable>(component: T, isModified: () => boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the start of the file, you will see that the IResponsible for creating and updating issues, and periodically fetching issues using GitHub. It would be better to have this method under another file.

if (isModified()) {
// if the initial field has been changed, request user to confirm the cancellation
component.openCancelDialog();
} else {
// if no changes have been made, simply cancel edit mode without getting confirmation
component.cancelEditMode();
}
}

startPollIssues() {
if (this.issuesPollSubscription === undefined) {
if (this.issues$.getValue().length === 0) {
Expand Down
13 changes: 4 additions & 9 deletions src/app/shared/issue/description/description.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Conflict } from '../../../core/models/conflict/conflict.model';
import { Issue } from '../../../core/models/issue.model';
import { DialogService } from '../../../core/services/dialog.service';
import { ErrorHandlingService } from '../../../core/services/error-handling.service';
import { IssueService } from '../../../core/services/issue.service';
import { EditCancellable, IssueService } from '../../../core/services/issue.service';
import { PermissionService } from '../../../core/services/permission.service';
import { PhaseService } from '../../../core/services/phase.service';
import { SUBMIT_BUTTON_TEXT } from '../../view-issue/view-issue.component';
Expand All @@ -18,7 +18,7 @@ import { ConflictDialogComponent } from '../conflict-dialog/conflict-dialog.comp
templateUrl: './description.component.html',
styleUrls: ['./description.component.css']
})
export class DescriptionComponent implements OnInit {
export class DescriptionComponent implements OnInit, EditCancellable {
isSavePending = false;
issueDescriptionForm: FormGroup;
conflict: Conflict;
Expand Down Expand Up @@ -120,13 +120,8 @@ export class DescriptionComponent implements OnInit {

openCancelDialogIfModified(): void {
const issueDescriptionInitialValue = this.issue.description || '';
if (this.issueDescriptionForm.get('description').value !== issueDescriptionInitialValue) {
// if the description has been edited, request user to confirm the cancellation
this.openCancelDialog();
} else {
// if no changes have been made, simply cancel edit mode without getting confirmation
this.cancelEditMode();
}
const isModified = () => this.issueDescriptionForm.get('description').value !== issueDescriptionInitialValue;
this.issueService.openCancelDialogIfModified(this, isModified);
}

openCancelDialog(): void {
Expand Down
13 changes: 4 additions & 9 deletions src/app/shared/issue/title/title.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { finalize } from 'rxjs/operators';
import { Issue } from '../../../core/models/issue.model';
import { DialogService } from '../../../core/services/dialog.service';
import { ErrorHandlingService } from '../../../core/services/error-handling.service';
import { IssueService } from '../../../core/services/issue.service';
import { EditCancellable, IssueService } from '../../../core/services/issue.service';
import { PermissionService } from '../../../core/services/permission.service';
import { PhaseService } from '../../../core/services/phase.service';

Expand All @@ -13,7 +13,7 @@ import { PhaseService } from '../../../core/services/phase.service';
templateUrl: './title.component.html',
styleUrls: ['./title.component.css']
})
export class TitleComponent implements OnInit {
export class TitleComponent implements OnInit, EditCancellable {
isEditing = false;
isSavePending = false;
issueTitleForm: FormGroup;
Expand Down Expand Up @@ -82,13 +82,8 @@ export class TitleComponent implements OnInit {

openCancelDialogIfModified(): void {
const issueTitleInitialValue = this.issue.title || '';
if (this.issueTitleForm.get('title').value !== issueTitleInitialValue) {
// if the title has been edited, request user to confirm the cancellation
this.openCancelDialog();
} else {
// if no changes have been made, simply cancel edit mode without getting confirmation
this.cancelEditMode();
}
const isModified = () => this.issueTitleForm.get('title').value !== issueTitleInitialValue;
this.issueService.openCancelDialogIfModified(this, isModified);
}

openCancelDialog(): void {
Expand Down
14 changes: 4 additions & 10 deletions src/app/shared/view-issue/team-response/team-response.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Conflict } from '../../../core/models/conflict/conflict.model';
import { Issue, STATUS } from '../../../core/models/issue.model';
import { DialogService } from '../../../core/services/dialog.service';
import { ErrorHandlingService } from '../../../core/services/error-handling.service';
import { IssueService } from '../../../core/services/issue.service';
import { EditCancellable, IssueService } from '../../../core/services/issue.service';
import { PermissionService } from '../../../core/services/permission.service';
import { PhaseService } from '../../../core/services/phase.service';
import { ConflictDialogComponent } from '../../issue/conflict-dialog/conflict-dialog.component';
Expand All @@ -19,7 +19,7 @@ import { SUBMIT_BUTTON_TEXT } from '../view-issue.component';
templateUrl: './team-response.component.html',
styleUrls: ['./team-response.component.css']
})
export class TeamResponseComponent implements OnInit {
export class TeamResponseComponent implements OnInit, EditCancellable {
isSavePending = false;
responseForm: FormGroup;
conflict: Conflict;
Expand Down Expand Up @@ -150,14 +150,8 @@ export class TeamResponseComponent implements OnInit {

openCancelDialogIfModified(): void {
const issueTeamResponseInitialValue = this.issue.teamResponse || '';

if (this.responseForm.get('description').value !== issueTeamResponseInitialValue) {
// if the response has been edited, request user to confirm the cancellation
this.openCancelDialog();
} else {
// if no changes have been made, simply cancel edit mode without getting confirmation
this.cancelEditMode();
}
const isModified = () => this.responseForm.get('description').value !== issueTeamResponseInitialValue;
this.issueService.openCancelDialogIfModified(this, isModified);
}

openCancelDialog(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TesterResponse } from '../../../core/models/tester-response.model';
import { UserRole } from '../../../core/models/user.model';
import { DialogService } from '../../../core/services/dialog.service';
import { ErrorHandlingService } from '../../../core/services/error-handling.service';
import { IssueService } from '../../../core/services/issue.service';
import { EditCancellable, IssueService } from '../../../core/services/issue.service';
import { PhaseService } from '../../../core/services/phase.service';
import { UserService } from '../../../core/services/user.service';
import { CommentEditorComponent } from '../../comment-editor/comment-editor.component';
Expand All @@ -21,7 +21,7 @@ import { ConflictDialogComponent, TesterResponseConflictData } from './conflict-
templateUrl: './tester-response.component.html',
styleUrls: ['./tester-response.component.css']
})
export class TesterResponseComponent implements OnInit, OnChanges {
export class TesterResponseComponent implements OnInit, OnChanges, EditCancellable {
testerResponseForm: FormGroup;
isFormPending = false;

Expand Down Expand Up @@ -159,16 +159,8 @@ export class TesterResponseComponent implements OnInit, OnChanges {
})
.reduce((a, b) => a || b, false);

const isModified = reasonForDisagreementIsModified || disagreementIsModified;

if (isModified) {
// if the disagreement decision and/or reason for disagreement of any response has been edited,
// request user to confirm the cancellation
this.openCancelDialog();
} else {
// if no changes have been made, simply cancel edit mode without getting confirmation
this.cancelEditMode();
}
const isModified = () => reasonForDisagreementIsModified || disagreementIsModified;
this.issueService.openCancelDialogIfModified(this, isModified);
}

openCancelDialog(): void {
Expand Down