2019-08-21 17:20:31 -04:00
|
|
|
import React, { Fragment } from 'react';
|
2019-08-14 16:54:00 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2019-08-21 17:20:31 -04:00
|
|
|
import { Redirect } from 'react-router-dom';
|
2019-08-14 16:54:00 -04:00
|
|
|
|
|
|
|
import ContainerHeader from './container-header';
|
2019-08-21 17:20:31 -04:00
|
|
|
import ErrorBox from '../components/error-box';
|
|
|
|
import InfoBox from '../components/info-box';
|
2019-10-15 09:37:23 -04:00
|
|
|
import { Building } from '../models/building';
|
|
|
|
import { User } from '../models/user';
|
2019-10-15 14:16:48 -04:00
|
|
|
import { compareObjects } from '../helpers';
|
2019-10-18 10:06:50 -04:00
|
|
|
import { CategoryViewProps, CopyProps } from './data-containers/category-view-props';
|
2019-10-15 09:37:23 -04:00
|
|
|
|
|
|
|
interface DataContainerProps {
|
|
|
|
title: string;
|
|
|
|
cat: string;
|
|
|
|
intro: string;
|
|
|
|
help: string;
|
|
|
|
inactive?: boolean;
|
|
|
|
|
|
|
|
user: User;
|
|
|
|
mode: 'view' | 'edit' | 'multi-edit';
|
|
|
|
building: Building;
|
|
|
|
building_like: boolean;
|
2019-10-15 14:16:48 -04:00
|
|
|
selectBuilding: (building: Building) => void
|
2019-10-15 09:37:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
interface DataContainerState {
|
|
|
|
error: string;
|
|
|
|
copying: boolean;
|
2019-10-15 10:44:22 -04:00
|
|
|
keys_to_copy: {[key: string]: boolean};
|
2019-10-15 14:16:48 -04:00
|
|
|
currentBuildingId: number;
|
|
|
|
buildingEdits: Partial<Building>;
|
2019-10-15 09:37:23 -04:00
|
|
|
}
|
2019-08-14 16:54:00 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shared functionality for view/edit forms
|
|
|
|
*
|
|
|
|
* See React Higher-order-component docs for the pattern
|
|
|
|
* - https://reactjs.org/docs/higher-order-components.html
|
|
|
|
*
|
|
|
|
* @param WrappedComponent
|
|
|
|
*/
|
2019-10-18 10:06:50 -04:00
|
|
|
const withCopyEdit = (WrappedComponent: React.ComponentType<CategoryViewProps>) => {
|
2019-10-17 12:07:34 -04:00
|
|
|
return class DataContainer extends React.Component<DataContainerProps, DataContainerState> {
|
2019-08-14 16:54:00 -04:00
|
|
|
static propTypes = { // TODO: generate propTypes from TS
|
|
|
|
title: PropTypes.string,
|
|
|
|
slug: PropTypes.string,
|
|
|
|
intro: PropTypes.string,
|
|
|
|
help: PropTypes.string,
|
|
|
|
inactive: PropTypes.bool,
|
|
|
|
children: PropTypes.node
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-08-21 17:20:31 -04:00
|
|
|
|
2019-08-14 16:54:00 -04:00
|
|
|
this.state = {
|
2019-10-15 09:37:23 -04:00
|
|
|
error: undefined,
|
2019-08-14 16:54:00 -04:00
|
|
|
copying: false,
|
2019-08-21 17:20:31 -04:00
|
|
|
keys_to_copy: {},
|
2019-10-15 14:16:48 -04:00
|
|
|
buildingEdits: {},
|
|
|
|
currentBuildingId: undefined
|
2019-08-14 16:54:00 -04:00
|
|
|
};
|
2019-08-21 17:20:31 -04:00
|
|
|
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
this.handleLike = this.handleLike.bind(this);
|
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
|
2019-08-14 16:54:00 -04:00
|
|
|
this.toggleCopying = this.toggleCopying.bind(this);
|
|
|
|
this.toggleCopyAttribute = this.toggleCopyAttribute.bind(this);
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:16:48 -04:00
|
|
|
static getDerivedStateFromProps(props, state) {
|
2019-10-16 08:11:25 -04:00
|
|
|
const newBuildingId = props.building == undefined ? undefined : props.building.building_id;
|
|
|
|
if(newBuildingId !== state.currentBuildingId) {
|
2019-10-15 14:16:48 -04:00
|
|
|
return {
|
2019-10-16 08:11:25 -04:00
|
|
|
error: undefined,
|
|
|
|
copying: false,
|
|
|
|
keys_to_copy: {},
|
2019-10-15 14:16:48 -04:00
|
|
|
buildingEdits: {},
|
2019-10-16 08:11:25 -04:00
|
|
|
currentBuildingId: newBuildingId
|
2019-10-15 14:16:48 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-14 16:54:00 -04:00
|
|
|
/**
|
|
|
|
* Enter or exit "copying" state - allow user to select attributes to copy
|
|
|
|
*/
|
|
|
|
toggleCopying() {
|
|
|
|
this.setState({
|
|
|
|
copying: !this.state.copying
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep track of data to copy (accumulate while in "copying" state)
|
|
|
|
*
|
|
|
|
* @param {string} key
|
|
|
|
*/
|
2019-08-21 17:20:31 -04:00
|
|
|
toggleCopyAttribute(key: string) {
|
2019-10-15 09:37:23 -04:00
|
|
|
const keys = {...this.state.keys_to_copy};
|
2019-08-21 17:20:31 -04:00
|
|
|
if(this.state.keys_to_copy[key]){
|
|
|
|
delete keys[key];
|
2019-08-14 16:54:00 -04:00
|
|
|
} else {
|
2019-08-21 17:20:31 -04:00
|
|
|
keys[key] = true;
|
2019-08-14 16:54:00 -04:00
|
|
|
}
|
|
|
|
this.setState({
|
2019-08-21 17:20:31 -04:00
|
|
|
keys_to_copy: keys
|
2019-08-14 16:54:00 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:16:48 -04:00
|
|
|
isEdited() {
|
|
|
|
const edits = this.state.buildingEdits;
|
|
|
|
// check if the edits object has any fields
|
|
|
|
return Object.entries(edits).length !== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getEditedBuilding() {
|
|
|
|
if(this.isEdited()) {
|
|
|
|
return Object.assign({}, this.props.building, this.state.buildingEdits);
|
|
|
|
} else {
|
|
|
|
return {...this.props.building};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBuildingState(key: string, value: any) {
|
|
|
|
const newBuilding = this.getEditedBuilding();
|
|
|
|
newBuilding[key] = value;
|
|
|
|
const [forwardPatch] = compareObjects(this.props.building, newBuilding);
|
2019-08-23 12:35:17 -04:00
|
|
|
|
|
|
|
this.setState({
|
2019-10-15 14:16:48 -04:00
|
|
|
buildingEdits: forwardPatch
|
2019-08-23 12:35:17 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:20:31 -04:00
|
|
|
/**
|
|
|
|
* Handle update directly
|
|
|
|
* - e.g. as callback from MultiTextInput where we set a list of strings
|
|
|
|
*
|
2019-08-23 12:46:22 -04:00
|
|
|
* @param {String} name
|
2019-08-21 17:20:31 -04:00
|
|
|
* @param {*} value
|
|
|
|
*/
|
2019-10-18 10:06:50 -04:00
|
|
|
handleChange(name: string, value: any) {
|
2019-08-23 12:35:17 -04:00
|
|
|
this.updateBuildingState(name, value);
|
2019-08-21 17:20:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle likes separately
|
|
|
|
* - like/love reaction is limited to set/unset per user
|
|
|
|
*
|
2019-08-23 12:35:17 -04:00
|
|
|
* @param {*} event
|
2019-08-21 17:20:31 -04:00
|
|
|
*/
|
2019-10-18 10:06:50 -04:00
|
|
|
async handleLike(like: boolean) {
|
|
|
|
try {
|
|
|
|
const res = await fetch(`/api/buildings/${this.props.building.building_id}/like.json`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers:{
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
credentials: 'same-origin',
|
|
|
|
body: JSON.stringify({like: like})
|
|
|
|
});
|
|
|
|
const data = await res.json();
|
|
|
|
|
|
|
|
if (data.error) {
|
|
|
|
this.setState({error: data.error})
|
2019-08-21 17:20:31 -04:00
|
|
|
} else {
|
2019-10-18 10:06:50 -04:00
|
|
|
this.props.selectBuilding(data);
|
|
|
|
this.updateBuildingState('likes_total', data.likes_total);
|
2019-08-21 17:20:31 -04:00
|
|
|
}
|
2019-10-18 10:06:50 -04:00
|
|
|
} catch(err) {
|
|
|
|
this.setState({error: err});
|
|
|
|
}
|
2019-08-21 17:20:31 -04:00
|
|
|
}
|
|
|
|
|
2019-10-15 14:16:48 -04:00
|
|
|
async handleSubmit(event) {
|
2019-08-21 17:20:31 -04:00
|
|
|
event.preventDefault();
|
2019-10-15 14:16:48 -04:00
|
|
|
this.setState({error: undefined});
|
2019-08-21 17:20:31 -04:00
|
|
|
|
2019-10-15 14:16:48 -04:00
|
|
|
try {
|
|
|
|
const res = await fetch(`/api/buildings/${this.props.building.building_id}.json`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(this.state.buildingEdits),
|
|
|
|
headers:{
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
credentials: 'same-origin'
|
|
|
|
});
|
|
|
|
const data = await res.json();
|
|
|
|
|
|
|
|
if (data.error) {
|
|
|
|
this.setState({error: data.error})
|
2019-08-21 17:20:31 -04:00
|
|
|
} else {
|
2019-10-15 14:16:48 -04:00
|
|
|
this.props.selectBuilding(data);
|
2019-08-21 17:20:31 -04:00
|
|
|
}
|
2019-10-15 14:16:48 -04:00
|
|
|
} catch(err) {
|
|
|
|
this.setState({error: err});
|
|
|
|
}
|
2019-08-21 17:20:31 -04:00
|
|
|
}
|
|
|
|
|
2019-08-14 16:54:00 -04:00
|
|
|
render() {
|
2019-10-15 09:37:23 -04:00
|
|
|
if (this.props.mode === 'edit' && !this.props.user){
|
2019-08-21 17:20:31 -04:00
|
|
|
return <Redirect to="/sign-up.html" />
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:16:48 -04:00
|
|
|
const currentBuilding = this.getEditedBuilding();
|
|
|
|
|
2019-08-21 17:20:31 -04:00
|
|
|
const values_to_copy = {}
|
|
|
|
for (const key of Object.keys(this.state.keys_to_copy)) {
|
2019-10-15 14:16:48 -04:00
|
|
|
values_to_copy[key] = currentBuilding[key]
|
2019-08-21 17:20:31 -04:00
|
|
|
}
|
|
|
|
const data_string = JSON.stringify(values_to_copy);
|
2019-10-15 10:44:22 -04:00
|
|
|
const copy: CopyProps = {
|
2019-08-14 16:54:00 -04:00
|
|
|
copying: this.state.copying,
|
|
|
|
toggleCopying: this.toggleCopying,
|
|
|
|
toggleCopyAttribute: this.toggleCopyAttribute,
|
2019-10-15 10:44:22 -04:00
|
|
|
copyingKey: (key: string) => this.state.keys_to_copy[key]
|
2019-10-18 10:06:50 -04:00
|
|
|
};
|
2019-09-08 20:10:52 -04:00
|
|
|
return (
|
2019-08-21 17:20:31 -04:00
|
|
|
<section
|
2019-10-15 09:37:23 -04:00
|
|
|
id={this.props.cat}
|
2019-08-21 17:20:31 -04:00
|
|
|
className="data-section">
|
2019-08-23 12:35:17 -04:00
|
|
|
<ContainerHeader
|
|
|
|
{...this.props}
|
|
|
|
data_string={data_string}
|
|
|
|
copy={copy}
|
2019-09-08 20:10:52 -04:00
|
|
|
/>
|
2019-10-15 09:37:23 -04:00
|
|
|
<div className="section-body">
|
2019-09-08 20:10:52 -04:00
|
|
|
{
|
2019-10-15 09:37:23 -04:00
|
|
|
this.props.inactive ?
|
|
|
|
<Fragment>
|
2019-10-02 10:15:13 -04:00
|
|
|
<InfoBox
|
|
|
|
msg={`We're not collecting data on ${this.props.title.toLowerCase()} yet - check back soon.`}
|
|
|
|
/>
|
|
|
|
<WrappedComponent
|
2019-10-18 10:06:50 -04:00
|
|
|
intro={this.props.intro}
|
2019-10-02 10:15:13 -04:00
|
|
|
building={undefined}
|
|
|
|
building_like={undefined}
|
|
|
|
mode={this.props.mode}
|
|
|
|
copy={copy}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onLike={this.handleLike}
|
|
|
|
/>
|
2019-10-15 09:37:23 -04:00
|
|
|
</Fragment> :
|
|
|
|
this.props.building != undefined ?
|
|
|
|
<form
|
|
|
|
action={`/edit/${this.props.cat}/${this.props.building.building_id}`}
|
|
|
|
method="POST"
|
|
|
|
onSubmit={this.handleSubmit}>
|
|
|
|
{
|
|
|
|
(this.props.mode === 'edit' && !this.props.inactive) ?
|
|
|
|
<Fragment>
|
|
|
|
<ErrorBox msg={this.state.error} />
|
|
|
|
{
|
2019-10-15 14:16:48 -04:00
|
|
|
this.isEdited() && this.props.cat !== 'like' ? // special-case for likes
|
2019-10-15 09:37:23 -04:00
|
|
|
<div className="buttons-container with-space">
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-primary">
|
|
|
|
Save
|
|
|
|
</button>
|
2019-10-15 14:16:48 -04:00
|
|
|
</div> :
|
|
|
|
null
|
2019-10-15 09:37:23 -04:00
|
|
|
}
|
|
|
|
</Fragment>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
<WrappedComponent
|
2019-10-18 10:06:50 -04:00
|
|
|
intro={this.props.intro}
|
2019-10-15 14:16:48 -04:00
|
|
|
building={currentBuilding}
|
2019-10-15 09:37:23 -04:00
|
|
|
building_like={this.props.building_like}
|
|
|
|
mode={this.props.mode}
|
|
|
|
copy={copy}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onLike={this.handleLike}
|
|
|
|
/>
|
|
|
|
</form> :
|
2019-10-02 10:15:13 -04:00
|
|
|
<InfoBox msg="Select a building to view data"></InfoBox>
|
2019-09-08 20:10:52 -04:00
|
|
|
}
|
2019-10-15 09:37:23 -04:00
|
|
|
</div>
|
2019-08-21 17:20:31 -04:00
|
|
|
</section>
|
2019-09-08 20:10:52 -04:00
|
|
|
);
|
2019-08-14 16:54:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:06:50 -04:00
|
|
|
export default withCopyEdit;
|