1515import { Component , OnInit , OnDestroy , Injector , Input , OnChanges , SimpleChange } from '@angular/core' ;
1616import { CoreEventsProvider } from '@providers/events' ;
1717import { CoreSitesProvider } from '@providers/sites' ;
18- import { CoreCoursesProvider } from '@core/courses/providers/courses' ;
18+ import { CoreCoursesProvider , CoreCoursesMyCoursesUpdatedEventData } from '@core/courses/providers/courses' ;
1919import { CoreCoursesHelperProvider } from '@core/courses/providers/helper' ;
2020import { CoreCourseHelperProvider } from '@core/course/providers/helper' ;
2121import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate' ;
@@ -72,8 +72,12 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
7272
7373 } , this . sitesProvider . getCurrentSiteId ( ) ) ;
7474
75- this . coursesObserver = this . eventsProvider . on ( CoreCoursesProvider . EVENT_MY_COURSES_UPDATED , ( ) => {
76- this . refreshContent ( ) ;
75+ this . coursesObserver = this . eventsProvider . on ( CoreCoursesProvider . EVENT_MY_COURSES_UPDATED ,
76+ ( data : CoreCoursesMyCoursesUpdatedEventData ) => {
77+
78+ if ( this . shouldRefreshOnUpdatedEvent ( data ) ) {
79+ this . refreshCourseList ( ) ;
80+ }
7781 } , this . sitesProvider . getCurrentSiteId ( ) ) ;
7882
7983 super . ngOnInit ( ) ;
@@ -130,6 +134,23 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
130134 } ) ;
131135 }
132136
137+ /**
138+ * Refresh the list of courses.
139+ *
140+ * @return Promise resolved when done.
141+ */
142+ protected async refreshCourseList ( ) : Promise < void > {
143+ this . eventsProvider . trigger ( CoreCoursesProvider . EVENT_MY_COURSES_REFRESHED ) ;
144+
145+ try {
146+ await this . coursesProvider . invalidateUserCourses ( ) ;
147+ } catch ( error ) {
148+ // Ignore errors.
149+ }
150+
151+ await this . loadContent ( true ) ;
152+ }
153+
133154 /**
134155 * Initialize the prefetch icon for selected courses.
135156 */
@@ -146,6 +167,49 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
146167 } ) ;
147168 }
148169
170+ /**
171+ * Whether list should be refreshed based on a EVENT_MY_COURSES_UPDATED event.
172+ *
173+ * @param data Event data.
174+ * @return Whether to refresh.
175+ */
176+ protected shouldRefreshOnUpdatedEvent ( data : CoreCoursesMyCoursesUpdatedEventData ) : boolean {
177+ if ( data . action == CoreCoursesProvider . ACTION_ENROL ) {
178+ // Always update if user enrolled in a course.
179+ return true ;
180+ }
181+
182+ if ( data . action == CoreCoursesProvider . ACTION_VIEW && data . courseId != this . sitesProvider . getCurrentSiteHomeId ( ) &&
183+ this . courses [ 0 ] && data . courseId != this . courses [ 0 ] . id ) {
184+ // Update list if user viewed a course that isn't the most recent one and isn't site home.
185+ return true ;
186+ }
187+
188+ if ( data . action == CoreCoursesProvider . ACTION_STATE_CHANGED && data . state == CoreCoursesProvider . STATE_FAVOURITE &&
189+ this . hasCourse ( data . courseId ) ) {
190+ // Update list if a visible course is now favourite or unfavourite.
191+ return true ;
192+ }
193+
194+ return false ;
195+ }
196+
197+ /**
198+ * Check if a certain course is in the list of courses.
199+ *
200+ * @param courseId Course ID to search.
201+ * @return Whether it's in the list.
202+ */
203+ protected hasCourse ( courseId : number ) : boolean {
204+ if ( ! this . courses ) {
205+ return false ;
206+ }
207+
208+ return ! ! this . courses . find ( ( course ) => {
209+ return course . id == courseId ;
210+ } ) ;
211+ }
212+
149213 /**
150214 * Prefetch all the shown courses.
151215 *
0 commit comments