Skip to content

Commit

Permalink
Feat/email type (#21)
Browse files Browse the repository at this point in the history
* added email specific answers and question types to api

* added text input for email type in ui

* added a email-question in the 'short' servey

* Added docs and changed email-text to email

* added proper validations for email
  • Loading branch information
anveshthakur authored Sep 27, 2024
1 parent a822276 commit fc446fb
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ Presents a question where users can only answer "yes" or "no".
label: Is Berlin the capital of Germany?
```

### Email

Prompts user to enter their email
```yaml
- type: email
label: Please enter your email.
```

## Responses

Responses can be shown in the UI and exported as a JSON. Alternatively you can use REST API to get survey resposnes:
Expand Down
2 changes: 2 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/google/uuid v1.6.0
github.com/labstack/echo/v4 v4.12.0
Expand All @@ -16,6 +17,7 @@ require (
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
Expand Down
7 changes: 7 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -19,6 +22,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
Expand Down Expand Up @@ -69,6 +74,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down Expand Up @@ -106,6 +112,7 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJ
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 18 additions & 0 deletions api/pkg/types/answers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"encoding/json"
"fmt"
"time"

validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
)

const DATE_FORMAT = "2006-01-02"
Expand Down Expand Up @@ -151,3 +154,18 @@ func (a BoolAnswer) Value() (driver.Value, error) {
func (a *BoolAnswer) Validate(q Question) error {
return nil
}

type EmailAnswer struct {
AnswerValue string `json:"value"`
}

func (a EmailAnswer) Value() (driver.Value, error) {
return json.Marshal(a)
}

func (a *EmailAnswer) Validate(q Question) error {
if err := validation.Validate(a.AnswerValue, validation.Required, is.Email); err != nil {
return err
}
return nil
}
4 changes: 4 additions & 0 deletions api/pkg/types/questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
QuestionType_Rating QuestionType = "rating"
QuestionType_Ranking QuestionType = "ranking"
QuestionType_YesNo QuestionType = "yes-no"
QuestionType_Email QuestionType = "email"
)

var supportedQuestionTypes = map[QuestionType]bool{
Expand All @@ -29,6 +30,7 @@ var supportedQuestionTypes = map[QuestionType]bool{
QuestionType_Rating: true,
QuestionType_Ranking: true,
QuestionType_YesNo: true,
QuestionType_Email: true,
}

type Questions struct {
Expand Down Expand Up @@ -178,6 +180,8 @@ func (q Question) GetAnswerType() (Answer, error) {
return &MultiOptionsAnswer{}, nil
case QuestionType_YesNo:
return &BoolAnswer{}, nil
case QuestionType_Email:
return &EmailAnswer{}, nil
default:
return nil, fmt.Errorf("question type %s is not supported", q.Type)
}
Expand Down
2 changes: 2 additions & 0 deletions api/surveys/simple/questions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ questions:
optionsFromVariable: german-city-options
- type: yes-no
label: Is Berlin the capital of Germany?
- type: email
label: Please Enter your email.
15 changes: 15 additions & 0 deletions ui/src/components/app/survey/SurveyQuestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ export default function SurveyQuestions({
/>
)
break
case SurveyQuestionType.EmailText:
questionContent = (
<TextInput
defaultValue={selectedStringValue || ''}
placeholder="Type your email here..."
required
type='email'
onChange={(e) => {
const newValue = e.target.value === '' ? undefined : e.target.value
setSelectedStringValue(newValue)
}}
/>
)
break
case SurveyQuestionType.LongText:
questionContent = (
<Textarea
Expand Down Expand Up @@ -313,6 +327,7 @@ export default function SurveyQuestions({
case SurveyQuestionType.SingleChoice:
case SurveyQuestionType.ShortText:
case SurveyQuestionType.LongText:
case SurveyQuestionType.EmailText:
case SurveyQuestionType.Date:
payload = {
value: selectedStringValue,
Expand Down
1 change: 1 addition & 0 deletions ui/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export enum SurveyQuestionType {
Rating = 'rating',
Ranking = 'ranking',
YesNo = 'yes-no',
EmailText = 'email'
}

export enum SurveySessionStatus {
Expand Down

0 comments on commit fc446fb

Please sign in to comment.