frontPanel/src/create.tsx

205 lines
9.8 KiB
TypeScript
Raw Normal View History

2022-05-27 14:06:07 +00:00
import React from 'react';
import moment from 'moment';
2022-06-02 23:18:58 +00:00
import {
2022-06-03 10:07:20 +00:00
Input, Select, Stack, Checkbox, Box, Text, Divider, Button, NumberInput,
NumberInputField, NumberInputStepper, NumberIncrementStepper, NumberDecrementStepper, useNumberInput,
} from '@chakra-ui/react'
import { Formik, Field, Form } from 'formik';
2022-05-27 14:06:07 +00:00
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import TimePicker from 'react-time-picker';
2022-06-02 23:18:58 +00:00
import {create} from "./API/quiz";
interface a {
name?: string,
description?: string,
config?: string,
status?: string,
fingerprinting?: boolean,
repeatable?: boolean,
note_prevented?: boolean,
mail_notifications?: boolean,
unique_answers?: boolean,
pausable?: boolean,
time_of_passing?: number,
due_to?: number,
limit?: number,
question_cnt?: number
}
2022-05-27 14:06:07 +00:00
const statusValues: any = ['draft', 'template', 'stop', 'start']
export default () => {
2022-06-02 23:18:58 +00:00
const [isSaved, setSaved] = React.useState(false)
const [isSend, setSend] = React.useState(false)
const Options = React.useMemo(() => () => (
statusValues.map((e:string,i:number)=> {
return <option key={i} value={e}>{e}</option>
})
), []);
2022-06-03 10:07:20 +00:00
const { getInputProps, getIncrementButtonProps, getDecrementButtonProps } =
useNumberInput({
min: 0,
})
const input = getInputProps()
2022-05-27 14:06:07 +00:00
return(
<Box
padding="20px"
>
<Formik
initialValues={{
name: "",
description: "",
config: "",
status: "draft",
2022-06-02 23:18:58 +00:00
fingerprinting: false,
repeatable: false,
note_prevented: false,
mail_notifications: false,
unique_answers: false,
time_of_passing: "",
pausable: false,
due_to: null,
limit: 0
}}
onSubmit={(values) => {
2022-06-03 10:07:20 +00:00
console.log(values)
2022-06-02 23:18:58 +00:00
setSend(true)
let inputBody:a = {
"status": values.status,
};
if (values.name.length !== 0) {inputBody.name = values.name}
if (values.description.length !== 0) {inputBody.description = values.description}
if (values.due_to !== null) {inputBody.due_to = Number(moment(values.due_to).format("x"))}
if (values.due_to !== null) {console.log(moment(values.due_to).format("x"))}
if (values.time_of_passing.length !== 0) {inputBody.time_of_passing = moment.duration(values.time_of_passing).asSeconds()}
if (values.time_of_passing.length !== 0) {console.log(moment.duration(values.time_of_passing).asSeconds())}
if (values.limit !== 0) {inputBody.limit = values.limit}
if (values.fingerprinting) {inputBody.fingerprinting = true}
if (values.repeatable) {inputBody.repeatable = true}
if (values.note_prevented) {inputBody.note_prevented = true}
if (values.mail_notifications) {inputBody.mail_notifications = true}
if (values.unique_answers) {inputBody.unique_answers = true}
if (values.pausable) {inputBody.pausable = true}
console.log(inputBody)
create.request({body: inputBody})
.then((e: any) => {
console.log(e)
console.log(e.message)
setSaved(true)
setSend(false)
})
.catch((e: any) => {
console.log(e)
console.log(e.message)
setSaved(false)
setSend(false)
})
}}
2022-05-27 14:06:07 +00:00
>
{({ values, setFieldValue }) => {
return (
<Form>
<label htmlFor="firstName">First Name</label>
<Stack
backgroundColor="lightgray"
padding="20px"
>
<Text>Название опроса (максимум 280 символов)</Text>
<Field as={Input} placeholder='name' name="name" />
<Text>Описание опроса</Text>
<Field as={Input} placeholder='description' name="description"/>
<Field as={Input} type="hidden" placeholder='config' name="config"/>
<Text>Статус опроса</Text>
<Field as={Select} name="status">
{
<Options/>
}
</Field>
2022-06-02 23:18:58 +00:00
<Text>Количество перепрохождений</Text>
<Field
2022-06-03 10:07:20 +00:00
as={Input}
{...input}
2022-06-02 23:18:58 +00:00
name="limit"
2022-06-03 10:07:20 +00:00
value={values.limit}
onChange={(e:any) => {
setFieldValue("limit", Number(e.target.value))
}}
/>
2022-06-02 23:18:58 +00:00
</Stack>
<Stack padding="20px">
<Text>Сохранить устройство</Text>
2022-06-02 23:18:58 +00:00
<Field as={Checkbox} name="fingerprinting" isChecked={values.fingerprinting}/>
<Divider/>
<Text>Разрешить пользователям перепроходить опрос</Text>
2022-06-02 23:18:58 +00:00
<Field as={Checkbox} name="repeatable" isChecked={values.repeatable}/>
<Divider/>
<Text>Сохранять статистику неполных прохождений</Text>
2022-06-02 23:18:58 +00:00
<Field as={Checkbox} name="note_prevented" isChecked={values.note_prevented}/>
<Divider/>
<Text>Уведомлять по почте при каждом прохождении опроса</Text>
2022-06-02 23:18:58 +00:00
<Field as={Checkbox} name="mail_notifications" isChecked={values.mail_notifications}/>
<Divider/>
<Text>Сохранять статистику только для уникального прохождения опроса</Text>
2022-06-02 23:18:58 +00:00
<Field as={Checkbox} name="unique_answers" isChecked={values.unique_answers}/>
<Divider/>
</Stack>
<Stack
backgroundColor="lightgray"
padding="20px"
>
<Text>Время прохождения опроса</Text>
<Field
as={TimePicker}
2022-06-02 23:18:58 +00:00
name="time_of_passing"
format="HH : mm"
disableClock={true}
amPmAriaLabel={"am"}
onChange={(e:any) => {
if (e !== null) {
2022-06-03 10:07:20 +00:00
setFieldValue("time_of_passing", e)
} else {
//При стирании значения пользователем затирается и последнее значение
2022-06-02 23:18:58 +00:00
setFieldValue("time_of_passing", "")
}
}}
2022-06-02 23:18:58 +00:00
value={values.time_of_passing}
/>
<Text>Разрешить ставить на паузу</Text>
2022-06-02 23:18:58 +00:00
<Field as={Checkbox} name="pausable" isDisabled={values.time_of_passing.length === 0 ? true : false}/>
<Text>Дата проведения опроса</Text>
<Field
as={DatePicker}
2022-06-02 23:18:58 +00:00
name="due_to"
selected={values.due_to}
dateFormat="MM/dd/yyyy"
onChange={(e:any) => {
2022-06-02 23:18:58 +00:00
setFieldValue("due_to", e)
}}
minDate={new Date()}
/>
</Stack>
2022-06-02 23:18:58 +00:00
<Button
type="submit"
isLoading={isSend}
isDisabled={isSaved}
loadingText='запрос в процессе обработки'
>
{isSaved ? "Сохранено" : "Сохранить"}
</Button>
</Form>
)
}}
</Formik>
2022-05-27 14:06:07 +00:00
</Box>
)
}