Merge pull request #3999 from alfonso-salces/MOBILE-4219

MOBILE-4219 blog: Update list when toggle only your blog entries
main
Dani Palou 2024-04-09 09:03:32 +02:00 committed by GitHub
commit 5a8ec1404d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 37 deletions

View File

@ -45,9 +45,6 @@ export class AddonBlogIndexPage implements OnInit, OnDestroy {
protected filter: AddonBlogFilter = {}; protected filter: AddonBlogFilter = {};
protected pageLoaded = 0; protected pageLoaded = 0;
protected userPageLoaded = 0;
protected canLoadMoreEntries = false;
protected canLoadMoreUserEntries = true;
protected siteHomeId: number; protected siteHomeId: number;
protected logView: () => void; protected logView: () => void;
@ -172,16 +169,13 @@ export class AddonBlogIndexPage implements OnInit, OnDestroy {
if (refresh) { if (refresh) {
this.pageLoaded = 0; this.pageLoaded = 0;
this.userPageLoaded = 0;
} }
const loadPage = this.onlyMyEntries ? this.userPageLoaded : this.pageLoaded;
try { try {
const result = await AddonBlog.getEntries( const result = await AddonBlog.getEntries(
this.filter, this.filter,
{ {
page: loadPage, page: this.pageLoaded,
readingStrategy: refresh readingStrategy: refresh
? CoreSitesReadingStrategy.PREFER_NETWORK ? CoreSitesReadingStrategy.PREFER_NETWORK
: undefined, : undefined,
@ -229,21 +223,9 @@ export class AddonBlogIndexPage implements OnInit, OnDestroy {
.sort((a, b) => b.created - a.created); .sort((a, b) => b.created - a.created);
} }
this.entries = this.entries.filter(entry => !this.onlyMyEntries || entry.userid === this.currentUserId); this.canLoadMore = result.totalentries > this.entries.length;
if (this.onlyMyEntries) {
const count = this.entries.filter((entry) => entry.userid == this.currentUserId).length;
this.canLoadMoreUserEntries = result.totalentries > count;
this.canLoadMore = this.canLoadMoreUserEntries;
this.userPageLoaded++;
} else {
this.canLoadMoreEntries = result.totalentries > this.entries.length;
this.canLoadMore = this.canLoadMoreEntries;
this.pageLoaded++; this.pageLoaded++;
}
await Promise.all(promises); await Promise.all(promises);
this.logView(); this.logView();
} catch (error) { } catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.blog.errorloadentries', true); CoreDomUtils.showErrorModalDefault(error, 'addon.blog.errorloadentries', true);
@ -258,24 +240,19 @@ export class AddonBlogIndexPage implements OnInit, OnDestroy {
* *
* @param enabled If true, filter my entries. False otherwise. * @param enabled If true, filter my entries. False otherwise.
*/ */
onlyMyEntriesToggleChanged(enabled: boolean): void { async onlyMyEntriesToggleChanged(enabled: boolean): Promise<void> {
this.canLoadMore = enabled ? this.canLoadMoreUserEntries : this.canLoadMoreEntries; const loading = await CoreDomUtils.showModalLoading();
if (!enabled) { try {
delete this.filter.userid; this.filter.userid = !enabled ? undefined : this.currentUserId;
await this.fetchEntries(true);
return; } catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.blog.errorloadentries', true);
this.onlyMyEntries = !enabled;
this.filter.userid = !enabled ? this.currentUserId : undefined;
} finally {
loading.dismiss();
} }
const count = this.entries.filter((entry) => entry.userid == this.currentUserId).length;
this.userPageLoaded = Math.floor(count / AddonBlogProvider.ENTRIES_PER_PAGE);
this.filter.userid = this.currentUserId;
if (count == 0 && this.canLoadMoreUserEntries) {
// First time but no entry loaded. Try to load some.
this.loadMore();
}
} }
/** /**