import { Box, Button, ButtonGroup, Center, Checkbox, FormControl, FormErrorMessage, FormHelperText, FormLabel, HStack, Image, Input, Stack, Tab, TabList, TabPanel, TabPanels, Tabs, Tag, TagLabel, Text, Textarea, useToast } from '@chakra-ui/react' import {useSnekFinder} from '@jaenjs/snek-finder' import * as React from 'react' import {Controller, useForm} from 'react-hook-form' import {IFormProps, IJaenTemplate} from '../../../../../types' import {HiCloudUpload} from '@react-icons/all-files/hi/HiCloudUpload' import {HiTemplate} from '@react-icons/all-files/hi/HiTemplate' export type ContentValues = { title: string slug: string image?: string description?: string excludedFromIndex?: boolean } export interface PageContentProps extends IFormProps { jaenPageId: string template: IJaenTemplate | null publishedAt: string | undefined } /** * Component for displaying a page content. * * It includes Accordion that can be used to expand/collapse the page content. */ export const PageContent = (props: PageContentProps) => { const toast = useToast() const [defaultValues, setDefaultValues] = React.useState( props.values ) React.useEffect(() => { setDefaultValues(props.values) }, [props.values]) const { register, reset, handleSubmit, setValue, control, formState: {errors, isSubmitting, isDirty, isValid, dirtyFields} } = useForm({ defaultValues }) const finder = useSnekFinder({ mode: 'selector', onSelect: ({src}) => { setValue('image', src, { shouldDirty: true }) } }) const onSubmit = (values: ContentValues) => { const vs = { ...defaultValues, ...values } props.onSubmit(values) setDefaultValues(vs) reset(vs) toast({ title: 'Saved', description: 'Your changes have been saved.', status: 'success', duration: 5000 }) } const onReset = () => { reset(defaultValues) } const handleImageUpload = () => { finder.toggleSelector() } const handleImageRemove = () => { setValue('image', '', { shouldDirty: true }) } return ( <> {finder.finderElement} General Content
{props.template && ( {props.template.displayName} )} Title {errors.title?.message} Slug { // return if the form field is not dirty if (!dirtyFields.slug) { return true } const {externalValidation} = props if (externalValidation) { const validation = externalValidation('slug', value) if (validation) { return validation } } } })} /> {!errors.slug && ( Make sure the slug is unique between siblings. )} {errors.slug?.message} Description