import { Modal } from 'antd' import debug from 'debug' import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import SwiperClass from 'swiper' import { Swiper, SwiperProps, SwiperSlide } from 'swiper/react' import { Omf } from '../../../omf' import { useWindowSize } from '../../../use.window.size' import { LayoutProps } from '../layout.props' import { Field } from './field' import { FormPage } from './page' const logger = debug('layout/slider') export const SliderLayout: React.FC = (props) => { const { t } = useTranslation() const [swiper, setSwiper] = useState(null) const { height } = useWindowSize() const { design, startPage, endPage, fields } = props.form const { finish, setField } = props.submission const goNext = () => { if (!swiper) return logger('goNext') swiper.allowSlideNext = true swiper.slideNext() swiper.allowSlideNext = false } const goPrev = () => { if (!swiper) { return } logger('goPrevious') swiper.slidePrev() } const swiperConfig: SwiperProps = { direction: 'vertical', allowSlideNext: false, allowSlidePrev: true, noSwiping: true, updateOnWindowResize: true, } return (
{/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */} { logger('setSwiper') setSwiper(next) }} > {[ startPage.show ? ( ) : undefined, ...fields .map((field, i) => { if (field.type === 'hidden') { return null } return ( { await setField(field.id, values[field.id]) if (fields.length === i + 1) { await finish() } }} next={() => { if (fields.length === i + 1) { // prevent going back! swiper.allowSlidePrev = true if (!endPage.show) { Modal.success({ content: t('form:submitted'), okText: t('from:restart'), onOk: () => { window.location.reload() }, }) } } goNext() }} prev={goPrev} /> ) }) .filter((e) => e !== null), endPage.show ? ( ) : undefined, ].filter((e) => !!e)}
) }