From 666cfb207bfbca61308ea464c1b3d85a2cfad4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilmar=20T=C3=BCrk?= Date: Wed, 8 Jan 2025 23:05:20 +0200 Subject: [PATCH] When editing follow-up topic don't change topic status --- .../topic-form/topic-form.component.ts | 110 +++++++++--------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/src/app/topic/components/topic-form/topic-form.component.ts b/src/app/topic/components/topic-form/topic-form.component.ts index 6750e7d51..8e92f14ce 100644 --- a/src/app/topic/components/topic-form/topic-form.component.ts +++ b/src/app/topic/components/topic-form/topic-form.component.ts @@ -215,16 +215,6 @@ export class TopicFormComponent { } )); - /* this.groups$ = this.GroupService.loadItems().pipe(map((groups) => { - groups.forEach((group: any) => { - if (this.groupId && this.groupId === group.id) { - const exists = this.topicGroups.find((mgroup) => mgroup.id === group.id); - if (!exists) this.addGroup(group); - } - }); - - }));*/ - this.tabSelected = this.route.fragment.pipe( map((fragment) => { if (!fragment) { @@ -343,53 +333,59 @@ export class TopicFormComponent { } nextTab(tab: string | void) { - if (tab === 'info') { - let invalid = false; - if (!this.topic.title) { - this.block.title = true; - invalid = true; - setTimeout(() => { - this.titleInput?.nativeElement?.parentNode.parentNode.classList.add('error'); - }); - } - if (invalid) { - return - } + if (!tab) return; + + const tabIndex = this.tabs.indexOf(tab); + if (tabIndex === -1 || tabIndex >= 3) return; + + // Validate info tab + if (tab === 'info' && !this.validateInfoTab()) return; + + // Validate discussion tab + if (tabIndex === 2 && !this.validateDiscussionTab()) return; + + // Handle preview tab + if (tabIndex + 1 === 3) { + this.loadPreviewTab(); } + window.scrollTo(0, 0); - if (tab) { - let tabIndex = this.tabs.indexOf(tab); - if (tab === 'info' && this.topic.permission.level === this.TopicService.LEVELS.edit) tabIndex = tabIndex + 1; - if (tabIndex > -1 && tabIndex < 3) { - this.selectTab(this.tabs[tabIndex + 1]); - } - if (tabIndex === 2) { - if (!this.discussion.question) { - this.Notification.removeAll(); - this.Notification.addError(this.translate.instant('VIEWS.TOPIC_CREATE.ERROR_MISSING_QUESTION')); - return; - } - /* if (this.voteCreateForm) - this.voteCreateForm.saveVoteSettings();*/ - } - if (tabIndex + 1 === 3) { - setTimeout(() => { - this.TopicService.readDescription(this.topic.id).pipe(take(1)).subscribe({ - next: (topic) => { - this.topic.description = topic.description; - }, - error: (err) => { - console.error(err) - } - }); - }, 200) - } - if (tabIndex > -1 && tabIndex < 3) { - setTimeout(() => { - this.selectTab(this.tabs[tabIndex + 1]); - }) - } + setTimeout(() => { + this.selectTab(this.tabs[tabIndex + 1]); + }); + } + + private validateInfoTab(): boolean { + if (!this.topic.title) { + this.block.title = true; + setTimeout(() => { + this.titleInput?.nativeElement?.parentNode.parentNode.classList.add('error'); + }); + return false; + } + return true; + } + + private validateDiscussionTab(): boolean { + if (!this.discussion.question) { + this.Notification.removeAll(); + this.Notification.addError(this.translate.instant('VIEWS.TOPIC_CREATE.ERROR_MISSING_QUESTION')); + return false; } + return true; + } + + private loadPreviewTab(): void { + setTimeout(() => { + this.TopicService.readDescription(this.topic.id).pipe(take(1)).subscribe({ + next: (topic) => { + this.topic.description = topic.description; + }, + error: (err) => { + console.error(err) + } + }); + }, 200); } previousTab(tab: string | void) { @@ -562,7 +558,9 @@ export class TopicFormComponent { publish() { this.titleInput?.nativeElement?.parentNode.parentNode.classList.remove('error'); - this.topic.status = this.TopicService.STATUSES.inProgress; + if (this.TopicService.STATUSES.draft === this.topic.status) { + this.topic.status = this.TopicService.STATUSES.inProgress; + } const updateTopic = { ...this.topic }; if (!updateTopic.intro?.length) { updateTopic.intro = null; @@ -789,7 +787,7 @@ export class TopicFormComponent { } canEditDiscussion() { - return this.TopicService.canDelete(this.topic) && (this.topic.status !== this.TopicService.STATUSES.draft || this.topic.status !== this.TopicService.STATUSES.inProgress); + return this.TopicService.canDelete(this.topic) && (this.topic.status === this.TopicService.STATUSES.draft || this.topic.status === this.TopicService.STATUSES.inProgress); } toggleDeadline() {