Skip to content

Commit

Permalink
[#12749] Autosave and restore progress if user navigates away (#13140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Respirayson authored Jul 23, 2024
1 parent 61df45a commit 67628fb
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ <h2 class="question-details"><b>Question {{ model.questionNumber }}: </b>{{ mode
<tm-ajax-loading *ngIf="isSavingResponses"></tm-ajax-loading>
<span>{{ isQuestionCountOne ? "Submit Response" : "Submit Response for Question " + model.questionNumber }}</span>
</button>
<button type="button" class="btn btn-warning float-end" (click)="resetForm(); resetTooltip.close();" ngbTooltip="Reset your responses for this question to the last submitted state."
#resetTooltip="ngbTooltip" (mouseenter)="resetTooltip.open()" [disabled]="!hasResponseChanged">{{ "Reset Question " + model.questionNumber }}</button>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class QuestionSubmissionFormComponent implements DoCheck {

this.model.isTabExpandedForRecipients.set(recipient.recipientIdentifier, true);
});
this.hasResponseChanged = Array.from(this.model.hasResponseChangedForRecipients.values()).some((value) => value);
}

@Input()
Expand All @@ -118,6 +119,12 @@ export class QuestionSubmissionFormComponent implements DoCheck {
@Output()
responsesSave: EventEmitter<QuestionSubmissionFormModel> = new EventEmitter();

@Output()
autoSave: EventEmitter<{ id: string, model: QuestionSubmissionFormModel }> = new EventEmitter();

@Output()
resetFeedback: EventEmitter<QuestionSubmissionFormModel> = new EventEmitter<QuestionSubmissionFormModel>();

@ViewChild(ContributionQuestionConstraintComponent)
private contributionQuestionConstraint!: ContributionQuestionConstraintComponent;

Expand Down Expand Up @@ -168,6 +175,8 @@ export class QuestionSubmissionFormComponent implements DoCheck {
visibilityStateMachine: VisibilityStateMachine;
isEveryRecipientSorted: boolean = false;

autosaveTimeout: any;

constructor(private feedbackQuestionsService: FeedbackQuestionsService,
private feedbackResponseService: FeedbackResponsesService) {
this.visibilityStateMachine =
Expand Down Expand Up @@ -221,6 +230,13 @@ export class QuestionSubmissionFormComponent implements DoCheck {
});
}

resetForm(): void {
this.resetFeedback.emit(this.model);
this.isSaved = true;
this.hasResponseChanged = false;
clearTimeout(this.autosaveTimeout);
}

toggleQuestionTab(): void {
if (this.currentSelectedSessionView === this.allSessionViews.DEFAULT) {
this.model.isTabExpanded = !this.model.isTabExpanded;
Expand Down Expand Up @@ -350,7 +366,6 @@ export class QuestionSubmissionFormComponent implements DoCheck {
*/
triggerRecipientSubmissionFormChange(index: number, field: string, data: any): void {
if (!this.isFormsDisabled) {
this.hasResponseChanged = true;
this.isSubmitAllClickedChange.emit(false);
this.model.hasResponseChangedForRecipients.set(this.model.recipientList[index].recipientIdentifier, true);

Expand All @@ -362,6 +377,12 @@ export class QuestionSubmissionFormComponent implements DoCheck {

this.updateIsValidByQuestionConstraint();
this.formModelChange.emit(this.model);

this.autoSave.emit({ id: this.model.feedbackQuestionId, model: this.model });
clearTimeout(this.autosaveTimeout);
this.autosaveTimeout = setTimeout(() => {
this.hasResponseChanged = true;
}, 100); // 0.1 second to prevent people from trying to immediately reset before autosave kicks in
}
}

Expand Down Expand Up @@ -451,6 +472,7 @@ export class QuestionSubmissionFormComponent implements DoCheck {
* Triggers saving of responses for the specific question.
*/
saveFeedbackResponses(): void {
clearTimeout(this.autosaveTimeout);
this.isSaved = true;
this.hasResponseChanged = false;
this.model.hasResponseChangedForRecipients.forEach(
Expand Down
Loading

0 comments on commit 67628fb

Please sign in to comment.