From 82c94c5b94f7fbeeb19f158a372e29840bff5c48 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Fri, 17 Jul 2020 15:39:40 +0200 Subject: [PATCH] prototype live preview --- src/components/pages/Publish/PublishForm.tsx | 108 ++++--------------- src/components/pages/Publish/index.tsx | 82 ++++++++++++-- 2 files changed, 97 insertions(+), 93 deletions(-) diff --git a/src/components/pages/Publish/PublishForm.tsx b/src/components/pages/Publish/PublishForm.tsx index 200c2774c..bb3547c5c 100644 --- a/src/components/pages/Publish/PublishForm.tsx +++ b/src/components/pages/Publish/PublishForm.tsx @@ -1,18 +1,12 @@ import React, { ReactElement } from 'react' -import { useNavigate } from '@reach/router' -import { toast } from 'react-toastify' import styles from './PublishForm.module.css' import { useOcean, usePublish } from '@oceanprotocol/react' -import { Formik, Form as FormFormik, Field } from 'formik' +import { useFormikContext, Form, Field } from 'formik' import Input from '../../atoms/Input' import Button from '../../atoms/Button' -import { transformPublishFormToMetadata } from './utils' import { FormContent, FormFieldProps } from '../../../@types/Form' -import { MetadataPublishForm } from '../../../@types/Metadata' -import { useSiteMetadata } from '../../../hooks/useSiteMetadata' import { Persist } from 'formik-persist' import Loader from '../../atoms/Loader' -import { initialValues, validationSchema } from './validation' export default function PublishForm({ content @@ -20,90 +14,30 @@ export default function PublishForm({ content: FormContent }): ReactElement { const { ocean, account } = useOcean() - const { - publish, - publishStepText, - publishStep, - isLoading, - publishError - } = usePublish() - const navigate = useNavigate() - const { marketAddress } = useSiteMetadata() - - async function handleSubmit(values: MetadataPublishForm): Promise { - console.log(` - Collected form values: - ---------------------- - ${JSON.stringify(values)} - `) - - const metadata = transformPublishFormToMetadata(values) - const tokensToMint = '4' // how to know this? - const serviceType = values.access === 'Download' ? 'access' : 'compute' - - console.log(` - Transformed metadata values: - ---------------------- - ${JSON.stringify(metadata)} - Cost: 1 - Tokens to mint: ${tokensToMint} - `) - - try { - const ddo = await publish(metadata as any, tokensToMint, marketAddress, [ - { serviceType, cost: '1' } - ]) - - if (publishError) { - toast.error(publishError) - return null - } - - // User feedback and redirect to new asset detail page - ddo && toast.success('Asset created successfully.') - - // TODO: reset form state and make sure persistant form in localStorage is cleared - - navigate(`/asset/${ddo.id}`) - } catch (error) { - console.error(error.message) - toast.error(error.message) - } - } + const { publishStepText, isLoading } = usePublish() + const { status, setStatus, isValid } = useFormikContext() return ( - { - await handleSubmit(values) - setSubmitting(false) - }} +
status === 'empty' && setStatus(null)} > - {({ isValid, status, setStatus }) => ( - status === 'empty' && setStatus(null)} - > - {content.data.map((field: FormFieldProps) => ( - - ))} + {content.data.map((field: FormFieldProps) => ( + + ))} - {isLoading ? ( - - ) : ( - - )} - - + {isLoading ? ( + + ) : ( + )} - + + ) } diff --git a/src/components/pages/Publish/index.tsx b/src/components/pages/Publish/index.tsx index 8ee336029..4c039b0d8 100644 --- a/src/components/pages/Publish/index.tsx +++ b/src/components/pages/Publish/index.tsx @@ -1,22 +1,92 @@ import React, { ReactElement } from 'react' +import { useNavigate } from '@reach/router' import PublishForm from './PublishForm' import styles from './index.module.css' import Web3Feedback from '../../molecules/Wallet/Feedback' import { FormContent } from '../../../@types/Form' +import { Formik } from 'formik' +import { initialValues, validationSchema } from './validation' +import { usePublish } from '@oceanprotocol/react' +import { useSiteMetadata } from '../../../hooks/useSiteMetadata' +import { MetadataPublishForm } from '../../../@types/Metadata' +import { transformPublishFormToMetadata } from './utils' +import { toast } from 'react-toastify' export default function PublishPage({ content }: { content: { form: FormContent } }): ReactElement { + const { publish, publishError } = usePublish() + const navigate = useNavigate() + const { marketAddress } = useSiteMetadata() + + async function handleSubmit(values: MetadataPublishForm): Promise { + console.log(` + Collected form values: + ---------------------- + ${JSON.stringify(values)} + `) + + const metadata = transformPublishFormToMetadata(values) + const tokensToMint = '4' // how to know this? + const serviceType = values.access === 'Download' ? 'access' : 'compute' + + console.log(` + Transformed metadata values: + ---------------------- + ${JSON.stringify(metadata)} + Cost: 1 + Tokens to mint: ${tokensToMint} + `) + + try { + const ddo = await publish(metadata as any, tokensToMint, marketAddress, [ + { serviceType, cost: '1' } + ]) + + if (publishError) { + toast.error(publishError) + return null + } + + // User feedback and redirect to new asset detail page + ddo && toast.success('Asset created successfully.') + + // TODO: reset form state and make sure persistant form in localStorage is cleared + + navigate(`/asset/${ddo.id}`) + } catch (error) { + console.error(error.message) + toast.error(error.message) + } + } + return (
- - + { + await handleSubmit(values) + setSubmitting(false) + }} + > + {({ values }) => ( + <> + + + + )} +
) }