import React from 'react'; import { Button, Message } from 'semantic-ui-react'; import { Form, Field, withFormik, FormikProps } from 'formik'; import * as Yup from 'yup'; import { History } from 'history'; import { TxButton, Section } from '@polkadot/joy-utils/react/components'; import { SubmittableResult } from '@polkadot/api'; import { withMulti } from '@polkadot/react-api/hoc'; import * as JoyForms from '@polkadot/joy-utils/react/components/forms'; import { CategoryId, Category } from '@joystream/types/forum'; import { useMyAccount } from '@polkadot/joy-utils/react/hooks'; import { UrlHasIdProps, CategoryCrumbs } from './utils'; import { withOnlyForumSudo } from './ForumSudo'; import { withForumCalls } from './calls'; import { ValidationProps, withCategoryValidation } from './validation'; import { TxFailedCallback, TxCallback } from '@polkadot/react-components/Status/types'; import { createMock } from '@joystream/types'; import { useApi } from '@polkadot/react-hooks'; const buildSchema = (props: ValidationProps) => { const { categoryTitleConstraint, categoryDescriptionConstraint } = props; if (!categoryTitleConstraint || !categoryDescriptionConstraint) { throw new Error('Missing some validation constraints'); } const minTitle = categoryTitleConstraint.min.toNumber(); const maxTitle = categoryTitleConstraint.max.toNumber(); const minDescr = categoryDescriptionConstraint.min.toNumber(); const maxDescr = categoryDescriptionConstraint.max.toNumber(); return Yup.object().shape({ title: Yup.string() .min(minTitle, `Category title is too short. Minimum length is ${minTitle} chars.`) .max(maxTitle, `Category title is too long. Maximum length is ${maxTitle} chars.`) .required('Category title is required'), description: Yup.string() .min(minDescr, `Category description is too short. Minimum length is ${minDescr} chars.`) .max(maxDescr, `Category description is too long. Maximum length is ${maxDescr} chars.`) .required('Category description is required') }); }; type OuterProps = ValidationProps & { history?: History; id?: CategoryId; parentId?: CategoryId; struct?: Category; }; type FormValues = { title: string; description: string; }; type FormProps = OuterProps & FormikProps; const LabelledField = JoyForms.LabelledField(); const LabelledText = JoyForms.LabelledText(); const InnerForm = (props: FormProps) => { const { history, id, parentId, struct, values, dirty, isValid, isSubmitting, setSubmitting, resetForm } = props; const { title, description } = values; const onSubmit = (sendTx: () => void) => { if (isValid) sendTx(); }; const onTxFailed: TxFailedCallback = (txResult: SubmittableResult | null) => { setSubmitting(false); if (txResult == null) { // Tx cancelled. } }; const onTxSuccess: TxCallback = (_txResult: SubmittableResult) => { setSubmitting(false); if (!history) return; // Get id of newly created category: let _id = id; if (!_id) { _txResult.events.find((event) => { const { event: { data, method } } = event; if (method === 'CategoryCreated') { _id = data.toArray()[0] as CategoryId; } return true; }); } // Redirect to category view: if (_id) { history.push('/forum/categories/' + _id.toString()); } }; const isNew = struct === undefined; const isSubcategory = parentId !== undefined; const buildTxParams = () => { if (!isValid) return []; if (isNew) { return [ createMock('Option', parentId), title, description ]; } else { // NOTE: currently update_category doesn't support title and description updates. return []; } }; const categoryWord = isSubcategory ? 'subcategory' : 'category'; const form =