/* Usage: <%= react_component("PageLookupSidebar", {document_id: @document.id}) %> */ import React from "react" import PropTypes from "prop-types" import Drawer from '@material-ui/core/Drawer'; import List from '@material-ui/core/List'; import Divider from '@material-ui/core/Divider'; import ListItem from '@material-ui/core/ListItem'; import ListItemText from '@material-ui/core/ListItemText'; import ListSubheader from '@material-ui/core/ListSubheader'; import Chip from '@material-ui/core/Chip'; import SimpleFormat from './SimpleFormat.js'; import ExpandLess from '@material-ui/icons/ExpandLess'; import ExpandMore from '@material-ui/icons/ExpandMore'; import Collapse from '@material-ui/core/Collapse'; var pluralize = require('pluralize'); import axios from 'axios'; class PageLookupSidebar extends React.Component { constructor(props) { super(props); this.state = { open: false, loading: false, show_data: false, category_open: {} }; } classIcon(class_name) { return window.ContentTypeData[class_name].icon; } classColor(class_name) { return window.ContentTypeData[class_name].color; } async loadPage(page_type, page_id) { this.setDrawerVisible(true); this.setState({ show_data: false, category_open: {} }); // make api request await axios.get( "/api/v1/" + page_type.toLowerCase() + "/" + page_id + '?application_token=4756de490e82956dc6329e6650aaec664e27ccd27e153e2f' + '&authorization_token=167bb93139303904cf67f6480a29e71c9f1eaf7a28e902e1', { headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } } ).then(response => { // load response into list this.setState({ page_data: response.data, show_data: !response.data.hasOwnProperty('error'), page_type: page_type, page_id: page_id }); }).catch(err => { console.log(err); this.setDrawerVisible(false); return null; }); }; content_page_view_path(content_type, content_id) { return '/plan/' + pluralize(content_type.toLowerCase()) + '/' + content_id; }; content_page_edit_path(content_type, content_id) { return '/plan/' + pluralize(content_type.toLowerCase()) + '/' + content_id + '/edit'; }; content_page_tag_path(content_type, tag_slug) { return '/plan/' + pluralize(content_type.toLowerCase()) + '/tagged/' + tag_slug; } link_destroy_path(document_id, page_type, page_id) { return '/documents/' + document_id + '/unlink/' + page_type + '/' + page_id; } setDrawerVisible(open) { this.setState({ open: open }); }; toggleCategoryOpen(category) { this.setState({ category_open: { [category]: !this.state.category_open[category] }}); }; fieldData(field) { switch (field.type) { case "name": case "text_area": // console.log(field.value); return ( {field.label} ); case "link": return( { pluralize(field.label, field.value.length, true) } {field.value.map((link) => ( { this.loadPage(link.page_type, link.id); }}> {this.classIcon(link.page_type)} { link.name } } /> ))} ); case "universe": // console.log(field.value); return( Universe { this.loadPage('Universe', field.id); }}> {this.classIcon('Universe')} { this.state.page_data.universe.name } } /> ); case "tags": return( Tags {field.value.map((tag) => { return( ); })} ); case "TimelineEvent": return(
schedule { field.time_label }
{field.label}
) default: return(
error loading {field.type}: {field.label}
); } } pageData() { if (this.state.show_data) { return (
  {this.classIcon(this.state.page_type)}    {this.state.page_data.name} } id="page-lookup-list" > Quick-reference {this.state.page_data.categories.map((category) => ( this.toggleCategoryOpen(category.label)}> { category.icon } {!!this.state.category_open[category.label] ? : } {category.fields.map((field) => ( this.fieldData(field) ))} ))} { this.classIcon(this.state.page_type) } { this.classIcon(this.state.page_type) } remove
); // also add divider + view/edit links } else { // No data to show yet return (
Quick-reference } >
); } }; render () { return ( this.setDrawerVisible(false)} > {this.pageData()} ) } } PageLookupSidebar.propTypes = { document_id: PropTypes.number }; export default PageLookupSidebar;