// import dependencies import shortid from 'shortid'; import React, { useEffect, useState } from 'react'; import { Box, Icon, TextField, IconButton, InputAdornment, View, MenuItem, Button, Tooltip, LoadingButton, Stack, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions } from '../'; // let context let DashupContext = null; // global timer let timer; // global debounce const debounce = (func, timeout = 1000) => { // return debounced return (...args) => { // clear timeout previously clearTimeout(timer); // create new timeout timer = setTimeout(() => func(...args), timeout); }; } // create menu component const DashupUIPageConnect = (props = {}) => { // state const [saving, setSaving] = useState(false); const [updated, setUpdated] = useState(new Date()); const [updating, setUpdating] = useState(null); const [removing, setRemoving] = useState(null); // get type const getType = (page, available) => { // get type return [...available].filter((connect) => (connect.categories || []).includes(page.get('type'))).map((connect) => { // return value return { icon : connect.icon, value : connect.type, label : connect.title, selected : connect.type === updating?.type, }; }); }; // set connect const setConnect = (key, value, { page, setPage }) => { // set to updating updating[key] = value; setUpdated(new Date()); // set page page.set('connects', [...page.get('connects')]); // check key if (key === 'type') { onSubmit(page, setPage); } }; // on submit const onSubmit = async (page, setPage) => { // set submitting setSaving(true); // set page await setPage('connects', page.get('connects')); // set saving setSaving(false); }; // set connect const onRemove = async (page, setPage) => { // set submitting setSaving(true); // new connects const newConnects = (page.get('connects') || []).filter((c) => c.uuid !== removing.uuid); // set page page.set('connects', newConnects); await setPage('connects', newConnects); // set saving setSaving(false); // remove setUpdating(null); setRemoving(null); }; // on create const onCreate = (page, setPage) => { // set connect const connect = { uuid : shortid(), }; // add to connects const connects = page.get('connects') || []; // add connect connects.push(connect); // set connects page.set('connects', connects); // set connect setUpdating(connect); debounce(setPage, 500)('connects', page.get('connects')); }; // use effect useEffect(() => { // page const onUpdate = () => { // check updating if (updating) { // set values const realUpdating = (props.page.get('connects') || []).find((c) => c.uuid === updating.uuid); // set values if (realUpdating) setUpdating(realUpdating); } // set updated setUpdated(new Date()); }; // on update props.page.on('connects', onUpdate); // return done return () => { // remove listener props.page.removeListener('connects', onUpdate); }; }, [props.page && props.page.get('_id'), updating?.uuid]); // return JSX return ( { (data) => { // destruct const { page, available, dashup, setPage, getConnectStruct } = data; // get connects const connects = page.get('connects') || []; // return jsx return ( <> { updating ? ( setConnect('type', e.target?.value, { page, setPage }) } fullWidth > { getType(page, available?.connects).map((option) => ( { option.label } ))} { !!updating.type && ( <> setConnect('name', e.target?.value, { page, setPage }) } fullWidth /> setConnect(key, value, { page, setPage }) } /> ) } ) : ( { (connects || []).length ? connects.map((connect, i) => { // return jsx return ( ), endAdornment : ( <> setRemoving(connect) }> setUpdating(connect) }> ) } } /> ); }) : ( This page has no connects, click below to create one. ) } ) } { !!updating && ( setRemoving(updating) }> ) } { !!updating && ( ) } { updating ? ( onSubmit(page, setPage) }> { saving ? 'Submitting...' : 'Submit' } ) : ( onCreate(page, setPage) }> New Connect ) } setRemoving(null) } > Confirm Remove Are you sure you want to remove this connect? onRemove(page, setPage) } loading={ saving }> { saving ? 'Removing...' : 'Remove' } ); } } ); }; // export default page menu export default (ctx) => { // use context DashupContext = ctx; // return actual component return DashupUIPageConnect; };