Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

prevent user from clicking submit twice (#180)

Guilherme Oenninge8c2ab418501
diff --git a/public/components/IdeaInput.tsx b/public/components/IdeaInput.tsx
index 3d4d1d97a..de5cd4f91 100644
--- a/public/components/IdeaInput.tsx
+++ b/public/components/IdeaInput.tsx
@@ -1,6 +1,6 @@
import * as React from 'react';
import Textarea from 'react-textarea-autosize';
-import { DisplayError, Button, Form } from '@fider/components/common';
+import { DisplayError, Button, ButtonClickEvent, Form } from '@fider/components/common';
import { inject, injectables } from '@fider/di';
import { Session, IdeaService, Failure } from '@fider/services';
@@ -51,12 +51,13 @@ export class IdeaInput extends React.Component<IdeaInputProps, IdeaInputState> {
}
}
- private async submit() {
+ private async submit(event: ButtonClickEvent) {
if (this.state.title) {
const result = await this.service.addIdea(this.state.title, this.state.description);
if (result.ok) {
this.form.clearFailure();
location.href = `/ideas/${result.data.number}/${result.data.slug}`;
+ event.preventEnable();
} else if (result.error) {
this.form.setFailure(result.error);
}
@@ -69,16 +70,16 @@ export class IdeaInput extends React.Component<IdeaInputProps, IdeaInputState> {
<div className="field">
<Textarea onChange={(e) => this.setState({ description: e.currentTarget.value })} placeholder="Describe your idea" />
</div>
- <Button className={buttonCss} onClick={() => this.form.submit() }>
+ <Button className={ buttonCss } onClick={ (e) => this.submit(e) }>
Submit
</Button>
</div>;
- return <Form ref={(f) => { this.form = f!; } } onSubmit={() => this.submit()}>
+ return <Form ref={(f) => { this.form = f!; } } >
<input id="new-idea-input"
type="text"
- ref={(e) => this.title = e! }
- onFocus={() => this.onTitleFocused()}
+ ref={ (e) => this.title = e! }
+ onFocus={ () => this.onTitleFocused() }
maxLength={100}
onKeyUp={(e) => { this.setState({ title: e.currentTarget.value }); }}
placeholder={this.props.placeholder} />
diff --git a/public/components/common/Button.tsx b/public/components/common/Button.tsx
index 5056eacdf..5dd549547 100644
--- a/public/components/common/Button.tsx
+++ b/public/components/common/Button.tsx
@@ -4,13 +4,23 @@ interface ButtonProps {
className?: string;
href?: string;
size?: 'mini' | 'tiny' | 'small' | 'large';
- onClick?: () => Promise<any>;
+ onClick?: (event: ButtonClickEvent) => Promise<any>;
}
interface ButtonState {
clicked: boolean;
}
+export class ButtonClickEvent {
+ private shouldEnable = true;
+ public preventEnable(): void {
+ this.shouldEnable = false;
+ }
+ public canEnable(): boolean {
+ return this.shouldEnable;
+ }
+}
+
export class Button extends React.Component<ButtonProps, ButtonState> {
private unmounted: boolean;
@@ -29,11 +39,17 @@ export class Button extends React.Component<ButtonProps, ButtonState> {
this.unmounted = true;
}
- private async click() {
+ private async click(e?: React.MouseEvent<HTMLButtonElement>) {
+ if (e) {
+ e.preventDefault();
+ e.stopPropagation();
+ }
+
+ const event = new ButtonClickEvent();
this.setState({ clicked: true });
if (this.props.onClick) {
- await this.props.onClick();
- if (!this.unmounted) {
+ await this.props.onClick(event);
+ if (!this.unmounted && event.canEnable()) {
this.setState({ clicked: false });
}
}
@@ -42,11 +58,11 @@ export class Button extends React.Component<ButtonProps, ButtonState> {
public render() {
const cssClasses = `ui ${this.props.size} button ${this.props.className || ''} ${this.state.clicked ? 'loading disabled' : ''}`;
if (this.props.href) {
- return <a href={this.props.href} className={cssClasses} onClick={() => this.click()}>
+ return <a href={ this.props.href } className={ cssClasses } onClick={ () => this.click() }>
{ this.props.children }
</a>;
} else {
- return <button className={ cssClasses } onClick={() => this.click()}>
+ return <button type="button" className={ cssClasses } onClick={ (e) => this.click(e) }>
{ this.props.children }
</button>;
}
diff --git a/public/components/common/Form.tsx b/public/components/common/Form.tsx
index 26527b1df..c4c71db77 100644
--- a/public/components/common/Form.tsx
+++ b/public/components/common/Form.tsx
@@ -3,7 +3,7 @@ import { Failure } from '@fider/services';
import { DisplayError } from './';
interface FormProps {
- onSubmit: () => Promise<any>;
+ onSubmit?: () => Promise<any>;
}
interface FormState {
@@ -18,9 +18,9 @@ export class Form extends React.Component<FormProps, FormState> {
public async submit(e?: React.FormEvent<HTMLFormElement>) {
if (e) {
e.preventDefault();
- if (this.props.onSubmit) {
- await this.props.onSubmit();
- }
+ }
+ if (this.props.onSubmit) {
+ await this.props.onSubmit();
}
}
@@ -33,7 +33,7 @@ export class Form extends React.Component<FormProps, FormState> {
}
public render() {
- return <form autoComplete="off" className="ui form" onSubmit={(e) => this.submit(e)}>
+ return <form autoComplete="off" className="ui form" onSubmit={ (e) => this.submit(e) }>
<DisplayError error={this.state && this.state.failure} />
{ this.props.children }
</form>;
diff --git a/public/components/common/SignInControl.tsx b/public/components/common/SignInControl.tsx
index e7e4f6eaf..76e836a2d 100644
--- a/public/components/common/SignInControl.tsx
+++ b/public/components/common/SignInControl.tsx
@@ -1,5 +1,5 @@
import * as React from 'react';
-import { SocialSignInButton, Form } from '@fider/components/common';
+import { SocialSignInButton, Form, Button } from '@fider/components/common';
import { inject, injectables } from '@fider/di';
import { Session } from '@fider/services/Session';
import { AuthSettings } from '@fider/models';
@@ -40,7 +40,7 @@ export class SignInControl extends React.Component<SignInControlProps, SignInCon
this.setState({ sent: true });
setTimeout(() => {
this.setState({ sent: false });
- }, 10000);
+ }, 5000);
} else if (result.error) {
this.form.setFailure(result.error);
}
@@ -87,10 +87,10 @@ export class SignInControl extends React.Component<SignInControlProps, SignInCon
<p>Enter your e-mail address to sign in</p>
<Form ref={(f) => { this.form = f!; } } onSubmit={() => this.signIn() }>
<div id="email-signin" className="ui small action fluid input">
- <input onChange={(e) => this.setState({ email: e.currentTarget.value }) } type="text" placeholder="[email protected]" className="small" />
- <button onClick={ () => this.form.submit() } className={`ui small positive button ${this.state.email === '' && 'disabled'}`}>
+ <input onChange={ (e) => this.setState({ email: e.currentTarget.value }) } type="text" placeholder="[email protected]" className="small" />
+ <Button onClick={ () => this.signIn() } className={`positive ${this.state.email === '' && 'disabled'}`}>
Sign in
- </button>
+ </Button>
</div>
</Form>
</div> }