Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

input validation

goenning419fe5a5987a
diff --git a/app/feedback/handler.go b/app/feedback/handler.go
index e8cd879d7..15ab7c587 100644
--- a/app/feedback/handler.go
+++ b/app/feedback/handler.go
@@ -1,6 +1,8 @@
package feedback
import (
+ "strings"
+
"github.com/WeCanHearYou/wechy/app"
"github.com/labstack/echo"
)
@@ -42,6 +44,12 @@ func (h IndexHandlder) Post() app.HandlerFunc {
return c.Failure(err)
}
+ if strings.Trim(input.Title, " ") == "" {
+ return c.JSON(400, echo.Map{
+ "message": "Idea title is required.",
+ })
+ }
+
idea, err := h.ideaService.Save(c.Tenant().ID, c.Claims().UserID, input.Title, input.Description)
if err != nil {
return c.Failure(err)
@@ -92,6 +100,12 @@ func (h IndexHandlder) PostComment() app.HandlerFunc {
return c.Failure(err)
}
+ if strings.Trim(input.Content, " ") == "" {
+ return c.JSON(400, echo.Map{
+ "message": "Comment is required.",
+ })
+ }
+
ideaID, err := c.ParamAsInt("id")
if err != nil {
return c.Failure(err)
diff --git a/public/components/CommentInput.tsx b/public/components/CommentInput.tsx
index b86e3e658..850a0f6c4 100644
--- a/public/components/CommentInput.tsx
+++ b/public/components/CommentInput.tsx
@@ -1,7 +1,9 @@
import axios from "axios";
import * as React from "react";
+
import { Idea } from "../models";
import { getCurrentUser } from "../storage";
+import { DisplayError } from "./common";
import { SocialSignInButton } from "./SocialSignInButton";
interface CommentInputProps {
@@ -11,6 +13,7 @@ interface CommentInputProps {
interface CommentInputState {
content: string;
clicked: boolean;
+ error?: Error;
}
export class CommentInput extends React.Component<CommentInputProps, CommentInputState> {
@@ -23,13 +26,23 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpu
}
public async submit() {
- this.setState({ clicked: true });
-
- const response = await axios.post(`/api/ideas/${this.props.idea.id}/comments`, {
- content: this.state.content
+ this.setState({
+ clicked: true,
+ error: undefined
});
- location.reload();
+ try {
+ await axios.post(`/api/ideas/${this.props.idea.id}/comments`, {
+ content: this.state.content
+ });
+
+ location.reload();
+ } catch (ex) {
+ this.setState({
+ clicked: false,
+ error: ex.response.data
+ });
+ }
}
public render() {
@@ -37,6 +50,7 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpu
const buttonClasses = `ui blue labeled submit icon button ${this.state.clicked && "loading disabled"}`;
const addComment = user ? <form className="ui reply form">
+ <DisplayError error={this.state.error} />
<div className="field">
<textarea onKeyUp={(e) => { this.setState({ content: e.currentTarget.value }); }}
placeholder="Leave your comment here..."></textarea>
@@ -47,7 +61,7 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpu
</form> :
<div className="ui message">
<div className="header">
- Please log in before leaving a comment
+ Please log in before leaving a comment.
</div>
<p>This only takes a second and you'll be good to go!</p>
<SocialSignInButton provider="facebook" small={true} />
diff --git a/public/components/Common.tsx b/public/components/Common.tsx
index 43d647220..2e65c4cb8 100644
--- a/public/components/Common.tsx
+++ b/public/components/Common.tsx
@@ -1,5 +1,6 @@
import * as React from "react";
import md5 = require("md5");
+import { AxiosError, AxiosResponse } from "axios";
export const Gravatar = (props: {email?: string}) => {
const hash = props.email ? md5(props.email) : "";
@@ -15,3 +16,16 @@ export const MultiLineText = (props: {text?: string}) => {
<span>{item}<br/></span>
)}</div>;
};
+
+export const DisplayError = (props: {error?: Error}) => {
+ if (!props.error) {
+ return <div></div>;
+ }
+
+ return <div className="ui negative message">
+ <div className="header">
+ Oops, an error ocurred...
+ </div>
+ <p>{ props.error.message }</p>
+ </div>;
+};
diff --git a/public/components/IdeaInput.tsx b/public/components/IdeaInput.tsx
index 260e467c5..0631bf447 100644
--- a/public/components/IdeaInput.tsx
+++ b/public/components/IdeaInput.tsx
@@ -1,11 +1,13 @@
import axios from "axios";
import * as React from "react";
import { getCurrentUser } from "../storage";
+import { DisplayError } from "./common";
import { SocialSignInButton } from "./SocialSignInButton";
interface IdeaInputState {
title: string;
clicked: boolean;
+ error?: Error;
}
export class IdeaInput extends React.Component<{}, IdeaInputState> {
@@ -20,14 +22,24 @@ export class IdeaInput extends React.Component<{}, IdeaInputState> {
}
public async submit() {
- this.setState({ clicked: true });
-
- const response = await axios.post("/api/ideas", {
- title: this.state.title,
- description: this.description.value
+ this.setState({
+ clicked: true,
+ error: undefined
});
- location.reload();
+ try {
+ await axios.post("/api/ideas", {
+ title: this.state.title,
+ description: this.description.value
+ });
+
+ location.reload();
+ } catch (ex) {
+ this.setState({
+ clicked: false,
+ error: ex.response.data
+ });
+ }
}
public render() {
@@ -56,6 +68,7 @@ export class IdeaInput extends React.Component<{}, IdeaInputState> {
</div>;
return <div className="ui form">
+ <DisplayError error={this.state.error} />
<div className="ui fluid input">
<input id="new-idea-input"
type="text"