1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00
Matthias Kretschmann 48c3190fed
Warn about publishing html files, unified file error states (#1279)
* cleanup

* warn about html files

* kick out toast error

* unify and simplify all file error states

* remove unused CSS

* same principles for provider check

* copy

* copy & comments

* fix files reset with correct initialValues, shorter optional chaining

* messaging change

* fix error message placement for provider field
2022-04-04 15:34:48 +01:00

75 lines
2.3 KiB
TypeScript

import React, { ReactElement, useState } from 'react'
import { useField, useFormikContext } from 'formik'
import UrlInput from '../URLInput'
import { InputProps } from '@shared/FormInput'
import FileInfo from '../FilesInput/Info'
import styles from './index.module.css'
import Button from '@shared/atoms/Button'
import { initialValues } from 'src/components/Publish/_constants'
import { LoggerInstance, ProviderInstance } from '@oceanprotocol/lib'
import { FormPublishData } from 'src/components/Publish/_types'
export default function CustomProvider(props: InputProps): ReactElement {
const [field, meta, helpers] = useField(props.name)
const [isLoading, setIsLoading] = useState(false)
const { setFieldError } = useFormikContext<FormPublishData>()
async function handleValidation(e: React.SyntheticEvent) {
e.preventDefault()
try {
setIsLoading(true)
const isValid = await ProviderInstance.isValidProvider(field.value.url)
// error if something's not right from response
// No way to detect a failed request with ProviderInstance.isValidProvider,
// making this error show up for multiple cases it shouldn't, like network
// down.
if (!isValid)
throw Error(
'✗ No valid provider detected. Check your network, your URL and try again.'
)
// if all good, add provider to formik state
helpers.setValue({ url: field.value.url, valid: isValid })
} catch (error) {
setFieldError(`${field.name}.url`, error.message)
LoggerInstance.error(error.message)
} finally {
setIsLoading(false)
}
}
function handleFileInfoClose() {
helpers.setValue({ url: '', valid: false })
helpers.setTouched(false)
}
function handleDefault(e: React.SyntheticEvent) {
e.preventDefault()
helpers.setValue(initialValues.services[0].providerUrl)
}
return field?.value?.valid === true ? (
<FileInfo file={field.value} handleClose={handleFileInfoClose} />
) : (
<>
<UrlInput
submitText="Validate"
{...props}
name={`${field.name}.url`}
isLoading={isLoading}
handleButtonClick={handleValidation}
/>
<Button
style="text"
size="small"
onClick={handleDefault}
className={styles.default}
>
Use Default Provider
</Button>
</>
)
}