1- import { Button , Dialog , DialogContent , Theme } from "@material-ui/core" ;
1+ import {
2+ Button ,
3+ Dialog ,
4+ DialogContent ,
5+ Snackbar ,
6+ Theme ,
7+ } from "@material-ui/core" ;
28import { Formik , Form , FieldArray } from "formik" ;
39import React from "react" ;
410import FormikTextField from "../../FormikTextField" ;
511import * as yup from "yup" ;
612import { createStyles , makeStyles } from "@material-ui/core/styles" ;
713import { pollType } from "../MeetingTabs" ;
14+ import { Alert } from "@material-ui/lab" ;
15+ import { createPoll , updatePoll } from "src/utils/pollCalls" ;
16+ import useTokenStore from "src/store/tokenStore" ;
17+ import { mutate } from "swr" ;
18+ import { server } from "src/store/global" ;
819
920export const useStyles = makeStyles ( ( theme : Theme ) =>
1021 createStyles ( {
@@ -53,13 +64,17 @@ export const useStyles = makeStyles((theme: Theme) =>
5364) ;
5465
5566const validSchema = yup . object ( {
56- question : yup . string ( ) . required ( ) . min ( 3 ) . max ( 64 ) ,
57- options : yup
58- . array ( )
59- . of ( yup . string ( ) . required ( ) . min ( 2 ) . max ( 64 ) )
60- . required ( )
61- . min ( 2 , "Atleast two options required" )
62- . max ( 10 ) ,
67+ club : yup . string ( ) . required ( "Club name is required" ) . min ( 3 ) . max ( 32 ) ,
68+ question : yup . string ( ) . required ( "Question cannot be empty" ) . min ( 3 ) . max ( 64 ) ,
69+ options : yup . array ( ) . of (
70+ yup . object ( ) . shape ( {
71+ name : yup
72+ . string ( )
73+ . required ( "Option cannot be empty" )
74+ . min ( 3 , "Option cannot be empty" )
75+ . max ( 32 ) ,
76+ } )
77+ ) ,
6378} ) ;
6479
6580interface PollFormProps {
@@ -70,24 +85,64 @@ interface PollFormProps {
7085
7186const PollForm : React . FC < PollFormProps > = ( { close, open, initialVal } ) => {
7287 const classes = useStyles ( ) ;
88+ const accessToken = useTokenStore ( ( state ) => state . token ) ;
89+ const [ openError , setOpenError ] = React . useState ( false ) ;
90+ const [ errorText , setErrorText ] = React . useState ( "Permission Denied" ) ;
91+
92+ const handleClose = ( event ?: React . SyntheticEvent , reason ?: string ) => {
93+ if ( reason === "clickaway" ) {
94+ return ;
95+ }
96+
97+ setOpenError ( false ) ;
98+ } ;
7399
74100 return (
75101 < Dialog aria-labelledby = "poll-dialog" open = { open } >
76102 < DialogContent >
77103 < Formik
78104 initialValues = { {
105+ club : initialVal ? initialVal . club : "" ,
79106 question : initialVal ? initialVal . question : "" ,
80- options : initialVal ? initialVal . options : [ "" ] ,
107+ options : initialVal ? initialVal . options : [ { name : "" } ] ,
81108 } }
82109 validationSchema = { validSchema }
83110 onSubmit = { async ( data , { setSubmitting } ) => {
84111 setSubmitting ( true ) ;
85- console . log ( data ) ;
112+ if ( data . options . length < 2 || data . options . length > 5 ) {
113+ setErrorText ( "Poll must have 2-5 options" ) ;
114+ setOpenError ( true ) ;
115+ } else {
116+ if ( initialVal ) {
117+ const res = await updatePoll ( data , initialVal . _id , accessToken ) ;
118+ if ( ! res . done ) {
119+ setErrorText ( "Permission Denied" ) ;
120+ setOpenError ( true ) ;
121+ } else {
122+ mutate ( [ `${ server } /api/poll/get_all` , accessToken ] ) ;
123+ close ( ) ;
124+ }
125+ } else {
126+ const res = await createPoll ( data , accessToken ) ;
127+ if ( ! res . done ) {
128+ setErrorText ( "Permission Denied" ) ;
129+ setOpenError ( true ) ;
130+ } else {
131+ mutate ( [ `${ server } /api/poll/get_all` , accessToken ] ) ;
132+ close ( ) ;
133+ }
134+ }
135+ }
86136 setSubmitting ( false ) ;
87137 } }
88138 >
89139 { ( { isSubmitting, values } ) => (
90140 < Form className = { classes . root } noValidate autoComplete = "off" >
141+ < FormikTextField
142+ label = "Club Name"
143+ className = { classes . input }
144+ name = "club"
145+ />
91146 < FormikTextField
92147 label = "Question"
93148 className = { classes . input }
@@ -103,7 +158,7 @@ const PollForm: React.FC<PollFormProps> = ({ close, open, initialVal }) => {
103158 < FormikTextField
104159 label = { `Option ${ key + 1 } ` }
105160 className = { classes . optInput }
106- name = { `options.${ key } ` }
161+ name = { `options.${ key } .name ` }
107162 />
108163 < Button
109164 className = { classes . optBtn }
@@ -114,7 +169,10 @@ const PollForm: React.FC<PollFormProps> = ({ close, open, initialVal }) => {
114169 </ Button >
115170 </ div >
116171 ) ) }
117- < Button onClick = { ( ) => arr . push ( "" ) } variant = "outlined" >
172+ < Button
173+ onClick = { ( ) => arr . push ( { name : "" } ) }
174+ variant = "outlined"
175+ >
118176 Add
119177 </ Button >
120178 </ div >
@@ -145,6 +203,11 @@ const PollForm: React.FC<PollFormProps> = ({ close, open, initialVal }) => {
145203 ) }
146204 </ Formik >
147205 </ DialogContent >
206+ < Snackbar open = { openError } autoHideDuration = { 6000 } onClose = { handleClose } >
207+ < Alert onClose = { handleClose } severity = "error" >
208+ { errorText }
209+ </ Alert >
210+ </ Snackbar >
148211 </ Dialog >
149212 ) ;
150213} ;
0 commit comments