mirror of
https://github.com/OpenNebula/one.git
synced 2026-12-02 13:16:48 +09:00
F OpenNebula/one#5422: Fix providers form
This commit is contained in:
9 files changed
+109
-67
No files matched your search
@@ -0,0 +1,77 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
|
||||
* *
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
|
||||
* not use this file except in compliance with the License. You may obtain *
|
||||
* a copy of the License at *
|
||||
* *
|
||||
* http://www.apache.org/licenses/LICENSE-2.0 *
|
||||
* *
|
||||
* Unless required by applicable law or agreed to in writing, software *
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, *
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
|
||||
* See the License for the specific language governing permissions and *
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { Redirect, useParams } from 'react-router'
|
||||
|
||||
import { Container, LinearProgress } from '@material-ui/core'
|
||||
|
||||
import { useFetchAll } from 'client/hooks'
|
||||
import { useProviderApi } from 'client/features/One'
|
||||
import ProviderForm from 'client/containers/Providers/Form/ProviderForm'
|
||||
import * as ProviderTemplateModel from 'client/models/ProviderTemplate'
|
||||
import { PATH } from 'client/apps/provision/routes'
|
||||
|
||||
function ProviderCreateForm () {
|
||||
const { id } = useParams()
|
||||
const [initialValues, setInitialValues] = useState(null)
|
||||
|
||||
const { getProvider, getProviderConnection } = useProviderApi()
|
||||
const { data: preloadedData, fetchRequestAll, loading, error } = useFetchAll()
|
||||
|
||||
useEffect(() => {
|
||||
const preloadFetchData = async () => {
|
||||
const data = await fetchRequestAll([
|
||||
getProvider(id),
|
||||
getProviderConnection(id)
|
||||
])
|
||||
|
||||
if (data) {
|
||||
const [provider = {}, connection = []] = data
|
||||
|
||||
const {
|
||||
PLAIN = {},
|
||||
PROVISION_BODY: { description, ...currentBodyTemplate }
|
||||
} = provider?.TEMPLATE
|
||||
|
||||
const connectionEditable = ProviderTemplateModel
|
||||
.getConnectionEditable({ plain: PLAIN, connection })
|
||||
|
||||
setInitialValues({
|
||||
template: [currentBodyTemplate],
|
||||
connection: connectionEditable,
|
||||
configuration: { description }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
id && preloadFetchData()
|
||||
}, [])
|
||||
|
||||
if (error) {
|
||||
return <Redirect to={PATH.PROVIDERS.LIST} />
|
||||
}
|
||||
|
||||
return (id && !initialValues) || loading ? (
|
||||
<LinearProgress color='secondary' />
|
||||
) : (
|
||||
<Container style={{ display: 'flex', flexFlow: 'column' }} disableGutters>
|
||||
<ProviderForm {...{ id, preloadedData, initialValues }} />
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
export default ProviderCreateForm
|
||||
+1
-1
@@ -21,7 +21,7 @@ import { T } from 'client/constants'
|
||||
|
||||
import {
|
||||
FORM_FIELDS, STEP_FORM_SCHEMA
|
||||
} from 'client/containers/Providers/Form/Create/Steps/BasicConfiguration/schema'
|
||||
} from 'client/containers/Providers/Form/ProviderForm/Steps/BasicConfiguration/schema'
|
||||
|
||||
export const STEP_ID = 'configuration'
|
||||
|
||||
File renamed without changes.
+10
-7
@@ -25,11 +25,11 @@ import * as ProviderTemplateModel from 'client/models/ProviderTemplate'
|
||||
|
||||
import {
|
||||
FORM_FIELDS, STEP_FORM_SCHEMA
|
||||
} from 'client/containers/Providers/Form/Create/Steps/Connection/schema'
|
||||
} from 'client/containers/Providers/Form/ProviderForm/Steps/Connection/schema'
|
||||
|
||||
import {
|
||||
STEP_ID as TEMPLATE_ID
|
||||
} from 'client/containers/Providers/Form/Create/Steps/Template'
|
||||
} from 'client/containers/Providers/Form/ProviderForm/Steps/Template'
|
||||
|
||||
export const STEP_ID = 'connection'
|
||||
|
||||
@@ -46,9 +46,12 @@ const Connection = ({ isUpdate }) => ({
|
||||
const { watch } = useFormContext()
|
||||
|
||||
useEffect(() => {
|
||||
const { [TEMPLATE_ID]: templateSelected, [STEP_ID]: currentConnection } = watch()
|
||||
const {
|
||||
[TEMPLATE_ID]: templateSelected,
|
||||
[STEP_ID]: currentConnection = {}
|
||||
} = watch()
|
||||
|
||||
const { provider, ...template } = templateSelected?.[0]
|
||||
const { provider, ...template } = templateSelected?.[0] ?? {}
|
||||
|
||||
providerType = provider
|
||||
|
||||
@@ -56,11 +59,11 @@ const Connection = ({ isUpdate }) => ({
|
||||
// when is updating, connections have the name as input label
|
||||
? Object.keys(currentConnection)
|
||||
.reduce((res, name) => ({ ...res, [name]: capitalize(name) }), {})
|
||||
// set connections from template, to take value as input labels
|
||||
// set connections from template, to take values as input labels
|
||||
: ProviderTemplateModel.getConnectionEditable(template)
|
||||
|
||||
setFields(FORM_FIELDS({ connection, providerType }))
|
||||
}, [data])
|
||||
}, [])
|
||||
|
||||
return (fields?.length === 0) ? (
|
||||
<EmptyCard title={"There aren't connections to fill"} />
|
||||
@@ -70,5 +73,5 @@ const Connection = ({ isUpdate }) => ({
|
||||
}, [])
|
||||
})
|
||||
|
||||
export * from 'client/containers/Providers/Form/Create/Steps/Connection/schema'
|
||||
export * from 'client/containers/Providers/Form/ProviderForm/Steps/Connection/schema'
|
||||
export default Connection
|
||||
File renamed without changes.
+3
-3
@@ -27,10 +27,10 @@ import { sanitize, groupBy } from 'client/utils'
|
||||
import * as ProviderTemplateModel from 'client/models/ProviderTemplate'
|
||||
import { T, PROVISIONS_TYPES, PROVIDERS_TYPES } from 'client/constants'
|
||||
|
||||
import { STEP_FORM_SCHEMA } from 'client/containers/Providers/Form/Create/Steps/Template/schema'
|
||||
import { STEP_FORM_SCHEMA } from 'client/containers/Providers/Form/ProviderForm/Steps/Template/schema'
|
||||
|
||||
import { STEP_ID as CONFIGURATION_ID } from 'client/containers/Providers/Form/Create/Steps/BasicConfiguration'
|
||||
import { STEP_ID as CONNECTION_ID } from 'client/containers/Providers/Form/Create/Steps/Connection'
|
||||
import { STEP_ID as CONFIGURATION_ID } from 'client/containers/Providers/Form/ProviderForm/Steps/BasicConfiguration'
|
||||
import { STEP_ID as CONNECTION_ID } from 'client/containers/Providers/Form/ProviderForm/Steps/Connection'
|
||||
|
||||
export const STEP_ID = 'template'
|
||||
|
||||
File renamed without changes.
File renamed without changes.
+18
-56
@@ -14,42 +14,33 @@
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import React, { useEffect } from 'react'
|
||||
import { Redirect, useHistory, useParams } from 'react-router'
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { useHistory } from 'react-router'
|
||||
|
||||
import { Container, LinearProgress } from '@material-ui/core'
|
||||
import { useForm, FormProvider } from 'react-hook-form'
|
||||
import { yupResolver } from '@hookform/resolvers'
|
||||
|
||||
import FormStepper from 'client/components/FormStepper'
|
||||
import Steps from 'client/containers/Providers/Form/Create/Steps'
|
||||
import Steps from 'client/containers/Providers/Form/ProviderForm/Steps'
|
||||
|
||||
import { useFetchAll } from 'client/hooks'
|
||||
import { useProviderApi } from 'client/features/One'
|
||||
import { useGeneralApi } from 'client/features/General'
|
||||
import * as ProviderTemplateModel from 'client/models/ProviderTemplate'
|
||||
import { PATH } from 'client/apps/provision/routes'
|
||||
|
||||
function ProviderCreateForm () {
|
||||
const ProviderCreateForm = ({ id, preloadedData, initialValues }) => {
|
||||
const history = useHistory()
|
||||
const { id } = useParams()
|
||||
const isUpdate = id !== undefined
|
||||
|
||||
const {
|
||||
getProvider,
|
||||
createProvider,
|
||||
updateProvider,
|
||||
getProviderConnection
|
||||
} = useProviderApi()
|
||||
|
||||
const { createProvider, updateProvider } = useProviderApi()
|
||||
const { enqueueError, enqueueSuccess, changeLoading } = useGeneralApi()
|
||||
|
||||
const { data, fetchRequestAll, loading, error } = useFetchAll()
|
||||
const { steps, defaultValues, resolvers } = Steps({ isUpdate })
|
||||
|
||||
const methods = useForm({
|
||||
mode: 'onSubmit',
|
||||
defaultValues,
|
||||
defaultValues: initialValues ?? defaultValues,
|
||||
resolver: yupResolver(resolvers())
|
||||
})
|
||||
|
||||
@@ -88,7 +79,7 @@ function ProviderCreateForm () {
|
||||
const callUpdateProvider = formData => {
|
||||
const { configuration, connection: connectionEditable } = formData
|
||||
const { description } = configuration
|
||||
const [provider = {}, connection = []] = data
|
||||
const [provider = {}, connection = []] = preloadedData
|
||||
|
||||
const { PROVISION_BODY: currentBodyTemplate } = provider?.TEMPLATE
|
||||
|
||||
@@ -108,46 +99,17 @@ function ProviderCreateForm () {
|
||||
isUpdate ? callUpdateProvider(formData) : callCreateProvider(formData)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
isUpdate && fetchRequestAll([
|
||||
getProvider(id),
|
||||
getProviderConnection(id)
|
||||
])
|
||||
}, [isUpdate])
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
const [provider = {}, connection = []] = data
|
||||
|
||||
const {
|
||||
PLAIN = {},
|
||||
PROVISION_BODY: { description, ...currentBodyTemplate }
|
||||
} = provider?.TEMPLATE
|
||||
|
||||
const connectionEditable = ProviderTemplateModel
|
||||
.getConnectionEditable({ plain: PLAIN, connection })
|
||||
|
||||
methods.reset({
|
||||
template: [currentBodyTemplate],
|
||||
connection: connectionEditable,
|
||||
configuration: { description }
|
||||
}, { errors: false })
|
||||
}
|
||||
}, [data])
|
||||
|
||||
if (error) {
|
||||
return <Redirect to={PATH.PROVIDERS.LIST} />
|
||||
}
|
||||
|
||||
return (isUpdate && !data) || loading ? (
|
||||
<LinearProgress color='secondary' />
|
||||
) : (
|
||||
<Container style={{ display: 'flex', flexFlow: 'column' }} disableGutters>
|
||||
<FormProvider {...methods}>
|
||||
<FormStepper steps={steps} schema={resolvers} onSubmit={onSubmit} />
|
||||
</FormProvider>
|
||||
</Container>
|
||||
return (
|
||||
<FormProvider {...methods}>
|
||||
<FormStepper steps={steps} schema={resolvers} onSubmit={onSubmit} />
|
||||
</FormProvider>
|
||||
)
|
||||
}
|
||||
|
||||
ProviderCreateForm.propTypes = {
|
||||
id: PropTypes.string,
|
||||
preloadedData: PropTypes.object,
|
||||
initialValues: PropTypes.object
|
||||
}
|
||||
|
||||
export default ProviderCreateForm
|
||||
Reference in new issue
Block a user