From ceb46590bd40c91d6b86344bd2628de06333f7d4 Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Thu, 26 Nov 2020 17:44:35 +0100 Subject: [PATCH] F #3951: Add catch when JSON parse (#488) --- .../src/client/components/Cards/ProvisionCard.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/fireedge/src/client/components/Cards/ProvisionCard.js b/src/fireedge/src/client/components/Cards/ProvisionCard.js index ff958c1536..e24b39606c 100644 --- a/src/fireedge/src/client/components/Cards/ProvisionCard.js +++ b/src/fireedge/src/client/components/Cards/ProvisionCard.js @@ -1,4 +1,4 @@ -import React, { memo } from 'react' +import React, { memo, useState, useEffect } from 'react' import PropTypes from 'prop-types' import { Avatar, IconButton } from '@material-ui/core' @@ -11,9 +11,19 @@ import { STATIC_FILES_URL } from 'client/constants' const ProvisionCard = memo( ({ value, isSelected, handleClick, isProvider, imgAsAvatar, actions }) => { const DEFAULT_IMAGE = isProvider ? 'provider.png' : 'provision.jpg' + + const [image, setImage] = useState(undefined) const { ID, NAME, TEMPLATE: { PLAIN = '{}' } } = value - // TODO: show error when fail parse - const { image = DEFAULT_IMAGE } = JSON.parse(PLAIN) ?? {} + + useEffect(() => { + try { + const plain = JSON.parse(PLAIN) + setImage(plain?.image ?? DEFAULT_IMAGE) + } catch { + setImage(DEFAULT_IMAGE) + console.warn('Image in plain property is not valid') + } + }, []) const isExternalURL = RegExp(/^(http|https):/g).test(image)