/* eslint-disable max-lines */ require('./metadataEditor.less'); const React = require('react'); const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); const request = require('superagent'); const Nav = require('naturalcrit/nav/nav.jsx'); const StringArrayEditor = require('../stringArrayEditor/stringArrayEditor.jsx'); const Themes = require('themes/themes.json'); const validations = require('./validations.js') const SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder']; const homebreweryThumbnail = require('../../thumbnail.png'); const MetadataEditor = createClass({ displayName : 'MetadataEditor', getDefaultProps : function() { return { metadata : { editId : null, title : '', description : '', thumbnail : '', tags : [], published : false, authors : [], systems : [], renderer : 'legacy', theme : '5ePHB' }, onChange : ()=>{} }; }, getInitialState : function(){ return { showThumbnail : true }; }, toggleThumbnailDisplay : function(){ this.setState({ showThumbnail : !this.state.showThumbnail }); }, renderThumbnail : function(){ if(!this.state.showThumbnail) return; return ; }, handleFieldChange : function(name, e){ e.persist(); // load validation rules, and check input value against them const inputRules = validations[name] ?? []; const validationErr = inputRules.map((rule)=>rule(e.target.value)).filter(Boolean); // if no validation rules, save to props if(validationErr.length === 0){ e.target.setCustomValidity(''); this.props.onChange({ ...this.props.metadata, [name] : e.target.value }); } else { // if validation issues, display built-in browser error popup with each error. console.log(validationErr); const errMessage = validationErr.map((err)=>{ return `- ${err}`; }).join('\n'); e.target.setCustomValidity(errMessage); e.target.reportValidity(); }; }, handleSystem : function(system, e){ if(e.target.checked){ this.props.metadata.systems.push(system); } else { this.props.metadata.systems = _.without(this.props.metadata.systems, system); } this.props.onChange(this.props.metadata); }, handleRenderer : function(renderer, e){ if(e.target.checked){ this.props.metadata.renderer = renderer; if(renderer == 'legacy') this.props.metadata.theme = '5ePHB'; } this.props.onChange(this.props.metadata); }, handlePublish : function(val){ this.props.onChange({ ...this.props.metadata, published : val }); }, handleTheme : function(theme){ this.props.metadata.renderer = theme.renderer; this.props.metadata.theme = theme.path; this.props.onChange(this.props.metadata); }, handleDelete : function(){ if(this.props.metadata.authors && this.props.metadata.authors.length <= 1){ if(!confirm('Are you sure you want to delete this brew? Because you are the only owner of this brew, the document will be deleted permanently.')) return; if(!confirm('Are you REALLY sure? You will not be able to recover the document.')) return; } else { if(!confirm('Are you sure you want to remove this brew from your collection? This will remove you as an editor, but other owners will still be able to access the document.')) return; if(!confirm('Are you REALLY sure? You will lose editor access to this document.')) return; } request.delete(`/api/${this.props.metadata.googleId ?? ''}${this.props.metadata.editId}`) .send() .end(function(err, res){ window.location.href = '/'; }); }, renderSystems : function(){ return _.map(SYSTEMS, (val)=>{ return ; }); }, renderPublish : function(){ if(this.props.metadata.published){ return ; } else { return ; } }, renderDelete : function(){ if(!this.props.metadata.editId) return; return
; }, renderAuthors : function(){ let text = 'None.'; if(this.props.metadata.authors && this.props.metadata.authors.length){ text = this.props.metadata.authors.join(', '); } return
{text}
; }, renderThemeDropdown : function(){ if(!global.enable_themes) return; const listThemes = (renderer)=>{ return _.map(_.values(Themes[renderer]), (theme)=>{ return
this.handleTheme(theme)} title={''}> {`${theme.renderer} : ${theme.name}`}
; }); }; const currentTheme = Themes[`${_.upperFirst(this.props.metadata.renderer)}`][this.props.metadata.theme]; let dropdown; if(this.props.metadata.renderer == 'legacy') { dropdown =
{`Themes are not supported in the Legacy Renderer`}
; } else { dropdown =
{`${_.upperFirst(currentTheme.renderer)} : ${currentTheme.name}`}
{/*listThemes('Legacy')*/} {listThemes('V3')}
; } return
{dropdown}
; }, renderRenderOptions : function(){ if(!global.enable_v3) return; return
Click here to see the demo page for the old Legacy renderer!
; }, render : function(){ return
this.handleFieldChange('title', e)} />