Skip to content

Commit 95756b5

Browse files
committed
MOBILE-4616 feedback: Fix tab selection after submit form
1 parent ce7243b commit 95756b5

File tree

9 files changed

+130
-88
lines changed

9 files changed

+130
-88
lines changed

src/addons/mod/feedback/components/index/addon-mod-feedback-index.html

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,13 @@
2020
[courseId]="courseId" [hasDataToSync]="hasOffline" (completionChanged)="onCompletionChange()" />
2121

2222
<core-tabs [hideUntil]="tabsReady" [selectedIndex]="firstSelectedTab">
23-
<core-tab [title]="'addon.mod_feedback.overview' | translate" id="overview" (ionSelect)="tabChanged('overview')">
23+
<core-tab [title]="tabs.overview.label | translate" [id]="tabs.overview.name" (ionSelect)="tabChanged(tabs.overview.name)">
2424
<ng-template>
2525
<ng-container *ngTemplateOutlet="tabOverview" />
2626
</ng-template>
2727
</core-tab>
28-
<core-tab *ngIf="showAnalysis && access && access.canviewreports" id="analysis" [title]="'addon.mod_feedback.analysis' | translate"
29-
(ionSelect)="tabChanged('analysis')">
30-
<ng-template>
31-
<ng-container *ngTemplateOutlet="tabAnalysis" />
32-
</ng-template>
33-
</core-tab>
34-
35-
<core-tab *ngIf="showAnalysis && access && !access.canviewreports" id="analysis"
36-
[title]="'addon.mod_feedback.completed_feedbacks' | translate" (ionSelect)="tabChanged('analysis')">
28+
<core-tab *ngIf="showAnalysis && access" [id]="tabs.analysis.name" [title]="tabs.analysis.label | translate"
29+
(ionSelect)="tabChanged(tabs.analysis.name)">
3730
<ng-template>
3831
<ng-container *ngTemplateOutlet="tabAnalysis" />
3932
</ng-template>
@@ -101,7 +94,7 @@
10194

10295
<!-- Template to render the overview. -->
10396
<ng-template #tabOverview>
104-
<core-loading [hideUntil]="tabsLoaded.overview">
97+
<core-loading [hideUntil]="tabs.overview.loaded">
10598
<ng-container *ngTemplateOutlet="basicInfo" />
10699

107100
<ion-card class="core-info-card" *ngIf="access && access.cancomplete && !access.isopen">
@@ -153,7 +146,7 @@
153146

154147
<!-- Template to render the analysis. -->
155148
<ng-template #tabAnalysis>
156-
<core-loading [hideUntil]="tabsLoaded.analysis">
149+
<core-loading [hideUntil]="tabs.analysis.loaded">
157150
<ng-container *ngTemplateOutlet="basicInfo" />
158151

159152
<ng-container *ngIf="access && (access.canedititems || !access.isempty)">

src/addons/mod/feedback/components/index/index.ts

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ import {
3838
AddonModFeedbackSyncResult,
3939
} from '../../services/feedback-sync';
4040
import { AddonModFeedbackPrefetchHandler } from '../../services/handlers/prefetch';
41-
import { ADDON_MOD_FEEDBACK_COMPONENT, ADDON_MOD_FEEDBACK_FORM_SUBMITTED, ADDON_MOD_FEEDBACK_PAGE_NAME } from '../../constants';
41+
import {
42+
ADDON_MOD_FEEDBACK_COMPONENT,
43+
ADDON_MOD_FEEDBACK_FORM_SUBMITTED,
44+
ADDON_MOD_FEEDBACK_PAGE_NAME,
45+
AddonModFeedbackIndexTabName,
46+
} from '../../constants';
4247

