|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { Component, OnInit } from '@angular/core'; |
| 16 | +import { CoreDomUtils } from '@services/utils/dom'; |
| 17 | +import { CoreUtils } from '@services/utils/utils'; |
| 18 | +import { CoreNavigator } from '@services/navigator'; |
| 19 | +import { ActivatedRoute } from '@angular/router'; |
| 20 | +import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics'; |
| 21 | +import { CoreTime } from '@singletons/time'; |
| 22 | +import { AddonBadges, AddonBadgesBadgeClass } from '../../services/badges'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Page that displays a badge class. |
| 26 | + */ |
| 27 | +@Component({ |
| 28 | + selector: 'page-addon-badges-badge-class', |
| 29 | + templateUrl: 'badge-class.html', |
| 30 | +}) |
| 31 | +export class AddonBadgesBadgeClassPage implements OnInit { |
| 32 | + |
| 33 | + protected badgeId = 0; |
| 34 | + protected logView: (badge: AddonBadgesBadgeClass) => void; |
| 35 | + |
| 36 | + badge?: AddonBadgesBadgeClass; |
| 37 | + badgeLoaded = false; |
| 38 | + currentTime = 0; |
| 39 | + |
| 40 | + constructor(protected route: ActivatedRoute) { |
| 41 | + this.badgeId = CoreNavigator.getRequiredRouteNumberParam('badgeId'); |
| 42 | + |
| 43 | + this.logView = CoreTime.once((badge) => { |
| 44 | + CoreAnalytics.logEvent({ |
| 45 | + type: CoreAnalyticsEventType.VIEW_ITEM, |
| 46 | + ws: 'core_badges_get_badge', |
| 47 | + name: badge.name, |
| 48 | + data: { id: this.badgeId, category: 'badges' }, |
| 49 | + url: `/badges/badgeclass.php?id=${this.badgeId}`, |
| 50 | + }); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * View loaded. |
| 56 | + */ |
| 57 | + ngOnInit(): void { |
| 58 | + this.fetchBadgeClass().finally(() => { |
| 59 | + this.badgeLoaded = true; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Fetch the badge class required for the view. |
| 65 | + * |
| 66 | + * @returns Promise resolved when done. |
| 67 | + */ |
| 68 | + async fetchBadgeClass(): Promise<void> { |
| 69 | + try { |
| 70 | + this.badge = await AddonBadges.getBadgeClass(this.badgeId); |
| 71 | + |
| 72 | + this.logView(this.badge); |
| 73 | + } catch (message) { |
| 74 | + CoreDomUtils.showErrorModalDefault(message, 'Error getting badge data.'); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Refresh the badge class. |
| 80 | + * |
| 81 | + * @param refresher Refresher. |
| 82 | + */ |
| 83 | + async refreshBadgeClass(refresher?: HTMLIonRefresherElement): Promise<void> { |
| 84 | + await CoreUtils.ignoreErrors(AddonBadges.invalidateBadgeClass(this.badgeId)); |
| 85 | + |
| 86 | + await this.fetchBadgeClass(); |
| 87 | + |
| 88 | + refresher?.complete(); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments