MOBILE-3320 wiki: Fix wiki links

main
Dani Palou 2021-06-09 15:53:10 +02:00
parent 287496c8ec
commit 8f4ae0adc7
5 changed files with 40 additions and 18 deletions

View File

@ -42,8 +42,8 @@ export class AddonModWikiEditPage implements OnInit, OnDestroy, CanLeave {
@ViewChild('editPageForm') formElement?: ElementRef;
cmId!: number; // Course module ID.
courseId!: number; // Course the wiki belongs to.
cmId?: number; // Course module ID.
courseId?: number; // Course the wiki belongs to.
title?: string; // Title to display.
pageForm?: FormGroup; // The form group.
contentControl?: FormControl; // The FormControl for the page content.
@ -77,8 +77,8 @@ export class AddonModWikiEditPage implements OnInit, OnDestroy, CanLeave {
* @inheritdoc
*/
async ngOnInit(): Promise<void> {
this.cmId = CoreNavigator.getRouteNumberParam('cmId')!;
this.courseId = CoreNavigator.getRouteNumberParam('courseId')!;
this.cmId = CoreNavigator.getRouteNumberParam('cmId') || undefined;
this.courseId = CoreNavigator.getRouteNumberParam('courseId') || undefined;
this.subwikiId = CoreNavigator.getRouteNumberParam('subwikiId');
this.wikiId = CoreNavigator.getRouteNumberParam('wikiId');
this.pageId = CoreNavigator.getRouteNumberParam('pageId');
@ -162,6 +162,8 @@ export class AddonModWikiEditPage implements OnInit, OnDestroy, CanLeave {
this.userId = pageContents.userid;
canEdit = pageContents.caneditpage;
await this.fetchModuleAndCourseId();
// Get subwiki files, needed to replace URLs for rich text editor.
this.subwikiFiles = await AddonModWiki.getSubwikiFiles(this.wikiId, {
groupId: this.groupId,
@ -190,8 +192,10 @@ export class AddonModWikiEditPage implements OnInit, OnDestroy, CanLeave {
this.editing = false;
canEdit = !!this.blockId; // If no blockId, the user cannot edit the page.
// Make sure we have the wiki ID.
if (!this.wikiId) {
await this.fetchModuleAndCourseId();
// Try to get wikiId.
if (!this.wikiId && this.cmId && this.courseId) {
const module = await CoreCourse.getModule(this.cmId, this.courseId, undefined, true);
this.wikiId = module.instance;
@ -248,6 +252,22 @@ export class AddonModWikiEditPage implements OnInit, OnDestroy, CanLeave {
}
}
/**
* Load cmId and courseId if they aren't set.
*
* @return Promise.
*/
protected async fetchModuleAndCourseId(): Promise<void> {
if (!this.wikiId || (this.cmId && this.courseId)) {
return;
}
const module = await CoreCourse.getModuleBasicInfoByInstance(this.wikiId, 'wiki');
this.cmId = module.id;
this.courseId = module.course;
}
/**
* Force leaving the page, without checking for changes.
*/

View File

@ -14,7 +14,6 @@
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { CoreError } from '@classes/errors/error';
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
import { CoreCourse } from '@features/course/services/course';
@ -106,29 +105,27 @@ export class AddonModWikiCreateLinkHandlerService extends CoreContentLinksHandle
const route = CoreNavigator.getCurrentRoute({ pageComponent: AddonModWikiIndexPage });
const subwikiId = parseInt(params.swid, 10);
const wikiId = parseInt(params.wid, 10);
let moduleId: number;
let path = AddonModWikiModuleHandlerService.PAGE_NAME;
// Check if the link is inside the same wiki.
const isSameWiki = await this.currentStateIsSameWiki(route, subwikiId, siteId);
if (isSameWiki) {
// User is seeing the wiki, we can get the module from the wiki params.
moduleId = route!.snapshot.params.cmId;
courseId = route!.snapshot.params.courseId;
path = path + `/${route!.snapshot.params.courseId}/${route!.snapshot.params.cmId}/edit`;
} else if (wikiId) {
// The URL specifies which wiki it belongs to. Get the module.
const module = await CoreCourse.getModuleBasicInfoByInstance(wikiId, 'wiki', siteId);
moduleId = module.id;
courseId = module.course;
path = path + `/${module.course}/${module.id}/edit`;
} else {
// Not enough data.
throw new CoreError();
// Cannot get module ID.
path = path + `/${courseId || 0}/0/edit`;
}
// Open the page.
CoreNavigator.navigateToSitePath(
AddonModWikiModuleHandlerService.PAGE_NAME + `/${courseId}/${moduleId}/edit`,
path,
{
params: {
pageTitle: params.title,

View File

@ -70,6 +70,7 @@ export class AddonModWikiPageOrMapLinkHandlerService extends CoreContentLinksHan
AddonModWikiModuleHandlerService.PAGE_NAME + `/${courseId}/${module.id}/page/${hash}`,
{
params: {
module,
pageId: page.id,
pageTitle: page.title,
subwikiId: page.subwikiid,

View File

@ -1216,8 +1216,8 @@ export type AddonModWikiPageCreatedData = {
* Data about a page that was just edited.
*/
export type AddonModWikiEditedPageData = {
cmId: number;
courseId: number;
cmId?: number;
courseId?: number;
wikiId: number;
pageTitle: string;
subwikiId?: number;

View File

@ -1409,7 +1409,11 @@ export class CoreDomUtilsProvider {
errorMessage = CoreTextUtils.getErrorMessageFromError(error);
}
return this.showErrorModal(typeof errorMessage == 'string' ? error! : defaultError, needsTranslate, autocloseTime);
return this.showErrorModal(
typeof errorMessage == 'string' && errorMessage ? error! : defaultError,
needsTranslate,
autocloseTime,
);
}
/**