diff --git a/client/homebrew/editor/metadataEditor/metadataEditor.jsx b/client/homebrew/editor/metadataEditor/metadataEditor.jsx index a14eb3f09..d713c1cc9 100644 --- a/client/homebrew/editor/metadataEditor/metadataEditor.jsx +++ b/client/homebrew/editor/metadataEditor/metadataEditor.jsx @@ -1,7 +1,6 @@ /* eslint-disable max-lines */ import '../uiEditor.less'; -import React from 'react'; -import createReactClass from 'create-react-class'; +import React, { useState } from 'react'; import _ from 'lodash'; import request from '../../utils/request-middleware.js'; import Combobox from '@components/combobox.jsx'; @@ -18,48 +17,35 @@ const callIfExists = (val, fn, ...args)=>{ } }; -const MetadataEditor = createReactClass({ - displayName : 'MetadataEditor', - getDefaultProps : function() { - return { - metadata : { - editId : null, - shareId : null, - title : '', - description : '', - thumbnail : '', - tags : [], - published : false, - authors : [], - renderer : 'legacy', - theme : '5ePHB', - lang : 'en' - }, - - onChange : ()=>{}, - reportError : ()=>{} - }; +const MetadataEditor = ({ + metadata = { + editId : null, + shareId : null, + title : '', + description : '', + thumbnail : '', + tags : [], + published : false, + authors : [], + renderer : 'legacy', + theme : '5ePHB', + lang : 'en', }, - getInitialState : function(){ - return { - isOwner : global.account?.username && global.account?.username === this.props.metadata?.authors[0], - showThumbnail : true - }; - }, + onChange = ()=>{}, + reportError = ()=>{}, + ...props +})=>{ + const [showThumbnail, setShowThumbnail] = useState(true); - toggleThumbnailDisplay : function(){ - this.setState({ - showThumbnail : !this.state.showThumbnail - }); - }, - - renderThumbnail : function(){ - if(!this.state.showThumbnail) return; - return ; - }, - - handleFieldChange : function(name, e){ + const toggleThumbnailDisplay = ()=>{ + setShowThumbnail(!showThumbnail); + }; + const renderThumbnail = ()=>{ + if(!showThumbnail) return; + return ; + }; + 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); @@ -72,8 +58,8 @@ const MetadataEditor = createReactClass({ // if no validation rules, save to props if(validationErr.length === 0){ callIfExists(e.target, 'setCustomValidity', ''); - this.props.onChange({ - ...this.props.metadata, + onChange({ + ...metadata, [name] : e.target.value }); return true; @@ -86,45 +72,39 @@ const MetadataEditor = createReactClass({ debouncedReportValidity(e.target, errMessage); return false; } - }, - - handleRenderer : function(renderer, e){ + }; + const handleRenderer = (renderer, e)=>{ if(e.target.checked){ - this.props.metadata.renderer = renderer; + metadata.renderer = renderer; if(renderer == 'legacy') - this.props.metadata.theme = '5ePHB'; + metadata.theme = '5ePHB'; } - this.props.onChange(this.props.metadata, 'renderer'); - }, - - handlePublish : function(val){ - this.props.onChange({ - ...this.props.metadata, + onChange(metadata, 'renderer'); + }; + const handlePublish = (val)=>{ + onChange({ + ...metadata, published : val }); - }, + }; + const handleTheme = (theme)=>{ + metadata.renderer = theme.renderer; + metadata.theme = theme.path; - handleTheme : function(theme){ - this.props.metadata.renderer = theme.renderer; - this.props.metadata.theme = theme.path; - - this.props.onChange(this.props.metadata, 'theme'); - }, - - handleThemeWritein : function(e) { + onChange(metadata, 'theme'); + }; + const handleThemeWritein = (e)=>{ const shareId = e.target.value.split('/').pop(); //Extract just the ID if a URL was pasted in - this.props.metadata.theme = shareId; + metadata.theme = shareId; - this.props.onChange(this.props.metadata, 'theme'); - }, - - handleLanguage : function(languageCode){ - this.props.metadata.lang = languageCode; - this.props.onChange(this.props.metadata); - }, - - handleDelete : function(){ - if(this.props.metadata.authors && this.props.metadata.authors.length <= 1){ + 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 { @@ -132,54 +112,51 @@ const MetadataEditor = createReactClass({ 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}`) + request.delete(`/api/${metadata.googleId ?? ''}${metadata.editId}`) .send() .end((err, res)=>{ if(err) { - this.props.reportError(err); + reportError(err); } else { window.location.href = '/'; } }); - }, - - handleDeleteAuthor : function(author){ + }; + 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(!this.props.metadata.authors.includes(author)) return; - this.props.onChange({ - ...this.props.metadata, - authors : this.props.metadata.authors.filter((a)=>a !== author) + if(!metadata.authors.includes(author)) return; + onChange({ + ...metadata, + authors : metadata.authors.filter((a)=>a !== author) }); - }, - - renderPublish : function(){ - if(this.props.metadata.published){ - return