1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +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:
Norbi 2021-04-22 13:55:11 +03:00 committed by GitHub
parent fa6554a23f
commit cf44ab7f51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 17 deletions

View File

@ -20,6 +20,15 @@
"rows": 10,
"required": true
},
{
"name": "price",
"label": "New Price",
"type": "number",
"min": "1",
"placeholder": "0",
"help": "Enter a new price.",
"required": true
},
{
"name": "links",
"label": "Sample file",

View File

@ -29,6 +29,7 @@ export interface MetadataEditForm {
name: string
description: string
timeout: string
price?: number
links?: string | EditableMetadataLinks[]
}

View File

@ -47,15 +47,17 @@ export default function FormEditMetadata({
data,
setShowEdit,
setTimeoutStringValue,
values
values,
showPrice
}: {
data: FormFieldProps[]
setShowEdit: (show: boolean) => void
setTimeoutStringValue: (value: string) => void
values: Partial<MetadataPublishForm>
showPrice: boolean
}): ReactElement {
const { accountId } = useWeb3()
const { ocean } = useOcean()
const { ocean, config } = useOcean()
const {
isValid,
validateField,
@ -79,16 +81,20 @@ export default function FormEditMetadata({
return (
<Form className={styles.form}>
{data.map((field: FormFieldProps) => (
<Field
key={field.name}
{...field}
component={Input}
onChange={(e: ChangeEvent<HTMLInputElement>) =>
handleFieldChange(e, field)
}
/>
))}
{data.map(
(field: FormFieldProps) =>
(!showPrice && field.name === 'price') || (
<Field
key={field.name}
{...field}
component={Input}
prefix={field.name === 'price' && config.oceanTokenSymbol}
onChange={(e: ChangeEvent<HTMLInputElement>) =>
handleFieldChange(e, field)
}
/>
)
)}
<footer className={styles.actions}>
<Button

View File

@ -36,6 +36,7 @@ const contentQuery = graphql`
label
help
type
min
required
sortOptions
options
@ -60,13 +61,25 @@ export default function Edit({
const { debug } = useUserPreferences()
const { accountId } = useWeb3()
const { ocean } = useOcean()
const { metadata, ddo, refreshDdo } = useAsset()
const { metadata, ddo, refreshDdo, price } = useAsset()
const [success, setSuccess] = useState<string>()
const [error, setError] = useState<string>()
const [timeoutStringValue, setTimeoutStringValue] = useState<string>()
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(
values: Partial<MetadataEditForm>,
resetForm: () => void
@ -79,6 +92,10 @@ export default function Edit({
links: typeof values.links !== 'string' ? values.links : []
})
price.type === 'exchange' &&
values.price !== price.value &&
(await updateFixedPrice(values.price))
if (!ddoEditedMetdata) {
setError(content.form.error)
Logger.error(content.form.error)
@ -124,7 +141,8 @@ export default function Edit({
<Formik
initialValues={getInitialValues(
metadata,
ddo.findServiceByType('access').attributes.main.timeout
ddo.findServiceByType('access').attributes.main.timeout,
price.value
)}
validationSchema={validationSchema}
onSubmit={async (values, { resetForm }) => {
@ -158,6 +176,7 @@ export default function Edit({
setShowEdit={setShowEdit}
setTimeoutStringValue={setTimeoutStringValue}
values={initialValues}
showPrice={price.type === 'exchange'}
/>
<aside>

View File

@ -1,4 +1,4 @@
import { MetadataMarket, MetadataPublishForm } from '../@types/MetaData'
import { MetadataMarket, MetadataEditForm } from '../@types/MetaData'
import { secondsToString } from '../utils/metadata'
import { EditableMetadataLinks } from '@oceanprotocol/lib'
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`)
.required('Required'),
description: Yup.string().required('Required').min(10),
price: Yup.number().required('Required'),
links: Yup.array<EditableMetadataLinks[]>().nullable(),
timeout: Yup.string().required('Required')
})
export function getInitialValues(
metadata: MetadataMarket,
timeout: number
): Partial<MetadataPublishForm> {
timeout: number,
price: number
): Partial<MetadataEditForm> {
return {
name: metadata.main.name,
description: metadata.additionalInformation.description,
price: price,
links: metadata.additionalInformation.links,
timeout: secondsToString(timeout)
}