mirror of
https://github.com/oceanprotocol/market.git
synced 2024-11-15 01:34:57 +01:00
Feature/allow changing of fixed rate price (#399)
* added price field to edit metadata form * use method for updating price * get exchangeId for setRate function * setRate method update * misc fixes Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * revert price Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * get correct exchange id Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * add price to MetadataEditForm interface * fixed lint error * disable update price on assets with dynamic price * display price input only on fixed price * price input changes * fixed price input displayed when no price is set * review fixes * fixes Co-authored-by: mihaisc <mihai.scarlat@smartcontrol.ro> Co-authored-by: Norbi <katunanorbert@gmai.com>
This commit is contained in:
parent
fa6554a23f
commit
cf44ab7f51
@ -20,6 +20,15 @@
|
|||||||
"rows": 10,
|
"rows": 10,
|
||||||
"required": true
|
"required": true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "price",
|
||||||
|
"label": "New Price",
|
||||||
|
"type": "number",
|
||||||
|
"min": "1",
|
||||||
|
"placeholder": "0",
|
||||||
|
"help": "Enter a new price.",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "links",
|
"name": "links",
|
||||||
"label": "Sample file",
|
"label": "Sample file",
|
||||||
|
1
src/@types/MetaData.d.ts
vendored
1
src/@types/MetaData.d.ts
vendored
@ -29,6 +29,7 @@ export interface MetadataEditForm {
|
|||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
timeout: string
|
timeout: string
|
||||||
|
price?: number
|
||||||
links?: string | EditableMetadataLinks[]
|
links?: string | EditableMetadataLinks[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,15 +47,17 @@ export default function FormEditMetadata({
|
|||||||
data,
|
data,
|
||||||
setShowEdit,
|
setShowEdit,
|
||||||
setTimeoutStringValue,
|
setTimeoutStringValue,
|
||||||
values
|
values,
|
||||||
|
showPrice
|
||||||
}: {
|
}: {
|
||||||
data: FormFieldProps[]
|
data: FormFieldProps[]
|
||||||
setShowEdit: (show: boolean) => void
|
setShowEdit: (show: boolean) => void
|
||||||
setTimeoutStringValue: (value: string) => void
|
setTimeoutStringValue: (value: string) => void
|
||||||
values: Partial<MetadataPublishForm>
|
values: Partial<MetadataPublishForm>
|
||||||
|
showPrice: boolean
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
const { ocean } = useOcean()
|
const { ocean, config } = useOcean()
|
||||||
const {
|
const {
|
||||||
isValid,
|
isValid,
|
||||||
validateField,
|
validateField,
|
||||||
@ -79,16 +81,20 @@ export default function FormEditMetadata({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Form className={styles.form}>
|
<Form className={styles.form}>
|
||||||
{data.map((field: FormFieldProps) => (
|
{data.map(
|
||||||
<Field
|
(field: FormFieldProps) =>
|
||||||
key={field.name}
|
(!showPrice && field.name === 'price') || (
|
||||||
{...field}
|
<Field
|
||||||
component={Input}
|
key={field.name}
|
||||||
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
{...field}
|
||||||
handleFieldChange(e, field)
|
component={Input}
|
||||||
}
|
prefix={field.name === 'price' && config.oceanTokenSymbol}
|
||||||
/>
|
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
||||||
))}
|
handleFieldChange(e, field)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
|
||||||
<footer className={styles.actions}>
|
<footer className={styles.actions}>
|
||||||
<Button
|
<Button
|
||||||
|
@ -36,6 +36,7 @@ const contentQuery = graphql`
|
|||||||
label
|
label
|
||||||
help
|
help
|
||||||
type
|
type
|
||||||
|
min
|
||||||
required
|
required
|
||||||
sortOptions
|
sortOptions
|
||||||
options
|
options
|
||||||
@ -60,13 +61,25 @@ export default function Edit({
|
|||||||
const { debug } = useUserPreferences()
|
const { debug } = useUserPreferences()
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
const { ocean } = useOcean()
|
const { ocean } = useOcean()
|
||||||
const { metadata, ddo, refreshDdo } = useAsset()
|
const { metadata, ddo, refreshDdo, price } = useAsset()
|
||||||
const [success, setSuccess] = useState<string>()
|
const [success, setSuccess] = useState<string>()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
const [timeoutStringValue, setTimeoutStringValue] = useState<string>()
|
const [timeoutStringValue, setTimeoutStringValue] = useState<string>()
|
||||||
|
|
||||||
const hasFeedback = error || success
|
const hasFeedback = error || success
|
||||||
|
|
||||||
|
async function updateFixedPrice(newPrice: number) {
|
||||||
|
const setPriceResp = await ocean.fixedRateExchange.setRate(
|
||||||
|
price.address,
|
||||||
|
newPrice,
|
||||||
|
accountId
|
||||||
|
)
|
||||||
|
if (!setPriceResp) {
|
||||||
|
setError(content.form.error)
|
||||||
|
Logger.error(content.form.error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function handleSubmit(
|
async function handleSubmit(
|
||||||
values: Partial<MetadataEditForm>,
|
values: Partial<MetadataEditForm>,
|
||||||
resetForm: () => void
|
resetForm: () => void
|
||||||
@ -79,6 +92,10 @@ export default function Edit({
|
|||||||
links: typeof values.links !== 'string' ? values.links : []
|
links: typeof values.links !== 'string' ? values.links : []
|
||||||
})
|
})
|
||||||
|
|
||||||
|
price.type === 'exchange' &&
|
||||||
|
values.price !== price.value &&
|
||||||
|
(await updateFixedPrice(values.price))
|
||||||
|
|
||||||
if (!ddoEditedMetdata) {
|
if (!ddoEditedMetdata) {
|
||||||
setError(content.form.error)
|
setError(content.form.error)
|
||||||
Logger.error(content.form.error)
|
Logger.error(content.form.error)
|
||||||
@ -124,7 +141,8 @@ export default function Edit({
|
|||||||
<Formik
|
<Formik
|
||||||
initialValues={getInitialValues(
|
initialValues={getInitialValues(
|
||||||
metadata,
|
metadata,
|
||||||
ddo.findServiceByType('access').attributes.main.timeout
|
ddo.findServiceByType('access').attributes.main.timeout,
|
||||||
|
price.value
|
||||||
)}
|
)}
|
||||||
validationSchema={validationSchema}
|
validationSchema={validationSchema}
|
||||||
onSubmit={async (values, { resetForm }) => {
|
onSubmit={async (values, { resetForm }) => {
|
||||||
@ -158,6 +176,7 @@ export default function Edit({
|
|||||||
setShowEdit={setShowEdit}
|
setShowEdit={setShowEdit}
|
||||||
setTimeoutStringValue={setTimeoutStringValue}
|
setTimeoutStringValue={setTimeoutStringValue}
|
||||||
values={initialValues}
|
values={initialValues}
|
||||||
|
showPrice={price.type === 'exchange'}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<aside>
|
<aside>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { MetadataMarket, MetadataPublishForm } from '../@types/MetaData'
|
import { MetadataMarket, MetadataEditForm } from '../@types/MetaData'
|
||||||
import { secondsToString } from '../utils/metadata'
|
import { secondsToString } from '../utils/metadata'
|
||||||
import { EditableMetadataLinks } from '@oceanprotocol/lib'
|
import { EditableMetadataLinks } from '@oceanprotocol/lib'
|
||||||
import * as Yup from 'yup'
|
import * as Yup from 'yup'
|
||||||
@ -8,17 +8,20 @@ export const validationSchema = Yup.object().shape({
|
|||||||
.min(4, (param) => `Title must be at least ${param.min} characters`)
|
.min(4, (param) => `Title must be at least ${param.min} characters`)
|
||||||
.required('Required'),
|
.required('Required'),
|
||||||
description: Yup.string().required('Required').min(10),
|
description: Yup.string().required('Required').min(10),
|
||||||
|
price: Yup.number().required('Required'),
|
||||||
links: Yup.array<EditableMetadataLinks[]>().nullable(),
|
links: Yup.array<EditableMetadataLinks[]>().nullable(),
|
||||||
timeout: Yup.string().required('Required')
|
timeout: Yup.string().required('Required')
|
||||||
})
|
})
|
||||||
|
|
||||||
export function getInitialValues(
|
export function getInitialValues(
|
||||||
metadata: MetadataMarket,
|
metadata: MetadataMarket,
|
||||||
timeout: number
|
timeout: number,
|
||||||
): Partial<MetadataPublishForm> {
|
price: number
|
||||||
|
): Partial<MetadataEditForm> {
|
||||||
return {
|
return {
|
||||||
name: metadata.main.name,
|
name: metadata.main.name,
|
||||||
description: metadata.additionalInformation.description,
|
description: metadata.additionalInformation.description,
|
||||||
|
price: price,
|
||||||
links: metadata.additionalInformation.links,
|
links: metadata.additionalInformation.links,
|
||||||
timeout: secondsToString(timeout)
|
timeout: secondsToString(timeout)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user