Skip to content

Commit

Permalink
Fix new forum post content resetting (#567)
Browse files Browse the repository at this point in the history
Co-authored-by: matteo <[email protected]>
Co-authored-by: Matteo Bronkhorst <[email protected]>
  • Loading branch information
3 people authored Nov 19, 2022
1 parent 04db56b commit 4da9761
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/components/forum/forum-post-new.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p class='card-text'>
<MdEditor @content={{content}} @textareaId='newForumPost' />
<MdEditor @content={{this.content}} @textareaId='newForumPost' />
</p>
<div class='card-footer'>
<div class='row justify-content-end'>
Expand Down
16 changes: 12 additions & 4 deletions app/components/forum/forum-post-new.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import Component from '@glimmer/component';
import { action } from '@ember/object';

// todo: incorporate the model-save-util into components?
export default class ForumPostNewComponent extends Component {
@service store;
@service flashNotice;

get content() {
return this.args.content;
}

set content(content) {
this.args.updateContent(content);
}

@action
async save() {
let { content, thread } = this;
let { content, thread } = this.args;
await this.store
.createRecord('forum/post', {
message: content,
thread,
})
.save();
this.flashNotice.sendSuccess('Forumbericht toegevoegd!');
this.set('content', '');
this.onSubmit();
this.content = '';
this.args.onSubmit();
}

@action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { tracked } from '@glimmer/tracking';
export default class PostsIndexController extends Controller {
@service session;
@service store;
@tracked
newContent = '';
@tracked newContent = '';

queryParams = ['page'];

Expand Down Expand Up @@ -55,8 +54,12 @@ export default class PostsIndexController extends Controller {

@action
async addQuote(q) {
this.set('newContent', `${this.newContent}${q} \n\n`);

if (!this.newContent?.endsWith('\n')) {
// insert a newline in between if necessary
this.newContent = `${this.newContent}\n${q} \n\n`;
} else {
this.newContent = `${this.newContent}${q} \n\n`;
}
if (!this.currentPageIsLastPage) {
await this.transitionToRoute({
queryParams: { page: this.model.postsPaged.meta.totalPages },
Expand All @@ -69,6 +72,16 @@ export default class PostsIndexController extends Controller {
}
}

@action
updateNewContent(newContent) {
this.newContent = newContent;
}

@action
resetNewContent() {
this.newContent = '';
}

@action
goToPreviousPage() {
return this.advanceToPage(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export default class PostIndexRoute extends AuthenticatedRoute {
this.controller.send('goToNextPage');
}

resetController(controller, isExiting, transition) {
super.resetController(controller, isExiting, transition);
if (isExiting) {
controller.send('resetNewContent');
}
}

constructor() {
super(...arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
<div class='card-body'>
<Forum::ForumPostNew
@thread={{@model}}
@content={{newContent}}
@onSubmit={{action 'newPostCreated'}}
@content={{this.newContent}}
@updateContent={{this.updateNewContent}}
@onSubmit={{this.newPostCreated}}
/>
</div>
</div>
Expand Down

0 comments on commit 4da9761

Please sign in to comment.