frontend/smart-hut/src/components/RoomModal.js

289 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
2020-03-23 20:24:17 +00:00
import {
Button,
Header,
Modal,
Form,
Input,
Icon,
Responsive,
2020-03-24 15:03:08 +00:00
Image,
2020-05-07 07:29:22 +00:00
Confirm,
2020-05-12 13:18:33 +00:00
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import SelectIcons from './SelectIcons';
import { RemoteService } from '../remote';
import { appActions } from '../storeActions';
2020-03-13 14:21:33 +00:00
2020-05-12 13:18:33 +00:00
const NO_IMAGE = 'https://react.semantic-ui.com/images/wireframe/image.png';
2020-04-11 11:57:03 +00:00
class RoomModal extends Component {
2020-03-13 14:21:33 +00:00
constructor(props) {
super(props);
2020-04-11 11:57:03 +00:00
this.state = this.initialState;
this.fileInputRef = React.createRef();
2020-03-14 14:25:55 +00:00
this.addRoomModal = this.addRoomModal.bind(this);
this.updateIcon = this.updateIcon.bind(this);
2020-05-04 15:58:08 +00:00
this.unsetImage = this.unsetImage.bind(this);
2020-04-11 11:57:03 +00:00
}
get initialState() {
return {
2020-05-12 13:18:33 +00:00
selectedIcon: this.type === 'new' ? 'home' : this.props.room.icon,
name: this.type === 'new' ? 'New Room' : this.props.room.name,
img: this.type === 'new' ? null : this.props.room.image,
2020-04-11 11:57:03 +00:00
openModal: false,
2020-05-07 07:29:22 +00:00
sure: false,
2020-04-11 11:57:03 +00:00
};
}
2020-05-04 15:58:08 +00:00
unsetImage = (e) => {
2020-04-11 11:57:03 +00:00
e.preventDefault();
2020-05-12 13:18:33 +00:00
this.setState({ ...this.state, img: '' });
2020-05-05 15:04:53 +00:00
};
2020-04-11 11:57:03 +00:00
setInitialState() {
2020-04-17 12:30:34 +00:00
this.setState(this.initialState);
2020-04-11 11:57:03 +00:00
}
get type() {
2020-05-12 13:18:33 +00:00
return !this.props.id ? 'new' : 'modify';
2020-03-14 14:25:55 +00:00
}
addRoomModal = (e) => {
2020-05-12 13:18:33 +00:00
const data = {
2020-03-23 20:24:17 +00:00
icon: this.state.selectedIcon,
name: this.state.name,
image: this.state.img,
};
2020-04-11 11:57:03 +00:00
2020-04-11 16:29:32 +00:00
this.props
.saveRoom(data, null)
2020-04-11 11:57:03 +00:00
.then(() => {
this.setInitialState();
this.closeModal();
})
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error in creating room', err));
2020-03-23 20:24:17 +00:00
};
2020-03-14 14:25:55 +00:00
modifyRoomModal = (e) => {
2020-05-12 13:18:33 +00:00
const data = {
2020-04-11 16:29:32 +00:00
icon: this.state.selectedIcon,
name: this.state.name,
2020-03-23 20:24:17 +00:00
image: this.state.img,
};
2020-04-11 11:57:03 +00:00
2020-05-12 13:18:33 +00:00
console.log('data', data);
2020-04-11 16:29:32 +00:00
this.props
.saveRoom(data, this.props.id)
2020-04-11 11:57:03 +00:00
.then(() => {
this.setInitialState();
this.closeModal();
})
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error in updating room', err));
2020-03-23 20:24:17 +00:00
};
2020-03-14 14:25:55 +00:00
deleteRoom = (e) => {
2020-04-11 16:29:32 +00:00
this.props
.deleteRoom(this.props.id)
.then(() => this.closeModal())
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('error in deleting room', err));
2020-03-23 20:24:17 +00:00
};
2020-03-13 14:21:33 +00:00
2020-05-07 07:29:22 +00:00
setSureTrue = () => {
this.setState({ sure: true });
};
2020-05-07 07:29:22 +00:00
setSureFalse = () => {
this.setState({ sure: false });
};
2020-05-07 07:29:22 +00:00
2020-03-14 14:25:55 +00:00
changeSomething = (event) => {
2020-05-12 13:18:33 +00:00
const nam = event.target.name;
const val = event.target.value;
2020-03-23 20:24:17 +00:00
this.setState({ [nam]: val });
};
2020-03-22 16:58:27 +00:00
2020-03-23 20:29:42 +00:00
closeModal = (e) => {
this.setState({ openModal: false });
};
2020-03-14 14:25:55 +00:00
2020-03-23 20:24:17 +00:00
openModal = (e) => {
this.setState({ openModal: true });
};
2020-03-14 14:25:55 +00:00
updateIcon(e) {
2020-03-23 20:24:17 +00:00
this.setState({ selectedIcon: e });
2020-03-14 14:25:55 +00:00
}
getBase64(file, callback) {
2020-05-12 13:18:33 +00:00
const reader = new FileReader();
reader.readAsDataURL(file.target.files[0]);
reader.onload = () => {
2020-03-24 16:08:19 +00:00
this.setState(Object.assign(this.state, { img: reader.result }));
};
reader.onerror = console.error;
}
2020-03-24 15:03:08 +00:00
render() {
2020-03-13 14:21:33 +00:00
const spaceDiv = {
2020-05-12 13:18:33 +00:00
background: '#f4f4f4',
padding: '10px 10px',
margin: '10px 0px',
2020-03-23 20:24:17 +00:00
};
return (
<div>
2020-03-26 09:26:43 +00:00
{!this.props.nicolaStop ? (
<div>
<Responsive minWidth={768}>
2020-05-12 13:18:33 +00:00
{this.type === 'new' ? (
2020-03-26 09:26:43 +00:00
<Button
icon
labelPosition="left"
inverted
onClick={this.openModal}
>
<Icon name="plus" size="small" />
ADD ROOM
</Button>
) : (
<Icon name="pencil" size="small" onClick={this.openModal} />
)}
</Responsive>
<Responsive maxWidth={768}>
2020-05-12 13:18:33 +00:00
{this.type === 'new' ? (
2020-03-26 09:26:43 +00:00
<Button
icon
fluid
labelPosition="left"
onClick={this.openModal}
>
<Icon name="plus" size="small" />
ADD ROOM
</Button>
) : (
<Button
icon
fluid
labelPosition="left"
onClick={this.openModal}
>
<Icon name="pencil" size="small" />
EDIT ROOM
</Button>
)}
</Responsive>
</div>
) : null}
2020-03-24 15:03:08 +00:00
2020-04-11 16:29:32 +00:00
<Modal closeIcon onClose={this.closeModal} open={this.state.openModal}>
2020-03-24 15:03:08 +00:00
<Header>
2020-05-12 13:18:33 +00:00
{this.type === 'new' ? 'Add new room' : 'Modify room'}
2020-03-24 15:03:08 +00:00
</Header>
<Modal.Content>
<Form>
<p>Insert the name of the room:</p>
<Form.Field>
<Input
label="Room name"
placeholder="Room Name"
name="name"
type="text"
onChange={this.changeSomething}
value={this.state.name}
/>
</Form.Field>
<p>Insert an image of the room:</p>
<Form.Field>
<Image
2020-04-11 16:29:32 +00:00
src={!this.state.img ? NO_IMAGE : this.state.img}
2020-03-24 15:03:08 +00:00
size="small"
onClick={() => this.fileInputRef.current.click()}
/>
<input
ref={this.fileInputRef}
hidden
label="Room image"
type="file"
name="img"
accept="image/png, image/jpeg"
onChange={this.getBase64.bind(this)}
/>
</Form.Field>
2020-04-11 16:29:32 +00:00
{this.state.img ? (
2020-04-11 11:57:03 +00:00
<Button onClick={this.unsetImage}>Remove image</Button>
2020-04-11 16:29:32 +00:00
) : null}
2020-03-24 15:03:08 +00:00
</Form>
<div style={spaceDiv}>
<p>Select an icon:</p>
<SelectIcons
updateIcon={this.updateIcon}
currentIcon={
2020-05-12 13:18:33 +00:00
this.type === 'new' ? 'home' : this.props.room.icon
2020-03-24 15:03:08 +00:00
}
/>
</div>
2020-05-12 13:18:33 +00:00
{this.type === 'modify' ? (
2020-05-07 07:29:22 +00:00
<div>
<Button
icon
labelPosition="left"
inverted
color="red"
onClick={this.setSureTrue}
>
<Icon name="trash alternate" />
2020-05-12 13:18:33 +00:00
Delete Room
{' '}
</Button>
2020-05-07 07:29:22 +00:00
<Confirm
open={this.state.sure}
onCancel={this.setSureFalse}
onConfirm={this.deleteRoom}
/>
</div>
2020-03-24 15:03:08 +00:00
) : null}
</Modal.Content>
<Modal.Actions>
<Button color="red" onClick={this.closeModal}>
2020-05-12 13:18:33 +00:00
<Icon name="remove" />
{' '}
{this.type === 'new' ? 'Cancel' : 'Discard changes'}
2020-03-24 15:03:08 +00:00
</Button>
<Button
color="green"
onClick={
2020-05-12 13:18:33 +00:00
this.type === 'new' ? this.addRoomModal : this.modifyRoomModal
2020-03-24 15:03:08 +00:00
}
>
2020-05-12 13:18:33 +00:00
<Icon name="checkmark" />
{' '}
{this.type === 'new' ? 'Add room' : 'Save changes'}
2020-03-24 15:03:08 +00:00
</Button>
</Modal.Actions>
</Modal>
</div>
);
2020-03-13 14:21:33 +00:00
}
}
2020-04-11 11:57:03 +00:00
2020-05-12 13:18:33 +00:00
const setActiveRoom = (activeRoom) => (dispatch) => dispatch(appActions.setActiveRoom(activeRoom));
2020-04-11 11:57:03 +00:00
const mapStateToProps = (state, ownProps) => ({
2020-04-11 16:29:32 +00:00
room: ownProps.id ? state.rooms[ownProps.id] : null,
2020-04-11 11:57:03 +00:00
});
const RoomModalContainer = connect(
mapStateToProps,
{ ...RemoteService, setActiveRoom },
null,
2020-05-12 13:18:33 +00:00
{ forwardRef: true },
2020-04-11 11:57:03 +00:00
)(RoomModal);
export default RoomModalContainer;