2021-04-20 04:37:17 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-04-18 16:44:03 +00:00
|
|
|
import { resolve_references } from '@/_helpers/utils';
|
2021-04-16 08:56:07 +00:00
|
|
|
import DOMPurify from 'dompurify';
|
2021-04-20 04:37:17 +00:00
|
|
|
import Skeleton from 'react-loading-skeleton';
|
2021-04-04 13:11:17 +00:00
|
|
|
|
2021-04-10 03:49:07 +00:00
|
|
|
export const Text = function Text({ id, width, height, component, onComponentClick, currentState }) {
|
2021-04-04 13:11:17 +00:00
|
|
|
|
|
|
|
|
const text = component.definition.properties.text.value;
|
|
|
|
|
const color = component.definition.styles.textColor.value;
|
|
|
|
|
|
2021-04-20 04:37:17 +00:00
|
|
|
const [loadingState, setLoadingState] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
|
|
const loadingStateProperty = component.definition.properties.loadingState;
|
|
|
|
|
if(loadingStateProperty && currentState) {
|
|
|
|
|
const newState = resolve_references(loadingStateProperty.value, currentState, false);
|
|
|
|
|
setLoadingState(newState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, [currentState]);
|
|
|
|
|
|
2021-04-04 13:11:17 +00:00
|
|
|
let data = text;
|
|
|
|
|
if(currentState) {
|
|
|
|
|
|
|
|
|
|
const matchedParams = text.match(/\{\{(.*?)\}\}/g);
|
|
|
|
|
|
2021-04-06 03:15:10 +00:00
|
|
|
if (matchedParams) {
|
|
|
|
|
for(const param of matchedParams) {
|
2021-04-18 16:44:03 +00:00
|
|
|
const resolvedParam = resolve_references(param, currentState, '');
|
2021-04-06 03:15:10 +00:00
|
|
|
console.log('resolved param', param, resolvedParam);
|
|
|
|
|
data = data.replace(param, resolvedParam);
|
|
|
|
|
}
|
2021-04-04 13:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 08:56:07 +00:00
|
|
|
const computedStyles = {
|
2021-04-10 03:49:07 +00:00
|
|
|
color,
|
|
|
|
|
width,
|
2021-04-11 07:08:44 +00:00
|
|
|
height,
|
2021-04-04 13:11:17 +00:00
|
|
|
}
|
2021-04-02 04:16:26 +00:00
|
|
|
|
|
|
|
|
return (
|
2021-04-16 08:56:07 +00:00
|
|
|
<div style={computedStyles} onClick={() => onComponentClick(id, component) }>
|
2021-04-20 04:37:17 +00:00
|
|
|
{!loadingState &&
|
|
|
|
|
<div
|
|
|
|
|
dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(data)}}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2021-04-27 11:32:13 +00:00
|
|
|
{loadingState === true &&
|
2021-04-20 04:37:17 +00:00
|
|
|
<div>
|
|
|
|
|
<Skeleton count={1}/>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
2021-04-16 08:56:07 +00:00
|
|
|
</div>
|
2021-04-02 04:16:26 +00:00
|
|
|
);
|
|
|
|
|
};
|