-
Notifications
You must be signed in to change notification settings - Fork 12
/
CreatePoll.js
549 lines (524 loc) · 17.4 KB
/
CreatePoll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import { useMemo, useRef, useState } from "react";
import { v4 as uuid } from "uuid";
import { usePubSub } from "@videosdk.live/react-sdk";
import { sideBarModes } from "../../../utils/common";
import { Input, Label } from "@windmill/react-ui";
import { XIcon } from "@heroicons/react/outline";
import { useMeetingAppContext } from "../../../MeetingAppContextDef";
const CreatePollPart = ({
isMarkAsCorrectChecked,
setIsMarkAsCorrectChecked,
isSetTimerChecked,
setIsSetTimerChecked,
question,
setQuestion,
questionErr,
option,
setOption,
options,
setOptions,
setTimer,
_handleKeyDown,
timer,
timerErr,
correctAnswerErr,
minOptionErr,
optionErr,
}) => {
const createOptionRef = useRef(null);
//for timer
const pollTimerArr = useMemo(() => {
const pollTimerArr = [{ value: 30, Label: "30 secs" }];
for (let i = 1; i < 11; i++) {
pollTimerArr.push({
value: i * 60,
Label: `${i} min${i === 1 ? "" : "s"}`,
});
}
return pollTimerArr;
}, []);
const focusCreateOption = () => {
setTimeout(() => {
createOptionRef.current.focus();
}, 500);
};
return (
<div className={`flex flex-col xl:m-4 m-2 overflow-y-auto`}>
<input
type="text"
className="bg-gray-750 text-white text-sm rounded block w-full p-2.5 border border-gray-600 placeholder-gray-400 focus:ring-0 focus:border-purple-550"
placeholder="What you want to ask ?"
autoFocus
value={question}
onChange={(e) => setQuestion(e.target.value)}
/>
{questionErr && (
<div className="mt-[2px]">
<p className="text-xs text-red-150">Please enter proper question.</p>
</div>
)}
<div className="mt-6">
{/* option list */}
<div>
{options.length > 0 && (
<div>
{options.map((item) => {
return (
<div className="flex items-center mb-4">
{isMarkAsCorrectChecked && item.option.length !== 0 && (
<Input
type="checkbox"
value={item.isCorrect}
checked={item.isCorrect === true}
onChange={() => {
setOptions(
options.map((option) => {
if (option.optionId === item.optionId) {
option.isCorrect = !option.isCorrect;
} else {
option.isCorrect = false;
}
return option;
})
);
}}
className="bg-transparent rounded-xl h-5 w-5 border-2 border-gray-300 focus:outline-none focus:border-gray-300 focus:ring-0"
/>
)}
<div
className={` ${
item.isCorrect && item.option !== ""
? "bg-purple-550"
: "bg-customGray-900"
} w-full relative rounded ${
isMarkAsCorrectChecked && item.option !== ""
? "ml-2"
: "ml-0"
}`}
>
<input
type="text"
className={`border-none ${
item.isCorrect && item.option !== ""
? "bg-purple-550"
: "bg-customGray-900"
} text-white focus:ring-0 rounded-l`}
placeholder="Add your options"
autocomplete="off"
value={item.option}
onBlur={_handleKeyDown}
onChange={(e) => {
setOptions(
options.map((option) => {
if (option.optionId === item.optionId) {
option.option = e.target.value;
if (e.target.value === "" && item.isCorrect) {
option.isCorrect = false;
}
}
return option;
})
);
}}
/>
<button
className="absolute right-2 top-2.5"
onClick={() => {
setOptions((options) => {
const newOptions = options.filter(
({ optionId }) => {
return optionId !== item.optionId;
}
);
return newOptions;
});
}}
>
<XIcon
className={`h-5 w-5 ${
item.isCorrect && item.option !== ""
? "text-white"
: "text-gray-400"
}`}
/>
</button>
</div>
</div>
);
})}
</div>
)}
</div>
{/* end of option list */}
<div>
{/* old Text */}
<div className="flex">
{isMarkAsCorrectChecked && option.option && (
<Input
type="checkbox"
value={option.isCorrect}
checked={option.isCorrect === true}
onChange={(e) => {
setOptions((options) => {
return [
...options.map((option) => {
return { ...option, isCorrect: false };
}),
{
...option,
isCorrect: e.target.checked,
},
];
});
setOption({
option: "",
isCorrect: false,
});
}}
className="bg-transparent rounded-xl h-5 w-5 border-2 border-gray-300 focus:outline-none focus:border-gray-300 focus:ring-0"
/>
)}
<input
type={"text"}
placeholder="Add your options"
inputref={createOptionRef}
className={`border-none ${
option.isCorrect && option.option
? "bg-purple-550"
: "bg-customGray-900"
} ${
isMarkAsCorrectChecked && option.option ? "ml-2" : "ml-0"
} text-white focus:ring-0 rounded w-full`}
ref={createOptionRef}
autoComplete="off"
value={option.option}
onChange={(e) =>
setOption({
optionId: uuid(),
option: e.target.value,
isCorrect: !!option.isCorrect,
})
}
onKeyDown={_handleKeyDown}
onBlur={_handleKeyDown}
/>
</div>
{/* end of old Text */}
{/* dummy Text */}
{option?.option?.length > 0 && (
<div style={{ display: "flex" }}>
<input
type={"text"}
placeholder="Add your options"
autocomplete="off"
onChange={(e) => {}}
onFocus={(e) => {
_handleKeyDown(e);
focusCreateOption();
}}
className="w-full bg-customGray-900 mt-4 focus:ring-0 rounded"
/>
</div>
)}
{/* end of dummy Text */}
{minOptionErr && (
<p className="text-xs text-red-150 mt-1">
Please add atleast 2 options.
</p>
)}
{optionErr && (
<p className="text-xs text-red-150 mt-1">
Please enter valid option value.
</p>
)}
<div className="mt-8">
<div className="flex items-center pl-1">
<Label check>
<Input
type="checkbox"
onClick={(e) => {
setIsMarkAsCorrectChecked((s) => !s);
}}
className="bg-transparent h-5 w-5 border-2 border-gray-300 focus:outline-none focus:border-gray-300 focus:ring-0"
/>
<p className="text-base text-white ml-3">Mark correct option</p>
</Label>
</div>
{correctAnswerErr && (
<p className="text-xs text-red-150 mt-1">
{
"Please check any one option as correct if `isMarkAsCorrectChecked`"
}
</p>
)}
<div className="flex flex-col">
<div className="flex items-center flex-row mt-5 relative">
<div className="flex items-center pl-1">
<Label check>
<Input
type="checkbox"
onClick={(e) => {
setIsSetTimerChecked((s) => !s);
}}
className="bg-transparent h-5 w-5 border-2 border-gray-300 focus:outline-none focus:border-gray-300 focus:ring-0"
/>
<p className="text-base text-white ml-3">Set Timer</p>
</Label>
</div>
{isSetTimerChecked && (
<select
value={timer}
onChange={(e) => {
setTimer(e.target.value);
}}
className="ml-3 absolute left-1/3 -bottom-1 right-0 form-select cursor-pointer appearance-none block w-2/6 px-3 py-1.5 text-base font-normal text-white bg-gray-700 border-none rounded transition ease-in-out m-0 focus:text-white focus:bg-gray-700 focus:border-none focus:outline-none focus:ring-0"
>
{pollTimerArr.map((item) => {
return (
<option className="cursor-pointer" value={item.value}>
{item.Label}
</option>
);
})}
</select>
)}
</div>
<div className="mt-1 ml-1">
{timerErr && (
<p className="text-xs text-red-150">
{"Timer should be more than 30 seconds."}
</p>
)}
</div>
</div>
</div>
</div>
</div>
</div>
);
};
const PollButtonPart = ({
publishCreatePoll,
publishDraftPoll,
question,
options,
timer,
setQuestionErr,
isMarkAsCorrectChecked,
isSetTimerChecked,
setTimerErr,
setCorrectAnswerErr,
setMinOptionErr,
setOptionErr,
}) => {
const { polls, setSideBarMode } = useMeetingAppContext();
const singleOption = options?.map((option) => {
return option.option;
});
const handleValidation = ({
question,
options,
isSetTimerChecked,
timer,
isMarkAsCorrectChecked,
}) => {
let isValid = true;
if (
question.length >= 2 &&
/^[^-\s][a-zA-Z0-9_!@#$%^&*()`~.,<>{}[\]<>?_=+\-|;:\\'\"\/\s-]+$/i.test(
question.trim()
)
) {
setQuestionErr(false);
} else {
isValid = false;
setQuestionErr(true);
return false;
}
if (options?.length < 2) {
isValid = false;
setMinOptionErr(true);
return false;
} else {
setMinOptionErr(false);
}
for (let i = 0; i < singleOption.length; i++) {
if (singleOption[i].length < 1) {
isValid = false;
setOptionErr(true);
return false;
} else {
setOptionErr(false);
}
}
// check time timer if `isSetTimerChecked`
if (isSetTimerChecked && timer < 30) {
isValid = false;
setTimerErr(true);
return false;
} else {
setTimerErr(false);
}
if (
isMarkAsCorrectChecked &&
options.findIndex(({ isCorrect }) => isCorrect) === -1
) {
// please check any one option as correct if `isMarkAsCorrectChecked`
isValid = false;
setCorrectAnswerErr(true);
return false;
} else {
setCorrectAnswerErr(false);
}
return isValid;
};
return (
<div className="flex xl:pt-4 xl:pb-2 xl:pl-4 xl:pr-4 pt-2 pb-1 pl-2 pr-2">
<button
className="w-1/2 bg-gray-700 text-white p-2 rounded"
onClick={() => {
const isValid = handleValidation({
question,
options,
isSetTimerChecked,
isMarkAsCorrectChecked,
timer,
});
if (isValid) {
publishDraftPoll(
{
id: uuid(),
question: question.trim(),
options: options.map((option) => ({
...option,
option: option.option.trim(),
})),
timeout: isSetTimerChecked ? timer : 0,
hasCorrectAnswer: isMarkAsCorrectChecked ? true : false,
hasTimer: isSetTimerChecked ? true : false,
isActive: false,
},
{
persist: true,
}
);
setSideBarMode(sideBarModes.POLLS);
}
}}
>
Save
</button>
<button
className="w-1/2 ml-2 p-2 text-white bg-purple-550 rounded"
onClick={() => {
const isValid = handleValidation({
question,
options,
isSetTimerChecked,
isMarkAsCorrectChecked,
timer,
});
if (isValid) {
publishCreatePoll(
{
id: uuid(),
question: question.trim(),
options: options.map((option) => ({
...option,
option: option.option.trim(),
})),
timeout: isSetTimerChecked ? timer : 0,
hasCorrectAnswer: isMarkAsCorrectChecked ? true : false,
hasTimer: isSetTimerChecked ? true : false,
isActive: true,
index: polls.length + 1,
},
{ persist: true }
);
setSideBarMode(sideBarModes.POLLS);
}
}}
>
Launch
</button>
</div>
);
};
const CreatePoll = ({ panelHeight }) => {
const [isMarkAsCorrectChecked, setIsMarkAsCorrectChecked] = useState(false);
const [isSetTimerChecked, setIsSetTimerChecked] = useState(false);
const [question, setQuestion] = useState("");
const [questionErr, setQuestionErr] = useState(false);
const [optionErr, setOptionErr] = useState(false);
const [option, setOption] = useState({
optionId: uuid(),
option: null,
isCorrect: false,
});
const [options, setOptions] = useState([]);
const [timer, setTimer] = useState(30);
const [timerErr, setTimerErr] = useState(false);
const [correctAnswerErr, setCorrectAnswerErr] = useState(false);
const [minOptionErr, setMinOptionErr] = useState(false);
const _handleKeyDown = (e) => {
if (
e.key === "Enter" ||
e.type === "mouseleave" ||
e.type === "focus" ||
e.type === "blur"
) {
if (option?.option?.length >= 1 && option?.option.trim()) {
e.preventDefault();
setOptions([...options, option]);
setOption({ option: "", isCorrect: false });
}
}
};
const { publish: publishCreatePoll } = usePubSub(`CREATE_POLL`);
const { publish: publishDraftPoll } = usePubSub(`DRAFT_A_POLL`);
const Height = panelHeight;
return (
<div
className="overflow-y-auto overflow-x-hidden"
style={{ height: Height }}
>
<div className="flex flex-col justify-between flex-1 h-full">
<CreatePollPart
isMarkAsCorrectChecked={isMarkAsCorrectChecked}
setIsMarkAsCorrectChecked={setIsMarkAsCorrectChecked}
isSetTimerChecked={isSetTimerChecked}
setIsSetTimerChecked={setIsSetTimerChecked}
question={question}
setQuestion={setQuestion}
questionErr={questionErr}
option={option}
setOption={setOption}
options={options}
setOptions={setOptions}
_handleKeyDown={_handleKeyDown}
setTimer={setTimer}
timer={timer}
timerErr={timerErr}
correctAnswerErr={correctAnswerErr}
minOptionErr={minOptionErr}
optionErr={optionErr}
/>
<PollButtonPart
publishCreatePoll={publishCreatePoll}
publishDraftPoll={publishDraftPoll}
question={question}
options={options}
timer={timer}
setQuestionErr={setQuestionErr}
isMarkAsCorrectChecked={isMarkAsCorrectChecked}
isSetTimerChecked={isSetTimerChecked}
setTimerErr={setTimerErr}
setCorrectAnswerErr={setCorrectAnswerErr}
setMinOptionErr={setMinOptionErr}
setOptionErr={setOptionErr}
/>
</div>
</div>
);
};
export default CreatePoll;