Skip to content

Commit

Permalink
feat: add debounce handling for submit comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Wang <[email protected]>
  • Loading branch information
ruibaby committed Sep 13, 2024
1 parent 844b269 commit ec8059d
Show file tree
Hide file tree
Showing 3 changed files with 1,245 additions and 1,000 deletions.
4 changes: 3 additions & 1 deletion packages/comment-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
"dayjs": "^1.11.10",
"emoji-mart": "^5.5.2",
"javascript-time-ago": "^2.5.9",
"lit": "^3.1.2"
"lit": "^3.1.2",
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
"lit-analyzer": "^2.0.3"
}
}
13 changes: 9 additions & 4 deletions packages/comment-widget/src/base-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { consume } from '@lit/context';
import { css, html, LitElement } from 'lit';
import { property, state } from 'lit/decorators.js';
import { createRef, Ref, ref } from 'lit/directives/ref.js';
import { debounce } from 'lodash-es';
import {
allowAnonymousCommentsContext,
baseUrlContext,
Expand Down Expand Up @@ -254,6 +255,13 @@ export class BaseForm extends LitElement {
`;
}

private debouncedSubmit = debounce((data: Record<string, unknown>) => {
const event = new CustomEvent('submit', {
detail: data,
});
this.dispatchEvent(event);
}, 300);

onSubmit(e: Event) {
e.preventDefault();
const form = e.target as HTMLFormElement;
Expand All @@ -270,10 +278,7 @@ export class BaseForm extends LitElement {
})
);

const event = new CustomEvent('submit', {
detail: data,
});
this.dispatchEvent(event);
this.debouncedSubmit(data);
}

resetForm() {
Expand Down
Loading

0 comments on commit ec8059d

Please sign in to comment.