From 58ea59d71f76dc77a21fb517157dc63f4db27ed2 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 1 Aug 2023 11:03:39 +0200 Subject: [PATCH] MOBILE-4401 wiki: Fix selected subwiki when wiki is empty This commit avoids a redundant error and also allows the create button to appear if the user has permissions to create the first page --- src/addons/mod/wiki/components/index/index.ts | 73 ++++++++++--------- .../course/tests/behat/basic_usage.feature | 2 - 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/addons/mod/wiki/components/index/index.ts b/src/addons/mod/wiki/components/index/index.ts index 16dc33583cc..cb078be54d5 100644 --- a/src/addons/mod/wiki/components/index/index.ts +++ b/src/addons/mod/wiki/components/index/index.ts @@ -343,42 +343,19 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp * @param subwiki Subwiki. */ protected async fetchSubwikiPages(subwiki: AddonModWikiSubwiki): Promise { - const subwikiPages = await AddonModWiki.getSubwikiPages(subwiki.wikiid, { - groupId: subwiki.groupid, - userId: subwiki.userid, - cmId: this.module.id, - }); + const subwikiPages = subwiki.id <= 0 ? + [] : + await AddonModWiki.getSubwikiPages(subwiki.wikiid, { + groupId: subwiki.groupid, + userId: subwiki.userid, + cmId: this.module.id, + }); - if (!this.currentPage) { - if (!this.pageTitle) { - // No page specified, search first page. - const firstPage = subwikiPages.find((page) => page.firstpage ); - if (firstPage) { - this.currentPage = firstPage.id; - this.pageTitle = firstPage.title; - } - } else { - // Got the page title but not its ID. Search the page. - const page = subwikiPages.find((page) => page.title === this.pageTitle ); - if (page) { - this.currentPage = page.id; - } - } - } + this.setCurrentPage(subwikiPages); // Now get the offline pages. const dbPages = await AddonModWikiOffline.getSubwikiNewPages(subwiki.id, subwiki.wikiid, subwiki.userid, subwiki.groupid); - // If no page specified, search page title in the offline pages. - if (!this.currentPage) { - const searchTitle = this.pageTitle ? this.pageTitle : this.wiki?.firstpagetitle ?? ''; - const pageExists = dbPages.some((page) => page.title == searchTitle); - - if (pageExists) { - this.pageTitle = searchTitle; - } - } - this.subwikiPages = AddonModWiki.sortPagesByTitle( (<(AddonModWikiSubwikiPage | AddonModWikiPageDBRecord)[]> subwikiPages).concat(dbPages), ); @@ -389,6 +366,32 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp } } + /** + * Set current page if needed. + * + * @param subwikiPages List of subwiki pages. + */ + setCurrentPage(subwikiPages: AddonModWikiSubwikiPage[]): void { + if (this.currentPage) { + return; // Already set, nothing to do. + } + + if (this.pageTitle) { + // Got the page title but not its ID. Search the page. + const page = subwikiPages.find((page) => page.title === this.pageTitle); + if (page) { + this.currentPage = page.id; + } + + return; + } + + // No page specified, search first page. + const firstPage = subwikiPages.find((page) => page.firstpage); + this.currentPage = firstPage?.id; + this.pageTitle = firstPage?.title ?? this.wiki?.firstpagetitle; + } + /** * Get the subwikis. * @@ -710,7 +713,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp * @returns Whether there is any subwiki selected. */ protected isAnySubwikiSelected(): boolean { - return this.subwikiData.subwikiSelected > 0 || this.subwikiData.userSelected > 0 || this.subwikiData.groupSelected > 0; + return this.subwikiData.subwikiSelected !== 0 || this.subwikiData.userSelected > 0 || this.subwikiData.groupSelected > 0; } /** @@ -752,7 +755,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp * @param groupId Group ID of the subwiki to select. */ protected setSelectedWiki(subwikiId: number | undefined, userId: number | undefined, groupId: number | undefined): void { - this.subwikiData.subwikiSelected = AddonModWikiOffline.convertToPositiveNumber(subwikiId); + this.subwikiData.subwikiSelected = subwikiId ?? 0; this.subwikiData.userSelected = AddonModWikiOffline.convertToPositiveNumber(userId); this.subwikiData.groupSelected = AddonModWikiOffline.convertToPositiveNumber(groupId); } @@ -800,7 +803,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp } const sameSubwiki = this.currentSubwiki && - ((this.currentSubwiki.id && this.currentSubwiki.id === editedPageData.subwikiId) || + ((this.currentSubwiki.id > 0 && this.currentSubwiki.id === editedPageData.subwikiId) || (this.currentSubwiki.userid === editedPageData.userId && this.currentSubwiki.groupid === editedPageData.groupId)); if (sameSubwiki && editedPageData.pageTitle === this.pageTitle) { @@ -1024,7 +1027,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp candidateSubwikiId = subwiki.id; } } else if (subwiki.groupid > 0) { - // Check if it's a current user' group. + // Check if it's a current user's group. if (showMyGroupsLabel) { candidateSubwikiId = subwiki.id; } diff --git a/src/core/features/course/tests/behat/basic_usage.feature b/src/core/features/course/tests/behat/basic_usage.feature index a815c12af83..8cb9e184df2 100755 --- a/src/core/features/course/tests/behat/basic_usage.feature +++ b/src/core/features/course/tests/behat/basic_usage.feature @@ -144,7 +144,6 @@ Feature: Test basic usage of one course in app When I press the back button in the app And I press "Test wiki name" in the app - And I press "OK" in the app Then the header should be "Test wiki name" in the app When I press the back button in the app @@ -199,7 +198,6 @@ Feature: Test basic usage of one course in app When I press the back button in the app And I press "Test wiki name" in the app - And I press "OK" in the app Then the header should be "Test wiki name" in the app When I press the back button in the app