-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
RenameDiscussionModal.tsx
86 lines (75 loc) · 2.52 KB
/
RenameDiscussionModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import app from '../../forum/app';
import FormModal, { IFormModalAttrs } from '../../common/components/FormModal';
import Button from '../../common/components/Button';
import Stream from '../../common/utils/Stream';
import Mithril from 'mithril';
import Discussion from '../../common/models/Discussion';
import Form from '../../common/components/Form';
export interface IRenameDiscussionModalAttrs extends IFormModalAttrs {
discussion: Discussion;
currentTitle: string;
}
/**
* The 'RenameDiscussionModal' displays a modal dialog with an input to rename a discussion
*/
export default class RenameDiscussionModal<
CustomAttrs extends IRenameDiscussionModalAttrs = IRenameDiscussionModalAttrs
> extends FormModal<CustomAttrs> {
discussion!: Discussion;
currentTitle!: string;
newTitle!: Stream<string>;
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
super.oninit(vnode);
this.discussion = this.attrs.discussion;
this.currentTitle = this.attrs.currentTitle;
this.newTitle = Stream(this.currentTitle);
}
className() {
return 'RenameDiscussionModal Modal--small';
}
title() {
return app.translator.trans('core.forum.rename_discussion.title');
}
content() {
return (
<div className="Modal-body">
<Form className="Form--centered">
<div className="Form-group">
<input className="FormControl" bidi={this.newTitle} type="text" />
</div>
<div className="Form-group Form-controls">
<Button className="Button Button--primary Button--block" type="submit" loading={this.loading}>
{app.translator.trans('core.forum.rename_discussion.submit_button')}
</Button>
</div>
</Form>
</div>
);
}
onsubmit(e: SubmitEvent): Promise<void> | void {
e.preventDefault();
this.loading = true;
const title = this.newTitle();
const currentTitle = this.currentTitle;
// If the title is different to what it was before, then save it. After the
// save has completed, update the post stream as there will be a new post
// indicating that the discussion was renamed.
if (title && title !== currentTitle) {
return this.discussion
.save({ title })
.then(() => {
if (app.viewingDiscussion(this.discussion)) {
app.current.get('stream').update();
}
m.redraw();
this.hide();
})
.catch(() => {
this.loading = false;
m.redraw();
});
} else {
this.hide();
}
}
}