~sirn/fanboi2

ref: 5b482cfc1bfb0bb1e89715cb57c7bac864391768 fanboi2/assets/app/javascripts/views/topic_view.ts -rw-r--r-- 1.4 KiB
5b482cfcKridsada Thanabulpong 0.30.0 3 years ago
                                                                                
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
import {VNode, h} from 'virtual-dom';
import {formatDate} from '../utils/formatters';
import {Topic} from '../models/topic';


export class TopicView {
    topicNode: VNode;

    constructor(topic: Topic) {
        this.topicNode = TopicView.renderTopic(topic);
    }

    render(): VNode {
        return this.topicNode;
    }

    private static renderTopic(topic: Topic): VNode {
        return h('div',
            {className: 'js-topic'},
            [
                h('div', {className: 'topic-header'}, [
                    h('div', {className: 'container'}, [
                        TopicView.renderTitle(topic),
                        TopicView.renderDate(topic),
                        TopicView.renderCount(topic),
                    ])
                ])
            ]
        );
    }

    private static renderTitle(topic: Topic): VNode {
        return h('h3', {className: 'topic-header-title'}, [topic.title]);
    }

    private static renderDate(topic: Topic): VNode {
        let postedAt = new Date(topic.postedAt);
        let formatter = formatDate(postedAt);
        return h('p', {className: 'topic-header-item'}, [
            'Last posted ',
            h('strong', {}, [formatter]),
        ]);
    }

    private static renderCount(topic: Topic): VNode {
        return h('p', {className: 'topic-header-item'}, [
            'Total of ',
            h('strong', {}, [`${topic.postCount} posts`]),
        ]);
    }
}