import { Add, Delete, Edit, Flip, PlusOne, ToggleOff, ToggleOn, Widgets, } from '@mui/icons-material' import { Box, Button, Chip, Container, Grid, IconButton, Typography, } from '@mui/joy' import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { useNotification } from '../../service/NotificationProvider' import { CreateThing, DeleteThing, GetThings, SaveThing, UpdateThingState, } from '../../utils/Fetcher' import ConfirmationModal from '../Modals/Inputs/ConfirmationModal' import CreateThingModal from '../Modals/Inputs/CreateThingModal' import EditThingStateModal from '../Modals/Inputs/EditThingState' const ThingCard = ({ thing, onEditClick, onStateChangeRequest, onDeleteClick, }) => { const [isDisabled, setIsDisabled] = useState(false) const Navigate = useNavigate() const getThingIcon = type => { if (type === 'text') { return } else if (type === 'number') { return } else if (type === 'boolean') { if (thing.state === 'true') { return } else { return } } else { return } } const handleRequestChange = thing => { setIsDisabled(true) onStateChangeRequest(thing) setTimeout(() => { setIsDisabled(false) }, 2000) } return ( Navigate(`/things/${thing?.id}`)} > Navigate(`/things/${thing?.id}`)} > {thing?.name} {thing?.type} State: {thing?.state} onEditClick(thing)} sx={{ borderRadius: '50%', width: 30, height: 30, ml: 1, transition: 'background-color 0.2s', '&:hover': { backgroundColor: 'action.hover' }, }} > onDeleteClick(thing)} sx={{ borderRadius: '50%', width: 30, height: 30, ml: 1, }} > ) } const ThingsView = () => { const [things, setThings] = useState([]) const [isShowCreateThingModal, setIsShowCreateThingModal] = useState(false) const [isShowEditThingStateModal, setIsShowEditStateModal] = useState(false) const [createModalThing, setCreateModalThing] = useState(null) const [confirmModelConfig, setConfirmModelConfig] = useState({}) const { showError, showNotification } = useNotification() useEffect(() => { // fetch things GetThings().then(result => { result.json().then(data => { setThings(data.res) }) }) }, []) const handleSaveThing = thing => { let saveFunc = CreateThing if (thing?.id) { saveFunc = SaveThing } saveFunc(thing) .then(result => { result.json().then(data => { if (thing?.id) { const currentThings = [...things] const thingIndex = currentThings.findIndex( currentThing => currentThing.id === thing.id, ) currentThings[thingIndex] = data.res setThings(currentThings) } else { const currentThings = [...things] currentThings.push(data.res) setThings(currentThings) } showNotification({ type: 'success', title: 'Thing Saved', message: 'Thing saved successfully', }) }) }) .catch(error => { if (error?.queued) { showError({ title: 'Unable to save thing', message: 'You are offline and the request has been queued', }) } else { showError({ title: 'Unable to save thing', message: 'An error occurred while saving the thing', }) } }) } const handleEditClick = thing => { setIsShowEditStateModal(true) setCreateModalThing(thing) } const handleDeleteClick = thing => { setConfirmModelConfig({ isOpen: true, title: 'Delete Things', confirmText: 'Delete', cancelText: 'Cancel', message: 'Are you sure you want to delete this Thing?', onClose: isConfirmed => { if (isConfirmed === true) { DeleteThing(thing.id) .then(response => { if (response.ok) { const currentThings = [...things] const thingIndex = currentThings.findIndex( currentThing => currentThing.id === thing.id, ) currentThings.splice(thingIndex, 1) setThings(currentThings) } else if (response.status === 405) { showError({ title: 'Unable to Delete Thing', message: 'Unable to delete thing with associated tasks', }) } // if method not allwo show snackbar: }) .catch(error => { if (error?.queued) { showError({ title: 'Unable to delete thing', message: 'You are offline and the request has been queued', }) } else { showError({ title: 'Unable to delete thing', message: 'An error occurred while deleting the thing', }) } }) } setConfirmModelConfig({}) }, }) } const handleStateChangeRequest = thing => { if (thing?.type === 'number') { thing.state = Number(thing.state) + 1 } else if (thing?.type === 'boolean') { if (thing.state === 'true') { thing.state = 'false' } else { thing.state = 'true' } } UpdateThingState(thing) .then(result => { result.json().then(data => { const currentThings = [...things] const thingIndex = currentThings.findIndex( currentThing => currentThing.id === thing.id, ) currentThings[thingIndex] = data.res setThings(currentThings) showNotification({ type: 'success', title: 'Thing Updated', message: 'Thing state updated successfully', }) }) }) .catch(error => { if (error?.queued) { showError({ title: 'Unable to update thing state', message: 'You are offline and the request has been queued', }) } else { showError({ title: 'Unable to update thing state', message: 'An error occurred while updating the thing state', }) } }) } return ( {things.length === 0 && ( No things has been created/found )} {things.map(thing => ( ))} } onClick={() => { setIsShowCreateThingModal(true) }} > {isShowCreateThingModal && ( { setIsShowCreateThingModal(false) setCreateModalThing(null) }} onSave={handleSaveThing} currentThing={createModalThing} /> )} {isShowEditThingStateModal && ( { setIsShowEditStateModal(false) setCreateModalThing(null) }} onSave={handleStateChangeRequest} currentThing={createModalThing} /> )} ) } export default ThingsView