-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
149 lines (134 loc) · 4.39 KB
/
index.js
File metadata and controls
149 lines (134 loc) · 4.39 KB
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
import './style.scss';
import AjaxForm from 'components/ajaxForm';
import alert from 'components/dialogs/alert';
import confirm from 'components/dialogs/confirm';
import Input from 'components/input';
import Ref from 'html-tag-js/ref';
import { getLoggedInUser, hashString } from 'lib/helpers';
import Router from 'lib/Router';
import { marked } from 'marked';
export default async function FAQs({ mode, oldQ, a, qHash }) {
const isUpdate = mode === 'update';
const loggedInUser = await getLoggedInUser();
const res = await fetch('/api/faqs');
const faqs = await res.json();
const isAdmin = !!loggedInUser?.isAdmin;
const form = Ref();
if (isUpdate) {
form.on('ref', (el) => {
setTimeout(() => {
el.scrollIntoView();
}, 0);
});
} else if (qHash) {
setTimeout(() => {
const el = document.getElementById(qHash);
el.scrollIntoView();
}, 0);
}
return (
<section id='faqs'>
<h1>FAQs</h1>
<FAQForm />
<FAQsContainer />
</section>
);
function FAQsContainer() {
return faqs.map(({ q, a: ans }) => <FAQ q={q} a={ans} />);
}
function FAQForm() {
if (!isAdmin) return <div />;
const method = isUpdate ? 'put' : 'post';
const mdPreview = Ref();
const q = Ref();
mdPreview.innerHTML = marked.parse(a || '');
return (
<details open={isUpdate} style={{ margin: '20px 0' }}>
<summary style={{ margin: '20px 0' }}>{isUpdate ? 'Update' : 'Add'} FAQ</summary>
<AjaxForm ref={form} onerror={onerror} onloadend={onloadend} action='/api/faqs' method={method}>
{isUpdate ? <Input name='old_q' required={true} hidden type='hidden' value={oldQ || ''} /> : ''}
<Input ref={q} name='q' required={true} label='Question' placeholder='Question' value={oldQ || ''} />
<Input
name='a'
required={true}
onkeydown={onkeydown}
oninput={oninput}
label='Answer'
type='textarea'
placeholder='Answer (Markdown)'
value={a || ''}
/>
<div className='preview' ref={mdPreview} />
<div className='buttons'>
<button type='submit'>Submit</button>
{isUpdate
? <button className='danger' type='button' onclick={() => Router.reload()}>
Cancel
</button>
: ''}
</div>
</AjaxForm>
</details>
);
function onerror(err) {
alert('Error', err.message);
}
function onloadend() {
if (method === 'put') {
const url = `/faqs/${hashString(q.value)}`;
if (window.location.pathname !== url) Router.loadUrl(url);
else Router.reload();
alert('Success', 'FAQ updated successfully');
} else {
Router.reload();
alert('Success', 'FAQ added successfully');
}
}
function oninput(e) {
mdPreview.innerHTML = marked.parse(e.target.value);
}
function onkeydown(e) {
if (e.key === 'Tab') {
e.preventDefault();
const start = e.target.selectionStart;
const end = e.target.selectionEnd;
e.target.value = `${e.target.value.substring(0, start)} ${e.target.value.substring(end)}`;
e.target.selectionStart = start + 1;
e.target.selectionEnd = start + 1;
}
}
}
function FAQ({ q, a: ans }) {
const id = hashString(q);
return (
<div className='faq'>
<a href={`/faqs/${id}`} style={{ textDecoration: 'none' }}>
<h2 id={id} style={{ textAlign: 'left' }}>
{q}
</h2>
</a>
<p innerHTML={marked.parse(ans)} />
{isAdmin
? <div className='icon-buttons'>
<span onclick={() => editFaq(q, ans)} title='Edit this FAQ' className='link icon create' />
<span onclick={() => deleteFaq(q)} title='Delete this FAQ' className='link icon delete danger' />
</div>
: ''}
</div>
);
}
function editFaq(q, ans) {
Router.reload({ mode: 'update', oldQ: q, a: ans });
}
async function deleteFaq(q) {
try {
const confirmation = await confirm('Warning', `Delete FAQ "${q}"?`);
if (!confirmation) return;
const faqRes = await fetch(`/api/faqs/${q}`, { method: 'delete' });
if (faqRes.error) throw new Error(faqRes.error);
Router.reload();
} catch (error) {
alert('Error', error.message);
}
}
}