/* eslint-disable max-lines */ import '../uiEditor.less'; import React, { useState } from 'react'; import _ from 'lodash'; import request from '../../utils/request-middleware.js'; import Themes from '@themes/themes.json'; import validations from './validations.js'; import Combobox from '@components/combobox.jsx'; import TagInput from '../tagInput/tagInput.jsx'; import homebreweryThumbnail from '../../thumbnail.png'; const callIfExists = (val, fn, ...args)=>{ if(val[fn]) { val[fn](...args); } }; const MetadataEditor = ({ metadata = { shareId : null, editId : null, title : '', description : '', tags : [], lang : 'en', renderer : 'legacy', authors : [], published : false, thumbnail : '', theme : '5ePHB', }, onChange = ()=>{}, reportError = ()=>{}, userThemes = {}, themeBundle = {} })=>{ const [showThumbnail, setShowThumbnail] = useState(true); const handleFieldChange = (name, e)=>{ // load validation rules, and check input value against them const inputRules = validations[name] ?? []; const validationErr = inputRules.map((rule)=>rule(e.target.value)).filter(Boolean); const debouncedReportValidity = _.debounce((target, errMessage)=>{ callIfExists(target, 'setCustomValidity', errMessage); callIfExists(target, 'reportValidity'); }, 300); // 300ms debounce delay, adjust as needed // if no validation rules, save to props if(validationErr.length === 0){ callIfExists(e.target, 'setCustomValidity', ''); onChange({ ...metadata, [name] : e.target.value }); return true; } else { // if validation issues, display built-in browser error popup with each error. const errMessage = validationErr.map((err)=>{ return `- ${err}`; }).join('\n'); debouncedReportValidity(e.target, errMessage); return false; } }; const handleRenderer = (renderer, e)=>{ if(e.target.checked){ metadata.renderer = renderer; if(renderer == 'legacy') metadata.theme = '5ePHB'; } onChange(metadata, 'renderer'); }; const handlePublish = (val)=>{ onChange({ ...metadata, published : val }); }; const handleTheme = (theme)=>{ metadata.renderer = theme.renderer; metadata.theme = theme.path; onChange(metadata, 'theme'); }; const handleThemeWritein = (e)=>{ const shareId = e.target.value.split('/').pop(); //Extract just the ID if a URL was pasted in metadata.theme = shareId; onChange(metadata, 'theme'); }; const handleLanguage = (languageCode)=>{ metadata.lang = languageCode; onChange(metadata); }; const handleDelete = ()=>{ if(metadata.authors && 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/${metadata.googleId ?? ''}${metadata.editId}`) .send() .end((err, res)=>{ if(err) { reportError(err); } else { window.location.href = '/'; } }); }; const handleDeleteAuthor = (author)=>{ if(!confirm('Are you sure you want to remove this author? They will lose all edit access to this brew, and it will dissapear from their userpage.')) return; if(!metadata.authors.includes(author)) return; onChange({ ...metadata, authors : metadata.authors.filter((a)=>a !== author) }); }; const renderPublish = ()=>{ if(metadata.published){ return