mirror of
https://github.com/OpenNebula/one.git
synced 2026-12-04 22:21:36 +09:00
F OpenNebula/one#3951: Add provisions form (#429)
This commit is contained in:
1 parent
494129f7e7
commit
8009b17481
24 files changed
+483
-64
No files matched your search
@@ -102,6 +102,10 @@ module.exports = {
|
||||
type: SUCCESS_ONE_REQUEST,
|
||||
payload: { providers }
|
||||
}),
|
||||
setProvisionsTemplates: provisionsTemplates => ({
|
||||
type: SUCCESS_ONE_REQUEST,
|
||||
payload: { provisionsTemplates }
|
||||
}),
|
||||
setProvisions: provisions => ({
|
||||
type: SUCCESS_ONE_REQUEST,
|
||||
payload: { provisions }
|
||||
|
||||
@@ -2,6 +2,7 @@ import React, { memo } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
import { Card, CardHeader, Fade, makeStyles } from '@material-ui/core'
|
||||
import { Tr } from 'client/components/HOC/Translate'
|
||||
|
||||
const useStyles = makeStyles(theme => ({
|
||||
root: {
|
||||
@@ -15,14 +16,14 @@ const useStyles = makeStyles(theme => ({
|
||||
}
|
||||
}))
|
||||
|
||||
const EmptyCard = memo(({ name }) => {
|
||||
const EmptyCard = memo(({ title }) => {
|
||||
const classes = useStyles()
|
||||
|
||||
return (
|
||||
<Fade in unmountOnExit>
|
||||
<Card className={classes.root} variant="outlined">
|
||||
<CardHeader
|
||||
subheader={`Your ${name ?? ''} list is empty`}
|
||||
subheader={title ?? Tr('empty')}
|
||||
className={classes.content}
|
||||
/>
|
||||
</Card>
|
||||
@@ -31,11 +32,11 @@ const EmptyCard = memo(({ name }) => {
|
||||
})
|
||||
|
||||
EmptyCard.propTypes = {
|
||||
name: PropTypes.string
|
||||
title: PropTypes.string
|
||||
}
|
||||
|
||||
EmptyCard.defaultProps = {
|
||||
name: undefined
|
||||
title: undefined
|
||||
}
|
||||
|
||||
EmptyCard.displayName = 'EmptyCard'
|
||||
|
||||
@@ -68,7 +68,7 @@ const ListCards = memo(({
|
||||
) : (
|
||||
(displayEmpty || EmptyComponent) && (
|
||||
<Grid item {...breakpoints}>
|
||||
{EmptyComponent ?? <EmptyCard />}
|
||||
{EmptyComponent ?? <EmptyCard title={'Your list is empty'} />}
|
||||
</Grid>
|
||||
)
|
||||
)}
|
||||
|
||||
@@ -49,7 +49,7 @@ function ApplicationsInstances () {
|
||||
<ListCards
|
||||
list={list}
|
||||
isLoading={list.length === 0 && loading}
|
||||
EmptyComponent={<EmptyCard name={'applications instances'} />}
|
||||
EmptyComponent={<EmptyCard title={'Your applications instances list is empty'} />}
|
||||
CardComponent={ApplicationCard}
|
||||
cardsProps={({ value }) => ({
|
||||
handleShow: () => setShowDialog(value)
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ const Clusters = () => ({
|
||||
<ListCards
|
||||
list={clusters}
|
||||
isLoading={clusters.length === 0 && loading}
|
||||
EmptyComponent={<EmptyCard name={'clusters'} />}
|
||||
EmptyComponent={<EmptyCard title={'Your clusters list is empty'} />}
|
||||
CardComponent={ClusterCard}
|
||||
cardsProps={({ value: { ID } }) => {
|
||||
const isSelected = data?.some(selected => selected === ID)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import React, { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
import { useFormContext } from 'react-hook-form'
|
||||
|
||||
import useProvision from 'client/hooks/useProvision'
|
||||
import FormWithSchema from 'client/components/Forms/FormWithSchema'
|
||||
|
||||
import { STEP_ID as PROVIDER_ID } from 'client/containers/Providers/Form/Create/Steps/Provider'
|
||||
import { FORM_FIELDS, STEP_FORM_SCHEMA } from './schema'
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ const Locations = () => ({
|
||||
return (
|
||||
<ListCards
|
||||
list={locationsList}
|
||||
EmptyComponent={<EmptyCard name={'locations'} />}
|
||||
EmptyComponent={<EmptyCard title={'Your locations list is empty'} />}
|
||||
CardComponent={LocationCard}
|
||||
cardsProps={({ value: { key } }) => {
|
||||
const isSelected = data?.some(selected => selected === key)
|
||||
|
||||
@@ -47,7 +47,7 @@ const Provider = () => ({
|
||||
<ListCards
|
||||
list={templates}
|
||||
isLoading={!templates || loading}
|
||||
EmptyComponent={<EmptyCard name={'providers templates'} />}
|
||||
EmptyComponent={<EmptyCard title={'Your providers templates list is empty'} />}
|
||||
CardComponent={SelectCard}
|
||||
cardsProps={({ value: { name } }) => {
|
||||
const isSelected = data?.some(selected => selected === name)
|
||||
|
||||
@@ -22,12 +22,17 @@ function Providers () {
|
||||
const {
|
||||
error,
|
||||
fetchRequest,
|
||||
cancelRequest,
|
||||
loading,
|
||||
reloading
|
||||
} = useFetch(getProviders)
|
||||
|
||||
useEffect(() => {
|
||||
fetchRequest()
|
||||
|
||||
return () => {
|
||||
cancelRequest()
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import React, { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
import { useFormContext } from 'react-hook-form'
|
||||
import { LinearProgress } from '@material-ui/core'
|
||||
|
||||
import FormWithSchema from 'client/components/Forms/FormWithSchema'
|
||||
import useFetch from 'client/hooks/useFetch'
|
||||
import useProvision from 'client/hooks/useProvision'
|
||||
import { EmptyCard } from 'client/components/Cards'
|
||||
|
||||
import {
|
||||
STEP_ID as PROVISION_TEMPLATE_ID
|
||||
} from 'client/containers/Provisions/Form/Create/Steps/ProvisionTemplate'
|
||||
import { FORM_FIELDS, STEP_FORM_SCHEMA } from './schema'
|
||||
|
||||
export const STEP_ID = 'inputs'
|
||||
|
||||
let inputs = []
|
||||
|
||||
const Inputs = () => ({
|
||||
id: STEP_ID,
|
||||
label: 'Inputs configuration',
|
||||
resolver: () => STEP_FORM_SCHEMA(inputs),
|
||||
optionsValidate: { abortEarly: false },
|
||||
content: useCallback(() => {
|
||||
const [fields, setFields] = useState(undefined)
|
||||
const { watch, reset } = useFormContext()
|
||||
const { getProvisionTemplate } = useProvision()
|
||||
const { data, fetchRequest, cancelRequest, loading } = useFetch(getProvisionTemplate)
|
||||
|
||||
useEffect(() => {
|
||||
const {
|
||||
[PROVISION_TEMPLATE_ID]: provisionTemplate,
|
||||
[STEP_ID]: currentInputs
|
||||
} = watch()
|
||||
|
||||
!currentInputs
|
||||
? fetchRequest({ id: provisionTemplate[0] })
|
||||
: setFields(FORM_FIELDS(inputs))
|
||||
|
||||
return () => {
|
||||
cancelRequest()
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
inputs = data.TEMPLATE.PROVISION_BODY.inputs ?? []
|
||||
setFields(FORM_FIELDS(inputs))
|
||||
reset({ ...watch(), [STEP_ID]: STEP_FORM_SCHEMA(inputs).default() })
|
||||
}
|
||||
}, [data])
|
||||
|
||||
if (!fields && loading) {
|
||||
return <LinearProgress />
|
||||
}
|
||||
|
||||
return (fields?.length === 0) ? (
|
||||
<EmptyCard title={'✔️ There is not inputs to fill'} />
|
||||
) : (
|
||||
<form>
|
||||
<FormWithSchema cy="form-provider" fields={fields ?? []} id={STEP_ID} />
|
||||
</form>
|
||||
)
|
||||
}, [])
|
||||
})
|
||||
|
||||
export default Inputs
|
||||
@@ -0,0 +1,31 @@
|
||||
import * as yup from 'yup'
|
||||
import { getValidationFromFields, schemaUserInput } from 'client/utils'
|
||||
|
||||
export const FORM_FIELDS = inputs =>
|
||||
inputs?.map(({
|
||||
name,
|
||||
description,
|
||||
type,
|
||||
default: defaultValue,
|
||||
min_value: min,
|
||||
max_value: max,
|
||||
options
|
||||
}) => {
|
||||
const optionsValue = options?.join(',') ?? `${min}..${max}`
|
||||
|
||||
return {
|
||||
name,
|
||||
label: `${description ?? name} *`,
|
||||
...schemaUserInput({
|
||||
mandatory: true,
|
||||
name,
|
||||
type,
|
||||
options: optionsValue,
|
||||
defaultValue
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export const STEP_FORM_SCHEMA = connection => yup.object(
|
||||
getValidationFromFields(FORM_FIELDS(connection))
|
||||
)
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
import React, { useCallback, useEffect } from 'react'
|
||||
|
||||
import { Redirect } from 'react-router-dom'
|
||||
import ProvisionTemplateIcon from '@material-ui/icons/Telegram'
|
||||
|
||||
import useFetch from 'client/hooks/useFetch'
|
||||
import useProvision from 'client/hooks/useProvision'
|
||||
import useListForm from 'client/hooks/useListForm'
|
||||
|
||||
import ListCards from 'client/components/List/ListCards'
|
||||
import { EmptyCard, SelectCard } from 'client/components/Cards'
|
||||
import { PATH } from 'client/router/provision'
|
||||
|
||||
import { STEP_ID as INPUTS_ID } from 'client/containers/Provisions/Form/Create/Steps/Inputs'
|
||||
import { STEP_FORM_SCHEMA } from './schema'
|
||||
|
||||
export const STEP_ID = 'provisionTemplate'
|
||||
|
||||
const ProvisionTemplate = () => ({
|
||||
id: STEP_ID,
|
||||
label: 'Provision template',
|
||||
resolver: () => STEP_FORM_SCHEMA,
|
||||
content: useCallback(({ data, setFormData }) => {
|
||||
const { provisionsTemplates, getProvisionsTemplates } = useProvision()
|
||||
const { fetchRequest, cancelRequest, loading, error } = useFetch(getProvisionsTemplates)
|
||||
|
||||
const { handleSelect, handleUnselect } = useListForm({
|
||||
key: STEP_ID,
|
||||
setList: setFormData
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
data?.length === 0 && fetchRequest()
|
||||
|
||||
return () => {
|
||||
cancelRequest()
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleClick = (nameTemplate, isSelected) => {
|
||||
setFormData(prev => ({ ...prev, [INPUTS_ID]: undefined }))
|
||||
isSelected ? handleUnselect(nameTemplate) : handleSelect(nameTemplate)
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <Redirect to={PATH.DASHBOARD} />
|
||||
}
|
||||
|
||||
return (
|
||||
<ListCards
|
||||
list={provisionsTemplates}
|
||||
isLoading={provisionsTemplates.length === 0 && loading}
|
||||
EmptyComponent={<EmptyCard title={'Your provisions templates list is empty'} />}
|
||||
CardComponent={SelectCard}
|
||||
cardsProps={({ value: { ID, NAME } }) => {
|
||||
const isSelected = data?.some(selected => selected === ID)
|
||||
|
||||
return {
|
||||
icon: <ProvisionTemplateIcon />,
|
||||
title: `${ID} - ${NAME}`,
|
||||
isSelected,
|
||||
handleClick: () => handleClick(ID, isSelected)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}, [])
|
||||
})
|
||||
|
||||
export default ProvisionTemplate
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import * as yup from 'yup'
|
||||
|
||||
export const STEP_FORM_SCHEMA = yup
|
||||
.array(yup.string().trim())
|
||||
.min(1, 'Select provision template')
|
||||
.max(1, 'Max. one template selected')
|
||||
.required('Provision template field is required')
|
||||
.default([])
|
||||
@@ -0,0 +1,23 @@
|
||||
import * as yup from 'yup'
|
||||
|
||||
import ProvisionTemplate from './ProvisionTemplate'
|
||||
import Inputs from './Inputs'
|
||||
|
||||
const Steps = () => {
|
||||
const provisionTemplate = ProvisionTemplate()
|
||||
const inputs = Inputs()
|
||||
|
||||
const steps = [provisionTemplate, inputs]
|
||||
|
||||
const resolvers = () => yup
|
||||
.object({
|
||||
[provisionTemplate.id]: provisionTemplate.resolver(),
|
||||
[inputs.id]: inputs.resolver()
|
||||
})
|
||||
|
||||
const defaultValues = resolvers().default()
|
||||
|
||||
return { steps, defaultValues, resolvers }
|
||||
}
|
||||
|
||||
export default Steps
|
||||
@@ -1,23 +1,36 @@
|
||||
import React from 'react'
|
||||
import { useHistory } from 'react-router'
|
||||
|
||||
import { Container, Button } from '@material-ui/core'
|
||||
import { Container } from '@material-ui/core'
|
||||
import { useForm, FormProvider } from 'react-hook-form'
|
||||
import { yupResolver } from '@hookform/resolvers'
|
||||
|
||||
import useProvision from 'client/hooks/useProvision'
|
||||
import FormStepper from 'client/components/FormStepper'
|
||||
import Steps from 'client/containers/Provisions/Form/Create/Steps'
|
||||
import { PATH } from 'client/router/provision'
|
||||
|
||||
function ProvisionCreateForm () {
|
||||
const history = useHistory()
|
||||
const { createProvision } = useProvision()
|
||||
const { steps, defaultValues, resolvers } = Steps()
|
||||
|
||||
const methods = useForm({
|
||||
mode: 'onSubmit',
|
||||
defaultValues,
|
||||
resolver: yupResolver(resolvers())
|
||||
})
|
||||
|
||||
const onSubmit = data => {
|
||||
createProvision({ data })
|
||||
.then(() => history.push(PATH.PROVISIONS.LIST))
|
||||
}
|
||||
|
||||
return (
|
||||
<Container disableGutters>
|
||||
<h1>{'Create provision form'}</h1>
|
||||
<Button
|
||||
variant="contained"
|
||||
color="primary"
|
||||
onClick={() => history.push(PATH.PROVISIONS.LIST)}
|
||||
>
|
||||
{'⬅️ Back to list provision'}
|
||||
</Button>
|
||||
<Container style={{ display: 'flex', flexFlow: 'column' }} disableGutters>
|
||||
<FormProvider {...methods}>
|
||||
<FormStepper steps={steps} schema={resolvers} onSubmit={onSubmit} />
|
||||
</FormProvider>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import AlertError from 'client/components/Alerts/Error'
|
||||
import { ProviderCard } from 'client/components/Cards'
|
||||
|
||||
import { Tr } from 'client/components/HOC'
|
||||
import { Providers as ProvidersLabel } from 'client/constants/translates'
|
||||
import { Provisions as ProvisionsLabel } from 'client/constants/translates'
|
||||
|
||||
function Provisions () {
|
||||
const history = useHistory()
|
||||
@@ -22,12 +22,17 @@ function Provisions () {
|
||||
const {
|
||||
error,
|
||||
fetchRequest,
|
||||
cancelRequest,
|
||||
loading,
|
||||
reloading
|
||||
} = useFetch(getProvisions)
|
||||
|
||||
useEffect(() => {
|
||||
fetchRequest()
|
||||
|
||||
return () => {
|
||||
cancelRequest()
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
@@ -40,7 +45,7 @@ function Provisions () {
|
||||
label={<RefreshIcon />}
|
||||
/>
|
||||
<Typography variant="h5" style={{ marginLeft: 8 }}>
|
||||
{Tr(ProvidersLabel)}
|
||||
{Tr(ProvisionsLabel)}
|
||||
</Typography>
|
||||
</Box>
|
||||
<Divider />
|
||||
|
||||
@@ -1,33 +1,39 @@
|
||||
import { useState, useCallback } from 'react'
|
||||
import axios from 'axios'
|
||||
|
||||
const useRequest = request => {
|
||||
const [data, setData] = useState(undefined)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [reloading, setReloading] = useState(false)
|
||||
const [error, setError] = useState(false)
|
||||
|
||||
const callRequest = useCallback(
|
||||
payload =>
|
||||
request(payload)
|
||||
.then(response => {
|
||||
if (response !== undefined) {
|
||||
setData(response)
|
||||
setError(false)
|
||||
} else setError(true)
|
||||
}
|
||||
)
|
||||
.finally(() => {
|
||||
setLoading(false)
|
||||
setReloading(false)
|
||||
}),
|
||||
[request]
|
||||
)
|
||||
const source = axios.CancelToken.source()
|
||||
|
||||
const fetchRequest = useCallback((payload, reload = false) => {
|
||||
reload ? setReloading(true) : setLoading(true)
|
||||
callRequest(payload)
|
||||
}, [])
|
||||
|
||||
return { data, fetchRequest, loading, reloading, error }
|
||||
request({
|
||||
...payload,
|
||||
config: { cancelToken: source.token }
|
||||
}).then(response => {
|
||||
if (!axios.isCancel(response)) {
|
||||
if (response !== undefined) {
|
||||
setData(response)
|
||||
setError(false)
|
||||
} else setError(true)
|
||||
|
||||
setLoading(false)
|
||||
setReloading(false)
|
||||
}
|
||||
})
|
||||
}, [source])
|
||||
|
||||
return {
|
||||
data,
|
||||
fetchRequest,
|
||||
cancelRequest: source.cancel,
|
||||
loading,
|
||||
reloading,
|
||||
error
|
||||
}
|
||||
}
|
||||
export default useRequest
|
||||
@@ -1,7 +1,12 @@
|
||||
import { useCallback } from 'react'
|
||||
import { useSelector, useDispatch, shallowEqual } from 'react-redux'
|
||||
|
||||
import { setProvidersTemplates, setProviders, setProvisions } from 'client/actions/pool'
|
||||
import {
|
||||
setProvidersTemplates,
|
||||
setProviders,
|
||||
setProvisionsTemplates,
|
||||
setProvisions
|
||||
} from 'client/actions/pool'
|
||||
|
||||
import { enqueueError, enqueueSuccess } from 'client/actions/general'
|
||||
|
||||
@@ -12,6 +17,7 @@ export default function useOpennebula () {
|
||||
const {
|
||||
providersTemplates,
|
||||
providers,
|
||||
provisionsTemplates,
|
||||
provisions,
|
||||
filterPool: filter
|
||||
} = useSelector(
|
||||
@@ -22,6 +28,10 @@ export default function useOpennebula () {
|
||||
shallowEqual
|
||||
)
|
||||
|
||||
// --------------------------------------------
|
||||
// PROVIDERS REQUESTS
|
||||
// --------------------------------------------
|
||||
|
||||
const getProvidersTemplates = useCallback(
|
||||
({ end, start } = { end: -1, start: -1 }) =>
|
||||
serviceProvision
|
||||
@@ -54,6 +64,7 @@ export default function useOpennebula () {
|
||||
})
|
||||
.catch(err => {
|
||||
dispatch(enqueueError(err ?? 'Error GET providers'))
|
||||
return err
|
||||
}),
|
||||
[dispatch, filter]
|
||||
)
|
||||
@@ -71,6 +82,36 @@ export default function useOpennebula () {
|
||||
[dispatch, providers]
|
||||
)
|
||||
|
||||
// --------------------------------------------
|
||||
// PROVISIONS TEMPLATES REQUESTS
|
||||
// --------------------------------------------
|
||||
|
||||
const getProvisionTemplate = useCallback(
|
||||
({ id }) =>
|
||||
serviceProvision.getProvisionTemplate({ id }).catch(err => {
|
||||
dispatch(enqueueError(err ?? `Error GET (${id}) provision template`))
|
||||
}),
|
||||
[dispatch]
|
||||
)
|
||||
|
||||
const getProvisionsTemplates = useCallback(
|
||||
({ end, start } = { end: -1, start: -1 }) =>
|
||||
serviceProvision
|
||||
.getProvisionsTemplates({ filter, end, start })
|
||||
.then(doc => {
|
||||
dispatch(setProvisionsTemplates(doc))
|
||||
return doc
|
||||
})
|
||||
.catch(err => {
|
||||
dispatch(enqueueError(err ?? 'Error GET provisions templates'))
|
||||
}),
|
||||
[dispatch, filter]
|
||||
)
|
||||
|
||||
// --------------------------------------------
|
||||
// PROVISIONS REQUESTS
|
||||
// --------------------------------------------
|
||||
|
||||
const getProvision = useCallback(
|
||||
({ id }) =>
|
||||
serviceProvision.getProvision({ id }).catch(err => {
|
||||
@@ -80,15 +121,16 @@ export default function useOpennebula () {
|
||||
)
|
||||
|
||||
const getProvisions = useCallback(
|
||||
({ end, start } = { end: -1, start: -1 }) =>
|
||||
({ end, start, config } = { end: -1, start: -1, config: {} }) =>
|
||||
serviceProvision
|
||||
.getProvisions({ filter, end, start })
|
||||
.getProvisions({ filter, end, start, config })
|
||||
.then(doc => {
|
||||
dispatch(setProvisions(doc))
|
||||
return doc
|
||||
})
|
||||
.catch(err => {
|
||||
dispatch(enqueueError(err ?? 'Error GET providers'))
|
||||
dispatch(enqueueError(err?.message ?? 'Error GET provisions'))
|
||||
return err
|
||||
}),
|
||||
[dispatch, filter]
|
||||
)
|
||||
@@ -97,15 +139,9 @@ export default function useOpennebula () {
|
||||
({ data }) =>
|
||||
serviceProvision
|
||||
.createProvision({ data })
|
||||
.then(doc => {
|
||||
/* dispatch(
|
||||
setApplicationsTemplates(
|
||||
filterBy([doc].concat(applicationsTemplates), 'ID')
|
||||
)
|
||||
) */
|
||||
|
||||
return dispatch(enqueueSuccess(`Template created - ID: ${doc?.ID}`))
|
||||
})
|
||||
.then(doc => dispatch(
|
||||
enqueueSuccess(`Provision created - ID: ${doc?.ID}`))
|
||||
)
|
||||
.catch(err => {
|
||||
dispatch(
|
||||
enqueueError(err?.message ?? 'Error CREATE Provision')
|
||||
@@ -123,6 +159,10 @@ export default function useOpennebula () {
|
||||
getProviders,
|
||||
createProvider,
|
||||
|
||||
provisionsTemplates,
|
||||
getProvisionTemplate,
|
||||
getProvisionsTemplates,
|
||||
|
||||
provisions,
|
||||
getProvision,
|
||||
getProvisions,
|
||||
|
||||
@@ -40,6 +40,7 @@ const initial = {
|
||||
acl: [],
|
||||
providersTemplates: [],
|
||||
providers: [],
|
||||
provisionsTemplates: [],
|
||||
provisions: []
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import axios from 'axios'
|
||||
import httpCodes from 'server/utils/constants/http-codes'
|
||||
import { httpMethod } from 'server/utils/constants/defaults'
|
||||
import {
|
||||
@@ -11,7 +12,7 @@ import { REQUEST_ACTIONS } from 'client/constants'
|
||||
const { GET, POST, PUT } = httpMethod
|
||||
|
||||
// --------------------------------------------
|
||||
// PROVIDER REQUESTS
|
||||
// PROVIDERS REQUESTS
|
||||
// --------------------------------------------
|
||||
|
||||
export const getProvidersTemplates = ({ filter }) =>
|
||||
@@ -41,7 +42,7 @@ export const getProviders = ({ filter }) =>
|
||||
method: GET,
|
||||
error: err => err?.message
|
||||
}).then(res => {
|
||||
if (!res?.id || res?.id !== httpCodes.ok.id) throw res
|
||||
if (axios.isCancel(res) || !res?.id || res?.id !== httpCodes.ok.id) throw res
|
||||
|
||||
return [res?.data?.DOCUMENT_POOL?.DOCUMENT ?? []].flat()
|
||||
})
|
||||
@@ -57,7 +58,35 @@ export const createProvider = ({ data = {} }) =>
|
||||
})
|
||||
|
||||
// --------------------------------------------
|
||||
// PROVISION REQUESTS
|
||||
// PROVISIONS TEMPLATES REQUESTS
|
||||
// --------------------------------------------
|
||||
|
||||
export const getProvisionTemplate = ({ id }) =>
|
||||
requestData(`/api/${PROVISION_TEMPLATE}/list/${id}`, {
|
||||
method: GET,
|
||||
error: err => err?.message
|
||||
}).then(res => {
|
||||
if (!res?.id || res?.id !== httpCodes.ok.id) throw res
|
||||
|
||||
return res?.data?.DOCUMENT ?? {}
|
||||
})
|
||||
|
||||
export const getProvisionsTemplates = ({ filter }) =>
|
||||
requestData(`/api/${PROVISION_TEMPLATE}/list`, {
|
||||
data: { filter },
|
||||
method: GET,
|
||||
error: err => err?.message
|
||||
}).then(res => {
|
||||
if (!res?.id || res?.id !== httpCodes.ok.id) throw res
|
||||
|
||||
return [res?.data?.DOCUMENT_POOL?.DOCUMENT ?? []].flat()
|
||||
})
|
||||
|
||||
export const createProvisionTemplate = ({ data = {} }) =>
|
||||
Promise.resolve().then(res => res?.data?.DOCUMENT ?? {})
|
||||
|
||||
// --------------------------------------------
|
||||
// PROVISIONS REQUESTS
|
||||
// --------------------------------------------
|
||||
|
||||
export const getProvision = ({ id }) =>
|
||||
@@ -70,13 +99,13 @@ export const getProvision = ({ id }) =>
|
||||
return res?.data?.DOCUMENT ?? {}
|
||||
})
|
||||
|
||||
export const getProvisions = ({ filter }) =>
|
||||
export const getProvisions = ({ filter, config }) =>
|
||||
requestData(`/api/${PROVISION}/list`, {
|
||||
config,
|
||||
data: { filter },
|
||||
method: GET,
|
||||
error: err => err?.message
|
||||
method: GET
|
||||
}).then(res => {
|
||||
if (!res?.id || res?.id !== httpCodes.ok.id) throw res
|
||||
if (axios.isCancel(res) || !res?.id || res?.id !== httpCodes.ok.id) throw res
|
||||
|
||||
return [res?.data?.DOCUMENT_POOL?.DOCUMENT ?? []].flat()
|
||||
})
|
||||
@@ -90,6 +119,9 @@ export default {
|
||||
getProviders,
|
||||
createProvider,
|
||||
|
||||
getProvisionTemplate,
|
||||
getProvisionsTemplates,
|
||||
|
||||
getProvision,
|
||||
getProvisions,
|
||||
createProvision
|
||||
|
||||
@@ -60,7 +60,24 @@ export default {
|
||||
},
|
||||
MuiCssBaseline: {
|
||||
'@global': {
|
||||
'@font-face': [UbuntuFont]
|
||||
'@font-face': [UbuntuFont],
|
||||
'.fade': {
|
||||
'&-enter': {
|
||||
opacity: 0
|
||||
},
|
||||
'&-enter-active': {
|
||||
opacity: 1,
|
||||
transition: 'opacity 100ms'
|
||||
},
|
||||
'&-exit': {
|
||||
opacity: 1,
|
||||
transform: 'scale(1)'
|
||||
},
|
||||
'&-exit-active': {
|
||||
opacity: 0,
|
||||
transition: 'opacity 100ms'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,3 +17,4 @@ export * from 'client/utils/utils'
|
||||
export * from 'client/utils/request'
|
||||
export * from 'client/utils/helpers'
|
||||
export * from 'client/utils/polyfills'
|
||||
export * from 'client/utils/schema'
|
||||
@@ -0,0 +1,93 @@
|
||||
import * as yup from 'yup'
|
||||
import { INPUT_TYPES } from 'client/constants'
|
||||
|
||||
const requiredSchema = (mandatory, name, schema) =>
|
||||
mandatory
|
||||
? schema.required(`${name} field is required`)
|
||||
: schema.notRequired().nullable()
|
||||
|
||||
const getRange = (options) =>
|
||||
options.split('..').map(option => parseFloat(option))
|
||||
|
||||
const getOptionsFromList = (options, separator = ',') =>
|
||||
options?.split(separator).map(value => ({ text: value, value }))
|
||||
|
||||
export const schemaUserInput = ({ mandatory, name, type, options, defaultValue }) => {
|
||||
switch (type) {
|
||||
case 'text':
|
||||
case 'text64':
|
||||
case 'password': return {
|
||||
type: INPUT_TYPES.TEXT,
|
||||
htmlType: type === 'password' ? 'password' : 'text',
|
||||
validation: yup.string()
|
||||
.trim()
|
||||
.concat(requiredSchema(mandatory, name, yup.string()))
|
||||
.default(defaultValue || undefined)
|
||||
}
|
||||
case 'number':
|
||||
case 'number-float': return {
|
||||
type: INPUT_TYPES.TEXT,
|
||||
htmlType: 'number',
|
||||
validation: yup.number()
|
||||
.typeError(`${name} must be a number`)
|
||||
.concat(requiredSchema(mandatory, name, yup.number()))
|
||||
.transform(value => !isNaN(value) ? value : null)
|
||||
.default(() => parseFloat(defaultValue) ?? undefined)
|
||||
}
|
||||
case 'range':
|
||||
case 'range-float': {
|
||||
const [min, max] = getRange(options)
|
||||
|
||||
return {
|
||||
type: INPUT_TYPES.SLIDER,
|
||||
validation: yup.number()
|
||||
.typeError(`${name} must be a number`)
|
||||
.concat(requiredSchema(mandatory, name, yup.number()))
|
||||
.min(min, `${name} must be greater than or equal to ${min}`)
|
||||
.max(max, `${name} must be less than or equal to ${max}`)
|
||||
.transform(value => !isNaN(value) ? value : undefined)
|
||||
.default(parseFloat(defaultValue) ?? undefined),
|
||||
fieldProps: { min, max, step: type === 'range-float' ? 0.01 : 1 }
|
||||
}
|
||||
}
|
||||
case 'boolean': return {
|
||||
type: INPUT_TYPES.CHECKBOX,
|
||||
validation: yup.boolean()
|
||||
.concat(requiredSchema(mandatory, name, yup.boolean()))
|
||||
.default(defaultValue === 'YES' ?? false)
|
||||
}
|
||||
case 'list': {
|
||||
const values = getOptionsFromList(options)
|
||||
|
||||
return {
|
||||
values,
|
||||
type: INPUT_TYPES.SELECT,
|
||||
validation: yup.string()
|
||||
.trim()
|
||||
.concat(requiredSchema(mandatory, name, yup.string()))
|
||||
.oneOf(values.map(({ value }) => value))
|
||||
.default(defaultValue ?? undefined)
|
||||
}
|
||||
}
|
||||
case 'list-multiple': {
|
||||
const values = getOptionsFromList(options)
|
||||
const defaultValues = defaultValue?.split(',') ?? undefined
|
||||
|
||||
return {
|
||||
values,
|
||||
type: INPUT_TYPES.SELECT,
|
||||
multiple: true,
|
||||
validation: yup.array(yup.string().trim())
|
||||
.concat(requiredSchema(mandatory, name, yup.array()))
|
||||
.default(defaultValues)
|
||||
}
|
||||
}
|
||||
default: return {
|
||||
type: INPUT_TYPES.TEXT,
|
||||
validation: yup.string()
|
||||
.trim()
|
||||
.concat(requiredSchema(mandatory, name, yup.string()))
|
||||
.default(defaultValue || undefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,8 @@ export const requestData = (url = '', data = {}) => {
|
||||
baseURL: params.baseURL,
|
||||
headers: {},
|
||||
validateStatus: status =>
|
||||
Object.values(httpCodes).some(({ id }) => id === status)
|
||||
Object.values(httpCodes).some(({ id }) => id === status),
|
||||
...params?.config
|
||||
}
|
||||
|
||||
if (params.json) {
|
||||
|
||||
Reference in new issue
Block a user