Skip to content

Commit d49dbea

Browse files
authored
feat: disable button when submitting comments (#120)
支持在提交评论或回复时,禁用提交按钮,防止重复提交。 Fixes #116 <img width="641" alt="image" src="https://github.com/halo-dev/plugin-comment-widget/assets/21301288/9dc636ea-8631-4be6-9386-e8e0b19dd433"> /kind feature ```release-note 支持在提交评论或回复时,禁用提交按钮,防止重复提交。 ```
1 parent 220219d commit d49dbea

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

packages/comment-widget/src/base-form.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import './emoji-button';
2-
import { LitElement, css, html } from 'lit';
3-
import { Ref, createRef, ref } from 'lit/directives/ref.js';
2+
import { css, html, LitElement } from 'lit';
3+
import { createRef, Ref, ref } from 'lit/directives/ref.js';
44
import {
55
allowAnonymousCommentsContext,
66
baseUrlContext,
@@ -9,11 +9,12 @@ import {
99
kindContext,
1010
nameContext,
1111
} from './context';
12-
import { state } from 'lit/decorators.js';
12+
import { property, state } from 'lit/decorators.js';
1313
import type { User } from '@halo-dev/api-client';
1414
import baseStyles from './styles/base';
1515
import { consume } from '@lit/context';
1616
import varStyles from './styles/var';
17+
import './icons/icon-loading';
1718

1819
export class BaseForm extends LitElement {
1920
@consume({ context: baseUrlContext })
@@ -40,6 +41,9 @@ export class BaseForm extends LitElement {
4041
@state()
4142
name = '';
4243

44+
@property({ type: Boolean })
45+
submitting = false;
46+
4347
textareaRef: Ref<HTMLTextAreaElement> = createRef<HTMLTextAreaElement>();
4448

4549
get customAccount() {
@@ -179,13 +183,20 @@ export class BaseForm extends LitElement {
179183
: ''}
180184
<div class="form__actions">
181185
<emoji-button @emoji-select=${this.onEmojiSelect}></emoji-button>
182-
<button type="submit" class="form__button--submit">
183-
<svg viewBox="0 0 24 24" width="1.2em" height="1.2em" class="h-full w-full">
184-
<path
185-
fill="currentColor"
186-
d="M8 7.71L18 12L8 16.29v-3.34l7.14-.95L8 11.05V7.71M12 2a10 10 0 0 1 10 10a10 10 0 0 1-10 10A10 10 0 0 1 2 12A10 10 0 0 1 12 2m0 2a8 8 0 0 0-8 8a8 8 0 0 0 8 8a8 8 0 0 0 8-8a8 8 0 0 0-8-8Z"
187-
></path>
188-
</svg>
186+
<button .disabled=${this.submitting} type="submit" class="form__button--submit">
187+
${this.submitting
188+
? html` <icon-loading></icon-loading>`
189+
: html` <svg
190+
viewBox="0 0 24 24"
191+
width="1.25em"
192+
height="1.25em"
193+
class="h-full w-full"
194+
>
195+
<path
196+
fill="currentColor"
197+
d="M8 7.71L18 12L8 16.29v-3.34l7.14-.95L8 11.05V7.71M12 2a10 10 0 0 1 10 10a10 10 0 0 1-10 10A10 10 0 0 1 2 12A10 10 0 0 1 12 2m0 2a8 8 0 0 0-8 8a8 8 0 0 0 8 8a8 8 0 0 0 8-8a8 8 0 0 0-8-8Z"
198+
></path>
199+
</svg>`}
189200
提交评论
190201
</button>
191202
</div>

packages/comment-widget/src/comment-form.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LitElement, html } from 'lit';
1+
import { html, LitElement } from 'lit';
22
import { state } from 'lit/decorators.js';
33
import { consume } from '@lit/context';
44
import {
@@ -11,8 +11,8 @@ import {
1111
toastContext,
1212
versionContext,
1313
} from './context';
14-
import { CommentRequest, User, Comment } from '@halo-dev/api-client';
15-
import { Ref, createRef, ref } from 'lit/directives/ref.js';
14+
import { Comment, CommentRequest, User } from '@halo-dev/api-client';
15+
import { createRef, Ref, ref } from 'lit/directives/ref.js';
1616
import { BaseForm } from './base-form';
1717
import './base-form';
1818
import { ToastManager } from './lit-toast';
@@ -50,15 +50,24 @@ export class CommentForm extends LitElement {
5050
@state()
5151
toastManager: ToastManager | undefined;
5252

53+
@state()
54+
submitting = false;
55+
5356
baseFormRef: Ref<BaseForm> = createRef<BaseForm>();
5457

5558
override render() {
56-
return html`<base-form ${ref(this.baseFormRef)} @submit="${this.onSubmit}"></base-form>`;
59+
return html` <base-form
60+
.submitting=${this.submitting}
61+
${ref(this.baseFormRef)}
62+
@submit="${this.onSubmit}"
63+
></base-form>`;
5764
}
5865

5966
async onSubmit(e: CustomEvent) {
6067
e.preventDefault();
6168

69+
this.submitting = true;
70+
6271
const data = e.detail;
6372

6473
const { displayName, email, website, content } = data || {};
@@ -78,12 +87,14 @@ export class CommentForm extends LitElement {
7887

7988
if (!this.currentUser && !this.allowAnonymousComments) {
8089
this.toastManager?.warn('请先登录');
90+
this.submitting = false;
8191
return;
8292
}
8393

8494
if (!this.currentUser && this.allowAnonymousComments) {
8595
if (!displayName || !email) {
8696
this.toastManager?.warn('请先登录或者完善信息');
97+
this.submitting = false;
8798
return;
8899
} else {
89100
commentRequest.owner = {
@@ -121,6 +132,8 @@ export class CommentForm extends LitElement {
121132
if (error instanceof Error) {
122133
this.toastManager?.error(error.message);
123134
}
135+
} finally {
136+
this.submitting = false;
124137
}
125138
}
126139
}

packages/comment-widget/src/reply-form.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import './base-form';
22
import { CommentVo, Reply, ReplyRequest, ReplyVo, User } from '@halo-dev/api-client';
3-
import { LitElement, html } from 'lit';
4-
import { Ref, createRef, ref } from 'lit/directives/ref.js';
3+
import { html, LitElement } from 'lit';
4+
import { createRef, Ref, ref } from 'lit/directives/ref.js';
55
import {
66
allowAnonymousCommentsContext,
77
baseUrlContext,
@@ -36,6 +36,9 @@ export class ReplyForm extends LitElement {
3636
@state()
3737
toastManager: ToastManager | undefined;
3838

39+
@state()
40+
submitting = false;
41+
3942
baseFormRef: Ref<BaseForm> = createRef<BaseForm>();
4043

4144
override connectedCallback(): void {
@@ -48,11 +51,18 @@ export class ReplyForm extends LitElement {
4851
}
4952

5053
override render() {
51-
return html`<base-form ${ref(this.baseFormRef)} @submit="${this.onSubmit}"></base-form>`;
54+
return html` <base-form
55+
.submitting=${this.submitting}
56+
${ref(this.baseFormRef)}
57+
@submit="${this.onSubmit}"
58+
></base-form>`;
5259
}
5360

5461
async onSubmit(e: CustomEvent) {
5562
e.preventDefault();
63+
64+
this.submitting = true;
65+
5666
const data = e.detail;
5767

5868
const { displayName, email, website, content } = data || {};
@@ -70,12 +80,14 @@ export class ReplyForm extends LitElement {
7080

7181
if (!this.currentUser && !this.allowAnonymousComments) {
7282
this.toastManager?.warn('请先登录');
83+
this.submitting = false;
7384
return;
7485
}
7586

7687
if (!this.currentUser && this.allowAnonymousComments) {
7788
if (!displayName || !email) {
7889
this.toastManager?.warn('请先登录或者完善信息');
90+
this.submitting = false;
7991
return;
8092
} else {
8193
replyRequest.owner = {
@@ -116,6 +128,8 @@ export class ReplyForm extends LitElement {
116128
if (error instanceof Error) {
117129
this.toastManager?.error(error.message);
118130
}
131+
} finally {
132+
this.submitting = false;
119133
}
120134
}
121135
}

0 commit comments

Comments
 (0)