import React, { PropTypes } from 'react'; import cx from 'classnames'; import withStyles from 'isomorphic-style-loader/lib/withStyles'; import { MdCheck, MdError } from 'react-icons/lib/md'; import s from './RsvpCard.css'; import { starters, mains } from '../../config'; class RsvpCard extends React.Component { static propTypes = { focusCallback: PropTypes.func, className: PropTypes.string, person: PropTypes.shape({ key: PropTypes.string, firstname: PropTypes.string, lastname: PropTypes.string, email: PropTypes.string, password: PropTypes.string, completed: PropTypes.boolean, attending: PropTypes.boolean, ceremony: PropTypes.boolean, dietary: PropTypes.string, starter: PropTypes.string, main: PropTypes.string, }).isRequired, isEditable: PropTypes.bool.isRequired, }; constructor(props) { super(props); this.state = { detailsVisible: this.props.person.attending, saveAttending: this.props.person.attending ? 'saved' : null, saveStarter: this.props.person.starter !== '-1' ? 'saved' : null, saveMain: this.props.person.main !== '-1' ? 'saved' : null, saveDietary: this.props.person.dietary ? 'saved' : null, }; if (!this.props.isEditable) { this.state.summaryState = 'readonly'; this.state.summary = 'Sorry, you can no longer change your RSVP.'; } } isRSVPComplete(prop, value) { function isOK(str) { return (str === 'saved' || str === 'success'); } if (prop === 'attending' && value === 'false') { return true; } if (!this.props.person.ceremony) { return true; } return (isOK(this.state.saveAttending) && isOK(this.state.saveStarter) && isOK(this.state.saveMain)); } saveData(prop, value) { const stateKey = `save${prop.charAt(0).toUpperCase()}${prop.slice(1)}`; const state = {}; fetch('/rsvp/save', { method: 'post', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ key: this.props.person.key, prop, value, }), credentials: 'include', }).then(res => res.json()) .then(data => { state[stateKey] = data.success ? 'success' : 'error'; this.setState(state); if (this.isRSVPComplete(prop, value)) { this.setState({ summaryState: 'success', summary: `Thanks ${this.props.person.firstname}, your RSVP has been received`, }); if (!this.state.saveCompleted) { this.setState({ saveCompleted: true }); this.saveData('completed', true); } } state[stateKey] = data.success ? 'saved' : 'error'; setTimeout(() => { this.setState(state); }, 1000); }).catch(() => { state[stateKey] = 'error'; }); } handleAttendingChange = (event) => { this.saveData('attending', event.target.value); this.setState({ detailsVisible: event.target.value === 'true', attendingResponse: event.target.value === 'true' ? 'đ' : 'âšī¸', }); }; handleDietaryBlur = (event) => { this.saveData('dietary', event.target.value); }; handleDietaryChange = (event) => { const value = event.target.value; clearTimeout(this.dietaryChangeTimer); this.dietaryChangeTimer = setTimeout(() => { this.saveData('dietary', value); }, 5000); } handleStarterChange = (event) => { this.saveData('starter', event.target.value); }; handleMainChange = (event) => { this.saveData('main', event.target.value); }; handleFormSubmit = (event) => { event.preventDefault(); }; render() { return (