4348
/**
4449
* Component that displays a feedback index page.
@@ -51,7 +56,7 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
5156

5257
@ViewChild(CoreTabsComponent) tabsComponent?: CoreTabsComponent;
5358

54-
@Input() tab = 'overview';
59+
@Input() selectedTab: AddonModFeedbackIndexTabName = AddonModFeedbackIndexTabName.OVERVIEW;
5560
@Input() group = 0;
5661

5762
component = ADDON_MOD_FEEDBACK_COMPONENT;
@@ -75,9 +80,9 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
7580
closeTimeReadable: '',
7681
};
7782

78-
tabsLoaded = {
79-
overview: false,
80-
analysis: false,
83+
tabs = {
84+
overview: { name: AddonModFeedbackIndexTabName.OVERVIEW, label: 'addon.mod_feedback.overview', loaded: false },
85+
analysis: { name: AddonModFeedbackIndexTabName.ANALYSIS, label: 'addon.mod_feedback.analysis', loaded: false },
8186
};
8287

8388
protected submitObserver: CoreEventObserver;
@@ -92,12 +97,12 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
9297

9398
// Listen for form submit events.
9499
this.submitObserver = CoreEvents.on(ADDON_MOD_FEEDBACK_FORM_SUBMITTED, async (data) => {
95-
if (!this.feedback || data.feedbackId != this.feedback.id) {
100+
if (!this.feedback || data.feedbackId !== this.feedback.id) {
96101
return;
97102
}
98103

99-
this.tabsLoaded.analysis = false;
100-
this.tabsLoaded.overview = false;
104+
this.tabs.analysis.loaded = false;
105+
this.tabs.overview.loaded = false;
101106
this.showLoading = true;
102107

103108
// Prefetch data if needed.
@@ -110,7 +115,7 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
110115
}
111116

112117
// Load the right tab.
113-
if (data.tab != this.tab) {
118+
if (data.tab !== this.selectedTab) {
114119
this.tabChanged(data.tab);
115120
} else {
116121
this.loadContent(true);
@@ -149,7 +154,9 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
149154
*/
150155
protected callAnalyticsLogEvent(): void {
151156
this.analyticsLogEvent('mod_feedback_view_feedback', {
152-
url: this.tab === 'analysis' ? `/mod/feedback/analysis.php?id=${this.module.id}` : undefined,
157+
url: this.selectedTab === AddonModFeedbackIndexTabName.ANALYSIS
158+
? `/mod/feedback/analysis.php?id=${this.module.id}`
159+
: undefined,
153160
});
154161
}
155162

@@ -168,8 +175,8 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
168175
promises.push(AddonModFeedback.invalidateResumePageData(this.feedback.id));
169176
}
170177

171-
this.tabsLoaded.analysis = false;
172-
this.tabsLoaded.overview = false;
178+
this.tabs.analysis.loaded = false;
179+
this.tabs.overview.loaded = false;
173180

