import React from "react" ; import ReactDOMServer from "react-dom/server" ; import Select from "react-select" ; import CreatableSelect from "react-select/creatable" ; import { gAppRef } from "./index.js" ; import { ImageFileUploader } from "./FileUploader.js" ; import { makeOptionalLink, unloadCreatableSelect, pluralString, applyUpdatedVals } from "./utils.js" ; const axios = require( "axios" ) ; // -------------------------------------------------------------------- export class PublicationSearchResult extends React.Component { render() { const publ = gAppRef.caches.publishers[ this.props.data.publ_id ] ; let tags = [] ; if ( this.props.data.pub_tags ) this.props.data.pub_tags.map( t => tags.push(
{t}
) ) ; return (
gAppRef.setTestAttribute( r, "pub_id", this.props.data.pub_id ) } >
{ makeOptionalLink( this._makeDisplayName(), this.props.data.pub_url ) } { publ && ({publ.publ_name}) } Edit this publication. Delete this publication.
{ tags.length > 0 &&
{tags}
}
) ; } static onNewPublication( notify ) { PublicationSearchResult._doEditPublication( {}, (newVals,refs) => { axios.post( gAppRef.makeFlaskUrl( "/publication/create", {list:1} ), newVals ) .then( resp => { // update the caches gAppRef.caches.publications = resp.data.publications ; gAppRef.caches.tags = resp.data.tags ; // unload any updated values applyUpdatedVals( newVals, newVals, resp.data.updated, refs ) ; // update the UI with the new details notify( resp.data.pub_id, newVals ) ; if ( resp.data.warnings ) gAppRef.showWarnings( "The new publication was created OK.", resp.data.warnings ) ; else gAppRef.showInfoToast(
The new publication was created OK.
) ; gAppRef.closeModalForm() ; } ) .catch( err => { gAppRef.showErrorMsg(
Couldn't create the publication:
{err.toString()}
) ; } ) ; } ) ; } onEditPublication() { PublicationSearchResult._doEditPublication( this.props.data, (newVals,refs) => { // send the updated details to the server newVals.pub_id = this.props.data.pub_id ; axios.post( gAppRef.makeFlaskUrl( "/publication/update", {list:1} ), newVals ) .then( resp => { // update the caches gAppRef.caches.publications = resp.data.publications ; gAppRef.caches.tags = resp.data.tags ; // update the UI with the new details applyUpdatedVals( this.props.data, newVals, resp.data.updated, refs ) ; this.forceUpdate() ; if ( resp.data.warnings ) gAppRef.showWarnings( "The publication was updated OK.", resp.data.warnings ) ; else gAppRef.showInfoToast(
The publication was updated OK.
) ; gAppRef.closeModalForm() ; } ) .catch( err => { gAppRef.showErrorMsg(
Couldn't update the publication:
{err.toString()}
) ; } ) ; } ); } static _doEditPublication( vals, notify ) { let refs = {} ; // initialize the image let imageFilename=null, imageData=null ; let imageRef=null, uploadImageRef=null, removeImageRef=null ; let imageUrl = gAppRef.makeFlaskUrl( "/images/publication/" + vals.pub_id ) ; imageUrl += "?foo=" + Math.random() ; // FUDGE! To bypass the cache :-/ let onMissingImage = (evt) => { imageRef.src = "/images/placeholder.png" ; removeImageRef.style.display = "none" ; } ; let onUploadImage = (evt) => { if ( evt === null && !gAppRef.isFakeUploads() ) { // nb: the publication image was clicked - trigger an upload request uploadImageRef.click() ; return ; } let fileUploader = new ImageFileUploader() ; fileUploader.getFile( evt, imageRef, removeImageRef, (fname,data) => { imageFilename = fname ; imageData = data ; } ) ; } ; let onRemoveImage = () => { imageData = "{remove}" ; imageRef.src = "/images/placeholder.png" ; removeImageRef.style.display = "none" ; } ; // initialize the publishers let publishers = [ { value: null, label: (none) } ] ; let currPubl = 0 ; for ( let p of Object.entries(gAppRef.caches.publishers) ) { publishers.push( { value: p[1].publ_id, label: } ) ; if ( p[1].publ_id === vals.publ_id ) currPubl = publishers.length - 1 ; } publishers.sort( (lhs,rhs) => { return ReactDOMServer.renderToStaticMarkup( lhs.label ).localeCompare( ReactDOMServer.renderToStaticMarkup( rhs.label ) ) ; } ) ; // initialize the tags const tags = gAppRef.makeTagLists( vals.pub_tags ) ; // prepare the form content /* eslint-disable jsx-a11y/img-redundant-alt */ const content =
onUploadImage(null)} ref={r => imageRef=r} alt="Click to upload an image for this publication." /> removeImageRef=r} alt="Remove the publication's image." /> uploadImageRef=r} />
refs.pub_name=r} />
refs.pub_edition=r} />