-
Notifications
You must be signed in to change notification settings - Fork 19
/
Article.svelte
193 lines (172 loc) · 5.79 KB
/
Article.svelte
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<script>
import { onMount } from 'svelte';
import { t } from 'ttag';
import { gql } from '../lib';
import AppBar from '../components/AppBar.svelte';
import SingleColorLogo from '../components/icons/SingleColorLogo.svelte';
import FullpagePrompt from '../components/FullpagePrompt.svelte';
import Header from '../components/Header.svelte';
import ArticleCard from '../components/ArticleCard.svelte';
import ArticleReplyCard from '../components/ArticleReplyCard.svelte';
import Spacer from '../components/Spacer.svelte';
import Terms from '../components/Terms.svelte';
import Button from '../components/Button.svelte';
import { ArticleReplyCard_articleReply } from '../components/fragments';
import { getArticleURL } from 'src/lib/sharedUtils';
import improveBanner from '../assets/improve-reply-banner.png';
import multipleRepliesBanner from '../assets/multiple-replies.png';
const params = new URLSearchParams(location.search);
const articleId = params.get('articleId');
const replyId = params.get('replyId');
const articleUrl = getArticleURL(articleId);
let articleData;
let articleReplies = [];
// These article replies are collapsed by default. Used when replyId is specified from URL.
let collapsedArticleReplies = [];
let expanded = false; // Collapse above article replies by default
let createdAt;
const loadData = async () => {
const {
data: { GetArticle },
} = await gql`
query GetArticleInLIFF($id: String) {
GetArticle(id: $id) {
text
replyRequestCount
requestedForReply
createdAt
articleReplies(status: NORMAL) {
...ArticleReplyCard_articleReply
}
}
}
${ArticleReplyCard_articleReply}
`({id: articleId});
const {articleReplies: list, ...rest} = GetArticle;
articleReplies = !replyId ? list : list.filter(({reply}) => reply.id === replyId);
collapsedArticleReplies = !replyId ? [] : list.filter(({reply}) => reply.id !== replyId);
articleData = rest;
createdAt = new Date(articleData.createdAt);
// Send event to Google Analytics
gtag('event', 'ViewArticle', {
event_category: 'LIFF',
event_label: articleId,
});
articleReplies.forEach(({reply}) => {
gtag('event', 'ViewReply', {
event_category: 'LIFF',
event_label: reply.id,
});
})
}
const setViewed = async () => {
await gql`
mutation SetViewedInLIFF($id: String!) {
setViewed(articleId: $id) { lastViewedAt }
}
`({id: articleId});
}
onMount(() => {
loadData();
setViewed();
});
let isRequestingReply = false;
const handleRequestReply = async () => {
isRequestingReply = true;
const resp = await gql`
mutation RequestReplyInArticleLIFF($articleId: String!) {
CreateOrUpdateReplyRequest (articleId: $articleId) {
replyRequestCount
requestedForReply
}
}
`({articleId});
isRequestingReply = false;
articleData = {...articleData, ...resp.data.CreateOrUpdateReplyRequest};
}
$: replySectionTitle = articleReplies.length === 1
? t`Cofacts volunteer's reply to the message above`
: t`Cofacts volunteers have published ${articleReplies.length} replies to the message above`
</script>
<style>
.improve-banner > img {
margin-top: 24px;
width: 100%;
}
.multiple-replies-banner {
margin-top: 24px;
padding: 24px 16px;
background: var(--primary50);
display: grid;
grid-auto-flow: row;
row-gap: 16px;
}
.multiple-replies-banner > img {
width: 72.8%;
margin: 0 auto;
}
</style>
<svelte:head>
<title>{t`IM check`} | {t`Cofacts chatbot`}</title>
</svelte:head>
{#if !articleData }
<FullpagePrompt>{t`Loading IM data...`}</FullpagePrompt>
{:else}
<AppBar {articleId} />
<Header>
{t`Suspicious messages`}
</Header>
<ArticleCard
text={articleData.text}
replyRequestCount={articleData.replyRequestCount}
createdAt={createdAt}
/>
{#if articleReplies.length === 0}
<p>有 {articleData.replyRequestCount} 人回報說看到此訊息。</p>
<button type="button" on:click={handleRequestReply} disabled={isRequestingReply || articleData.requestedForReply}>
{#if articleData.requestedForReply}
已回報此訊息
{:else}
我也要回報此訊息!
{/if}
</button>
{:else}
<Header style="position: relative; overflow: hidden;">
{replySectionTitle}
<SingleColorLogo style="position: absolute; right: -4px; bottom: -9px; color: var(--secondary100); z-index: -1;"/>
</Header>
{#each articleReplies as articleReply, idx (articleReply.reply.id)}
{#if idx > 0}
<Spacer style="--dot-size: 8px" />
{/if}
<ArticleReplyCard articleReply={articleReply} />
{/each}
{#if collapsedArticleReplies.length > 0 }
{#if expanded}
<Header>
{t`Other replies`}
</Header>
{#each collapsedArticleReplies as articleReply, idx (articleReply.reply.id)}
{#if idx > 0}
<Spacer style="--dot-size: 8px" />
{/if}
<ArticleReplyCard articleReply={articleReply} />
{/each}
{:else}
<footer class="multiple-replies-banner">
<img src={multipleRepliesBanner} alt="Multiple replies available" />
{t`There are different replies for the message. We suggest read them all here before you make judgements.`}
<Button variant="outlined" on:click={() => { expanded = true; }}>
{t`Read other replies`}
</Button>
</footer>
{/if}
{/if}
{#if collapsedArticleReplies.length === 0 || expanded}
<a class="improve-banner" href={articleUrl} target="_blank">
<img src={improveBanner} alt="Help improve replies" />
</a>
{/if}
{/if}
<Terms />
{/if}