174181
await Promise.all(promises);
175182
}
@@ -178,7 +185,7 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
178185
* @inheritdoc
179186
*/
180187
protected isRefreshSyncNeeded(syncEventData: AddonModFeedbackAutoSyncData): boolean {
181-
if (this.feedback && syncEventData.feedbackId == this.feedback.id) {
188+
if (syncEventData.feedbackId === this.feedback?.id) {
182189
// Refresh the data.
183190
this.content?.scrollToTop();
184191

@@ -207,12 +214,17 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
207214
this.access = await AddonModFeedback.getFeedbackAccessInformation(this.feedback.id, { cmId: this.module.id });
208215

209216
this.showAnalysis = (this.access.canviewreports || this.access.canviewanalysis) && !this.access.isempty;
217+
218+
this.tabs.analysis.label = this.access.canviewreports
219+
? 'addon.mod_feedback.analysis'
220+
: 'addon.mod_feedback.completed_feedbacks';
221+
210222
this.firstSelectedTab = 0;
211223
if (!this.showAnalysis) {
212-
this.tab = 'overview';
224+
this.selectedTab = AddonModFeedbackIndexTabName.OVERVIEW;
213225
}
214226

215-
if (this.tab == 'analysis') {
227+
if (this.selectedTab === AddonModFeedbackIndexTabName.ANALYSIS) {
216228
this.firstSelectedTab = 1;
217229

218230
return await this.fetchFeedbackAnalysisData();
@@ -227,42 +239,44 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
227239

228240
if (this.tabsReady) {
229241
// Make sure the right tab is selected.
230-
this.tabsComponent?.selectTab(this.tab || 'overview');
242+
this.tabsComponent?.selectTab(this.selectedTab ?? AddonModFeedbackIndexTabName.OVERVIEW);
231243
}
232244
}
233245
}
234246

235247
/**
236248
* Convenience function to get feedback overview data.
237-
*
238-
* @returns Resolved when done.
239249
*/
240250
protected async fetchFeedbackOverviewData(): Promise<void> {
251+
if (!this.access || !this.feedback) {
252+
return;
253+
}
254+
241255
const promises: Promise<void>[] = [];
242256

243-
if (this.access!.cancomplete && this.access!.cansubmit && this.access!.isopen) {
244-
promises.push(AddonModFeedback.getResumePage(this.feedback!.id, { cmId: this.module.id }).then((goPage) => {
257+
if (this.access.cancomplete && this.access.cansubmit && this.access.isopen) {
258+
promises.push(AddonModFeedback.getResumePage(this.feedback.id, { cmId: this.module.id }).then((goPage) => {
245259
this.goPage = goPage > 0 ? goPage : undefined;
246260

247261
return;
248262
}));
249263
}
250264

251-
if (this.access!.canedititems) {
252-
this.overview.timeopen = (this.feedback!.timeopen || 0) * 1000;
265+
if (this.access.canedititems) {
266+
this.overview.timeopen = (this.feedback.timeopen || 0) * 1000;
253267
this.overview.openTimeReadable = this.overview.timeopen ? CoreTimeUtils.userDate(this.overview.timeopen) : '';
254-
this.overview.timeclose = (this.feedback!.timeclose || 0) * 1000;
268+
this.overview.timeclose = (this.feedback.timeclose || 0) * 1000;
255269
this.overview.closeTimeReadable = this.overview.timeclose ? CoreTimeUtils.userDate(this.overview.timeclose) : '';
256270
}
257-
if (this.access!.canviewanalysis) {
271+
if (this.access.canviewanalysis) {
258272
// Get groups (only for teachers).
259273
promises.push(this.fetchGroupInfo(this.module.id));
260274
}
261275

262276
try {
263277
await Promise.all(promises);
264278
} finally {
265-
this.tabsLoaded.overview = true;
279+
this.tabs.overview.loaded = true;
266280
}
267281
}
268282

@@ -273,15 +287,14 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
273287
*/
274288
protected async fetchFeedbackAnalysisData(): Promise<void> {
275289
try {
276-
if (this.access!.canviewanalysis) {
290+
if (this.access?.canviewanalysis) {
277291
// Get groups (only for teachers).
278292
await this.fetchGroupInfo(this.module.id);
279293
} else {
280-
this.tabChanged('overview');
294+
this.tabChanged(AddonModFeedbackIndexTabName.OVERVIEW);
281295
}
282-
283296
} finally {
284-
this.tabsLoaded.analysis = true;
297+
this.tabs.analysis.loaded = true;
285298
}
286299
}
287300

@@ -419,7 +432,7 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
419432
* Open attempts page.
420433
*/
421434
openAttempts(): void {
422-
if (!this.access!.canviewreports || this.completedCount <= 0) {
435+
if (!this.access || !this.access.canviewreports || this.completedCount <= 0) {
423436
return;
424437
}
425438

@@ -438,11 +451,11 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
438451
*
439452
* @param tabName New tab name.
440453
*/
441-
tabChanged(tabName: string): void {
442-
const tabHasChanged = this.tab !== undefined && this.tab !== tabName;
443-
this.tab = tabName;
454+
tabChanged(tabName: AddonModFeedbackIndexTabName): void {
455+
const tabHasChanged = this.selectedTab !== undefined && this.selectedTab !== tabName;
456+
this.selectedTab = tabName;
444457

445-
if (!this.tabsLoaded[this.tab]) {
458+
if (!this.tabs[this.selectedTab].loaded) {
446459
this.loadContent(false, false, true);
447460
}
448461

@@ -455,17 +468,20 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity
455468
* Set group to see the analysis.
456469
*
457470
* @param groupId Group ID.
458-
* @returns Resolved when done.
459471
*/
460472
async setGroup(groupId: number): Promise<void> {
473+
if (!this.feedback) {
474+
return;
475+
}
476+
461477
this.group = groupId;
462478

463-
const analysis = await AddonModFeedback.getAnalysis(this.feedback!.id, { groupId, cmId: this.module.id });
479+
const analysis = await AddonModFeedback.getAnalysis(this.feedback.id, { groupId, cmId: this.module.id });
464480

465481
this.completedCount = analysis.completedcount;
466482
this.itemsCount = analysis.itemscount;
467483

468-
if (this.tab == 'analysis') {
484+
if (this.selectedTab === AddonModFeedbackIndexTabName.ANALYSIS) {
469485
let num = 1;
470486

471487
this.items = <AddonModFeedbackItem[]> analysis.itemsdata.map((itemData) => {

src/addons/mod/feedback/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ export const ADDON_MOD_FEEDBACK_MULTICHOICE_HIDENOSELECT = 'h';
2424
export const ADDON_MOD_FEEDBACK_MULTICHOICERATED_VALUE_SEP = '####';
2525

2626
export const ADDON_MOD_FEEDBACK_PER_PAGE = 20;
27+
28+
/**
29+
* Index Tabs.
30+
*/
31+
export enum AddonModFeedbackIndexTabName {
32+
OVERVIEW = 'overview',
33+
ANALYSIS = 'analysis',
34+
}

0 commit comments

Comments
 (0)