diff --git a/src/fireedge/src/client/assets/provider.png b/src/fireedge/src/client/assets/provider.png new file mode 100644 index 0000000000..2872311923 Binary files /dev/null and b/src/fireedge/src/client/assets/provider.png differ diff --git a/src/fireedge/src/client/assets/provision.jpg b/src/fireedge/src/client/assets/provision.jpg new file mode 100644 index 0000000000..4defc15745 Binary files /dev/null and b/src/fireedge/src/client/assets/provision.jpg differ diff --git a/src/fireedge/src/client/components/Cards/ProviderCard.js b/src/fireedge/src/client/components/Cards/ProviderCard.js index 9ef8db161a..b6fac9717c 100644 --- a/src/fireedge/src/client/components/Cards/ProviderCard.js +++ b/src/fireedge/src/client/components/Cards/ProviderCard.js @@ -1,17 +1,25 @@ import React, { memo } from 'react' import PropTypes from 'prop-types' -import TemplateIcon from '@material-ui/icons/Description' import SelectCard from 'client/components/Cards/SelectCard' +const DEFAULT_IMAGE = '/client/assets/provider.png' + const ProviderCard = memo(({ value, actions }) => { - const { ID, NAME } = value + const { ID, NAME, TEMPLATE: { PLAIN = '{}' } } = value + const { image = DEFAULT_IMAGE } = JSON.parse(PLAIN) return ( } actions={actions} + mediaProps={{ + component: 'img', + image, + onError: evt => { + evt.target.src = DEFAULT_IMAGE + } + }} /> ) }) @@ -19,7 +27,14 @@ const ProviderCard = memo(({ value, actions }) => { ProviderCard.propTypes = { value: PropTypes.shape({ ID: PropTypes.string.isRequired, - NAME: PropTypes.string.isRequired + NAME: PropTypes.string.isRequired, + TEMPLATE: PropTypes.shape({ + PLAIN: PropTypes.string, + PROVISION_BODY: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.object + ]) + }) }), actions: PropTypes.arrayOf( PropTypes.shape({ diff --git a/src/fireedge/src/client/components/Cards/ProviderTemplateCard.js b/src/fireedge/src/client/components/Cards/ProviderTemplateCard.js deleted file mode 100644 index d4f8cf3565..0000000000 --- a/src/fireedge/src/client/components/Cards/ProviderTemplateCard.js +++ /dev/null @@ -1,35 +0,0 @@ -import React, { memo } from 'react' -import PropTypes from 'prop-types' - -import FileIcon from '@material-ui/icons/Description' -import SelectCard from 'client/components/Cards/SelectCard' - -const ProviderTemplateCard = memo( - ({ value, handleClick }) => { - const { name } = value - - return ( - } - /> - ) - } -) - -ProviderTemplateCard.propTypes = { - value: PropTypes.shape({ - name: PropTypes.string.isRequired - }), - handleClick: PropTypes.func -} - -ProviderTemplateCard.defaultProps = { - value: {}, - handleClick: undefined -} - -ProviderTemplateCard.displayName = 'ProviderTemplateCard' - -export default ProviderTemplateCard diff --git a/src/fireedge/src/client/components/Cards/ProvisionCard.js b/src/fireedge/src/client/components/Cards/ProvisionCard.js index 97c53c0fdf..c4f5d36fa0 100644 --- a/src/fireedge/src/client/components/Cards/ProvisionCard.js +++ b/src/fireedge/src/client/components/Cards/ProvisionCard.js @@ -1,17 +1,25 @@ import React, { memo } from 'react' import PropTypes from 'prop-types' -import ProvisionIcon from '@material-ui/icons/Cloud' import SelectCard from 'client/components/Cards/SelectCard' +const DEFAULT_IMAGE = '/client/assets/provision.jpg' + const ProvisionCard = memo(({ value, actions }) => { - const { ID, NAME } = value + const { ID, NAME, TEMPLATE: { PLAIN = '{}' } } = value + const { image = DEFAULT_IMAGE } = JSON.parse(PLAIN) return ( } actions={actions} + mediaProps={{ + component: 'img', + image, + onError: evt => { + evt.target.src = DEFAULT_IMAGE + } + }} /> ) }) @@ -19,7 +27,14 @@ const ProvisionCard = memo(({ value, actions }) => { ProvisionCard.propTypes = { value: PropTypes.shape({ ID: PropTypes.string.isRequired, - NAME: PropTypes.string.isRequired + NAME: PropTypes.string.isRequired, + TEMPLATE: PropTypes.shape({ + PLAIN: PropTypes.string, + PROVISION_BODY: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.object + ]) + }) }), actions: PropTypes.arrayOf( PropTypes.shape({ diff --git a/src/fireedge/src/client/components/Cards/ProvisionTemplateCard.js b/src/fireedge/src/client/components/Cards/ProvisionTemplateCard.js new file mode 100644 index 0000000000..50957094e5 --- /dev/null +++ b/src/fireedge/src/client/components/Cards/ProvisionTemplateCard.js @@ -0,0 +1,59 @@ +import React, { memo } from 'react' +import PropTypes from 'prop-types' + +import { Avatar } from '@material-ui/core' +import SelectCard from 'client/components/Cards/SelectCard' + +const PROVIDER_IMAGE = '/client/assets/provider.png' +const PROVISION_IMAGE = '/client/assets/provision.jpg' + +const ProvisionTemplateCard = memo( + ({ value, isProvider, isSelected, handleClick }) => { + const { name, plain: { image } = {} } = value + const DEFAULT_IMG = isProvider ? PROVIDER_IMAGE : PROVISION_IMAGE + + return ( + { + evt.target.src = DEFAULT_IMG + } + }} + /> + } + /> + ) + } +) + +ProvisionTemplateCard.propTypes = { + value: PropTypes.shape({ + name: PropTypes.string.isRequired, + plain: PropTypes.shape({ + image: PropTypes.string + }) + }), + isProvider: PropTypes.bool, + isSelected: PropTypes.bool, + handleClick: PropTypes.func +} + +ProvisionTemplateCard.defaultProps = { + value: {}, + isProvider: undefined, + isSelected: undefined, + handleClick: undefined +} + +ProvisionTemplateCard.displayName = 'ProvisionTemplateCard' + +export default ProvisionTemplateCard diff --git a/src/fireedge/src/client/components/Cards/SelectCard.js b/src/fireedge/src/client/components/Cards/SelectCard.js index 33311f9f41..7ae2c45096 100644 --- a/src/fireedge/src/client/components/Cards/SelectCard.js +++ b/src/fireedge/src/client/components/Cards/SelectCard.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types' import clsx from 'clsx' import { - makeStyles, Card, CardActionArea, CardHeader, CardActions, IconButton + makeStyles, Card, CardActionArea, CardHeader, CardActions, IconButton, CardMedia } from '@material-ui/core' import { Skeleton } from '@material-ui/lab' @@ -11,7 +11,10 @@ import useNearScreen from 'client/hooks/useNearScreen' import ConditionalWrap from 'client/components/HOC/ConditionalWrap' const useStyles = makeStyles(theme => ({ - root: { height: '100%' }, + root: { + height: '100%', + display: ({ isMedia }) => isMedia ? 'flex' : 'block' + }, selected: { color: theme.palette.primary.contrastText, backgroundColor: theme.palette.primary.main, @@ -23,13 +26,20 @@ const useStyles = makeStyles(theme => ({ color: 'inherit' } }, - actionArea: { + actionArea: ({ minHeight = 140, isMedia }) => ({ height: '100%', - minHeight: ({ minHeight = 140 }) => minHeight, - padding: theme.spacing(1) - }, + minHeight, + padding: !isMedia && theme.spacing(1) + }), + mediaImage: { width: '40%' }, + headerRoot: ({ isMedia }) => isMedia && ({ + flex: '1' + }), headerAvatar: { display: 'flex' }, headerContent: { overflowX: 'hidden' }, + title: ({ isMedia }) => isMedia && ({ + paddingLeft: theme.spacing(1) + }), subheader: { overflow: 'hidden', textOverflow: 'ellipsis', @@ -38,7 +48,17 @@ const useStyles = makeStyles(theme => ({ lineClamp: 2, boxOrient: 'vertical' }, - actionsIcon: { margin: theme.spacing(1) } + actions: ({ isMedia }) => isMedia && ({ + justifyContent: 'space-around', + padding: theme.spacing(1) + }), + actionsIcon: { margin: theme.spacing(1) }, + details: { + display: 'flex', + flexDirection: 'column', + flex: 1, + width: ({ isMedia }) => isMedia && '60%' + } })) const SelectCard = memo(({ @@ -49,13 +69,14 @@ const SelectCard = memo(({ title, subheader, cardHeaderProps, + mediaProps, isSelected, handleClick, cardProps, observerOff, children }) => { - const classes = useStyles(stylesProps) + const classes = useStyles({ ...stylesProps, isMedia: !!mediaProps }) const { isNearScreen, fromRef } = useNearScreen({ distance: '100px' }) @@ -89,38 +110,56 @@ const SelectCard = memo(({ } > - - {children} - {actions?.length > 0 && ( - - {actions?.map(action => renderAction(action))} - + {mediaProps && ( + )} + +
+ {children} +
+ } + > + + {children} + {actions?.length > 0 && ( + + {actions?.map(action => renderAction(action))} + + )} +
) : ( - + )} ) @@ -155,6 +194,14 @@ SelectCard.propTypes = { titleTypographyProps: PropTypes.object, subheaderTypographyProps: PropTypes.object }), + mediaProps: PropTypes.shape({ + classes: PropTypes.object, + className: PropTypes.string, + component: PropTypes.elementType, + image: PropTypes.string, + src: PropTypes.string, + style: PropTypes.object + }), isSelected: PropTypes.bool, handleClick: PropTypes.func, cardProps: PropTypes.object, @@ -174,6 +221,7 @@ SelectCard.defaultProps = { title: undefined, subheader: undefined, cardHeaderProps: undefined, + mediaProps: undefined, isSelected: false, handleClick: undefined, cardProps: undefined, diff --git a/src/fireedge/src/client/components/Cards/index.js b/src/fireedge/src/client/components/Cards/index.js index babda1b4ec..28ca7d111a 100644 --- a/src/fireedge/src/client/components/Cards/index.js +++ b/src/fireedge/src/client/components/Cards/index.js @@ -9,7 +9,7 @@ import ApplicationTemplateCard from 'client/components/Cards/ApplicationTemplate import ApplicationCard from 'client/components/Cards/ApplicationCard' import ApplicationNetworkCard from 'client/components/Cards/ApplicationNetworkCard' import PolicyCard from 'client/components/Cards/PolicyCard' -import ProviderTemplateCard from 'client/components/Cards/ProviderTemplateCard' +import ProvisionTemplateCard from 'client/components/Cards/ProvisionTemplateCard' import ProviderCard from 'client/components/Cards/ProviderCard' import ProvisionCard from 'client/components/Cards/ProvisionCard' import LocationCard from 'client/components/Cards/LocationCard' @@ -26,7 +26,7 @@ export { ApplicationCard, ApplicationNetworkCard, PolicyCard, - ProviderTemplateCard, + ProvisionTemplateCard, ProviderCard, ProvisionCard, LocationCard diff --git a/src/fireedge/src/client/containers/Login/styles.js b/src/fireedge/src/client/containers/Login/styles.js index 32d94914b9..cd6f1c7fa2 100644 --- a/src/fireedge/src/client/containers/Login/styles.js +++ b/src/fireedge/src/client/containers/Login/styles.js @@ -24,7 +24,7 @@ export default makeStyles(theme => paper: { overflow: 'hidden', padding: theme.spacing(2), - height: 440, + minHeight: 440, [theme.breakpoints.up('xs')]: { display: 'flex', flexDirection: 'column' diff --git a/src/fireedge/src/client/containers/Providers/Form/Create/Steps/Provider/index.js b/src/fireedge/src/client/containers/Providers/Form/Create/Steps/Provider/index.js index 4d09ed344c..ad50dd95bd 100644 --- a/src/fireedge/src/client/containers/Providers/Form/Create/Steps/Provider/index.js +++ b/src/fireedge/src/client/containers/Providers/Form/Create/Steps/Provider/index.js @@ -1,14 +1,12 @@ import React, { useCallback, useEffect } from 'react' import { Redirect } from 'react-router-dom' -import FileIcon from '@material-ui/icons/Description' - 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 { EmptyCard, ProvisionTemplateCard } from 'client/components/Cards' import { PATH } from 'client/router/provision' import { STEP_ID as CONNECTION_ID } from 'client/containers/Providers/Form/Create/Steps/Connection' @@ -50,13 +48,12 @@ const Provider = () => ({ EmptyComponent={ } - CardComponent={SelectCard} + CardComponent={ProvisionTemplateCard} cardsProps={({ value: { name } }) => { const isSelected = data?.some(selected => selected === name) return { - icon: , - title: name, + isProvider: true, isSelected, handleClick: () => handleClick(name, isSelected) } diff --git a/src/fireedge/src/client/containers/Providers/Form/Create/index.js b/src/fireedge/src/client/containers/Providers/Form/Create/index.js index 3f1894518d..1d6447030f 100644 --- a/src/fireedge/src/client/containers/Providers/Form/Create/index.js +++ b/src/fireedge/src/client/containers/Providers/Form/Create/index.js @@ -58,6 +58,9 @@ function ProviderCreateForm () { const formatData = { ...(!isUpdate && { name: `${providerSelected}_${locationSelected}` }), + ...(providerTemplate?.image && { + plain: { image: providerTemplate.image } + }), provider: providerSelected, connection: { ...connection, @@ -76,7 +79,13 @@ function ProviderCreateForm () { } useEffect(() => { - isUpdate && fetchRequestAll([getProvider({ id }), getProvidersTemplates()]) + isUpdate && fetchRequestAll([ + getProvider({ id }), + getProvidersTemplates({ + onError: () => + history.push(PATH.PROVIDERS.LIST) + }) + ]) }, [isUpdate]) useEffect(() => { @@ -84,16 +93,20 @@ function ProviderCreateForm () { const [provider = {}, templates = []] = data const { TEMPLATE: { PROVISION_BODY = {} } } = provider - const { connection, provider: providerName, registration_time: time } = PROVISION_BODY + const { + connection, provider: providerName, registration_time: time + } = PROVISION_BODY const { location_key: key } = templates?.find(({ name }) => name === providerName) ?? {} if (!key) { - showError(` - Cannot found provider template (${providerName}), - ask your cloud administrator`) + showError({ + message: ` + Cannot found provider template (${providerName}), + ask your cloud administrator` + }) history.push(PATH.PROVIDERS.LIST) } diff --git a/src/fireedge/src/client/containers/Provisions/Form/Create/Steps/Provision/index.js b/src/fireedge/src/client/containers/Provisions/Form/Create/Steps/Provision/index.js index 940543d379..4cd9e2ad02 100644 --- a/src/fireedge/src/client/containers/Provisions/Form/Create/Steps/Provision/index.js +++ b/src/fireedge/src/client/containers/Provisions/Form/Create/Steps/Provision/index.js @@ -1,19 +1,15 @@ import React, { useCallback, useEffect } from 'react' - import { Redirect } from 'react-router-dom' -import ProvisionIcon from '@material-ui/icons/SettingsSystemDaydream' 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 { EmptyCard, ProvisionTemplateCard } 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_ID as INPUTS_ID } from 'client/containers/Provisions/Form/Create/Steps/Inputs' import { STEP_FORM_SCHEMA } from './schema' export const STEP_ID = 'provision' @@ -51,13 +47,11 @@ const Provision = () => ({ EmptyComponent={ } - CardComponent={SelectCard} + CardComponent={ProvisionTemplateCard} cardsProps={({ value: { name } }) => { const isSelected = data?.some(selected => selected === name) return { - icon: , - title: name, isSelected, handleClick: () => handleClick(name, isSelected) } diff --git a/src/fireedge/src/client/hooks/useFetch.js b/src/fireedge/src/client/hooks/useFetch.js index fc0b8fd266..17a91dccb7 100644 --- a/src/fireedge/src/client/hooks/useFetch.js +++ b/src/fireedge/src/client/hooks/useFetch.js @@ -1,5 +1,4 @@ import { useState, useCallback, useEffect, useRef } from 'react' -import axios from 'axios' import { debounce } from '@material-ui/core' import { fakeDelay } from 'client/utils' @@ -15,7 +14,7 @@ const useRequest = request => { const doFetch = useCallback( debounce(payload => request({ ...payload }).then(response => { - if (isMounted.current && !axios.isCancel(response)) { + if (isMounted.current) { if (response !== undefined) { setData(response) setError(false) diff --git a/src/fireedge/src/client/hooks/useFetchAll.js b/src/fireedge/src/client/hooks/useFetchAll.js index 5012cc0b7e..cb5ba1cec9 100644 --- a/src/fireedge/src/client/hooks/useFetchAll.js +++ b/src/fireedge/src/client/hooks/useFetchAll.js @@ -1,26 +1,54 @@ -import { useState, useCallback } from 'react' +import { useState, useCallback, useEffect, useRef } from 'react' +import { debounce } from '@material-ui/core' +import { fakeDelay } from 'client/utils' const useRequestAll = () => { const [data, setData] = useState(undefined) const [loading, setLoading] = useState(false) + const [reloading, setReloading] = useState(false) const [error, setError] = useState(false) + const isMounted = useRef(true) - const fetchRequestAll = useCallback( - requests => { - setLoading(true) - setError(false) + useEffect(() => () => { isMounted.current = false }, []) + const doFetchs = useCallback( + debounce(requests => Promise .all(requests) .then(response => { - if (response !== undefined) setData(response) - else setError(true) - }) - .finally(() => { setLoading(false) }) - }, - [] - ) + if (isMounted.current) { + if (response !== undefined) { + setData(response) + setError(false) + } else setError(true) - return { data, fetchRequestAll, loading, error } + setLoading(false) + setReloading(false) + } + }) + ), [isMounted]) + + const fetchRequestAll = useCallback((requests, options = {}) => { + const { reload = false, delay = 0 } = options + if (!(Number.isInteger(delay) && delay >= 0)) { + console.error(` + Delay must be a number >= 0! + If you're using it as a function, it must also return a number >= 0.`) + } + + if (isMounted.current) { + reload ? setReloading(true) : setLoading(true) + } + + fakeDelay(delay).then(() => doFetchs(requests)) + }, [isMounted]) + + return { + data, + fetchRequestAll, + loading, + reloading, + error + } } export default useRequestAll diff --git a/src/fireedge/src/client/hooks/useProvision.js b/src/fireedge/src/client/hooks/useProvision.js index 7f28255c3d..226803f7b8 100644 --- a/src/fireedge/src/client/hooks/useProvision.js +++ b/src/fireedge/src/client/hooks/useProvision.js @@ -33,15 +33,16 @@ export default function useOpennebula () { // -------------------------------------------- const getProvidersTemplates = useCallback( - ({ end, start } = { end: -1, start: -1 }) => + ({ onError }) => serviceProvision - .getProvidersTemplates({ filter, end, start }) + .getProvidersTemplates({ filter }) .then(doc => { dispatch(setProvidersTemplates(doc)) return doc }) .catch(err => { dispatch(enqueueError(err ?? 'Error GET providers templates')) + onError && onError(err) }), [dispatch, filter, providersTemplates] )