-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathreply-form.ts
More file actions
189 lines (160 loc) · 4.76 KB
/
reply-form.ts
File metadata and controls
189 lines (160 loc) · 4.76 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
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
import type {
CommentVo,
Reply,
ReplyRequest,
ReplyVo,
User,
} from '@halo-dev/api-client';
import { consume } from '@lit/context';
import { html, LitElement } from 'lit';
import { property, state } from 'lit/decorators.js';
import { createRef, type Ref, ref } from 'lit/directives/ref.js';
import './base-form';
import { msg } from '@lit/localize';
import { FetchError, type FetchResponse, ofetch } from 'ofetch';
import type { BaseForm } from './base-form';
import {
allowAnonymousCommentsContext,
baseUrlContext,
currentUserContext,
toastContext,
} from './context';
import type { ToastManager } from './lit-toast';
import type { ProblemDetail } from './types';
import {
type CaptchaRequiredResponse,
getCaptchaCodeHeader,
isRequireCaptcha,
} from './utils/captcha';
export class ReplyForm extends LitElement {
@consume({ context: baseUrlContext })
@state()
baseUrl = '';
@consume({ context: currentUserContext, subscribe: true })
@state()
currentUser: User | undefined;
@property({ type: Object })
comment: CommentVo | undefined;
@property({ type: Object })
quoteReply: ReplyVo | undefined;
@consume({ context: allowAnonymousCommentsContext, subscribe: true })
@state()
allowAnonymousComments = false;
@consume({ context: toastContext, subscribe: true })
@state()
toastManager: ToastManager | undefined;
@state()
submitting = false;
@state()
captcha = '';
baseFormRef: Ref<BaseForm> = createRef<BaseForm>();
override connectedCallback(): void {
super.connectedCallback();
setTimeout(() => {
this.scrollIntoView({
block: 'center',
inline: 'start',
behavior: 'smooth',
});
this.baseFormRef.value?.setFocus();
}, 0);
}
override render() {
return html` <base-form
.submitting=${this.submitting}
.captcha=${this.captcha}
.hidePrivateCheckbox=${this.comment?.spec.hidden || false}
${ref(this.baseFormRef)}
@submit="${this.onSubmit}"
></base-form>`;
}
async onSubmit(e: CustomEvent) {
e.preventDefault();
this.submitting = true;
const data = e.detail;
const { displayName, email, website, content, hidden } = data || {};
const replyRequest: ReplyRequest = {
raw: content,
content: content,
// TODO: support user input
allowNotification: true,
hidden: hidden || false,
};
if (this.quoteReply) {
replyRequest.quoteReply = this.quoteReply.metadata.name;
}
if (!this.currentUser && !this.allowAnonymousComments) {
this.toastManager?.warn(msg('Please login first'));
this.submitting = false;
return;
}
if (!this.currentUser && this.allowAnonymousComments) {
if (!displayName || !email) {
this.toastManager?.warn(
msg('Please log in or complete the information first')
);
this.submitting = false;
return;
} else {
replyRequest.owner = {
displayName: displayName,
email: email,
website: website,
};
}
}
try {
const newReply = await ofetch<Reply>(
`${this.baseUrl}/apis/api.halo.run/v1alpha1/comments/${this.comment?.metadata.name}/reply`,
{
method: 'POST',
headers: {
...getCaptchaCodeHeader(data.captchaCode),
},
body: replyRequest,
}
);
this.baseFormRef.value?.handleFetchCaptcha();
if (newReply.spec.approved) {
this.toastManager?.success(msg('Comment submitted successfully'));
} else {
this.toastManager?.success(
msg('Comment submitted successfully, pending review')
);
}
this.dispatchEvent(new CustomEvent('reload'));
window.dispatchEvent(new CustomEvent('halo:comment-reply:created'));
this.baseFormRef.value?.resetForm();
} catch (error) {
if (error instanceof FetchError) {
if (
isRequireCaptcha(
error.response as FetchResponse<CaptchaRequiredResponse>
)
) {
const { captcha, detail } =
error.data as unknown as CaptchaRequiredResponse;
this.captcha = captcha;
this.toastManager?.warn(detail);
return;
}
const problemDetail = error.data as unknown as ProblemDetail;
this.toastManager?.error(
[problemDetail?.title, problemDetail?.detail].join(' - ') ||
msg('Comment failed, please try again later')
);
return;
}
this.toastManager?.error(msg('Comment failed, please try again later'));
} finally {
this.submitting = false;
}
}
}
customElements.get('reply-form') ||
customElements.define('reply-form', ReplyForm);
declare global {
interface HTMLElementTagNameMap {
'reply-form': ReplyForm;
}
}