import React from "react" ; import { Link } from "react-router-dom" ; import { Menu, MenuList, MenuButton, MenuItem } from "@reach/menu-button" ; import { PublisherSearchResult2 } from "./PublisherSearchResult2.js" import "./PublisherSearchResult.css" ; import { PublicationSearchResult } from "./PublicationSearchResult.js" import { PreviewableImage } from "./PreviewableImage.js" ; import { PUBLISHER_EXCESS_PUBLICATION_THRESHOLD, PUBLISHER_EXCESS_ARTICLE_THRESHOLD } from "./constants.js" ; import { gAppRef } from "./App.js" ; import { makeCollapsibleList, pluralString, updateRecord } from "./utils.js" ; const axios = require( "axios" ) ; // -------------------------------------------------------------------- export class PublisherSearchResult extends React.Component { render() { // prepare the basic details const display_name = this._makeDisplayName() ; const display_description = PreviewableImage.adjustHtmlForPreviewableImages( this.props.data[ "publ_description!" ] || this.props.data.publ_description ) ; const image_url = gAppRef.makeFlaskImageUrl( "publisher", this.props.data.publ_image_id ) ; // prepare the publisher's URL let publ_url = this.props.data.publ_url ; if ( publ_url ) publ_url = gAppRef.makeExternalDocUrl( publ_url ) ; // prepare the publications let pubs = this.props.data.publications ; pubs.sort( (lhs,rhs) => { if ( lhs.pub_seqno && rhs.pub_seqno ) return rhs.pub_seqno - lhs.pub_seqno ; else if ( lhs.pub_seqno ) return +1 ; else if ( rhs.pub_seqno ) return -1 ; else return rhs.time_created - lhs.time_created ; // nb: we compare timestamps for back-compat } ) ; pubs = pubs.map( p => ) ; // prepare any associated articles let articles = [] ; if ( this.props.data.articles ) { articles = this.props.data.articles.map( a => ) ; articles.reverse() ; } // prepare the menu const menu = ( this.onEditPublisher() } > Edit. Edit this.onDeletePublisher() } > Delete. Delete ) ; return (
gAppRef.setTestAttribute( r, "publ_id", this.props.data.publ_id ) } >
{menu} { publ_url && Open publisher. }
{ image_url && }
{ makeCollapsibleList( "Publications", pubs, PUBLISHER_EXCESS_PUBLICATION_THRESHOLD, {float:"left"} ) } { makeCollapsibleList( "Articles", articles, PUBLISHER_EXCESS_ARTICLE_THRESHOLD, {clear:"both",float:"left"} ) }
) ; } componentDidMount() { PreviewableImage.activatePreviewableImages( this ) ; } static onNewPublisher() { gAppRef.dataCache.get( [ "publishers", "publications" ], () => { PublisherSearchResult2._doEditPublisher( {}, (newVals,refs) => { axios.post( gAppRef.makeFlaskUrl( "/publisher/create" ), newVals ).then( resp => { gAppRef.dataCache.refresh( [ "publishers" ] ) ; // update the UI const newPubl = resp.data.record ; gAppRef.prependSearchResult( newPubl ) ; // update the UI if ( resp.data.warnings ) gAppRef.showWarnings( "The new publisher was created OK.", resp.data.warnings ) ; else gAppRef.showInfoToast(
The new publisher was created OK.
) ; gAppRef.closeModalForm() ; } ).catch( err => { gAppRef.showErrorMsg(
Couldn't create the publisher:
{err.toString()}
) ; } ) ; } ) ; } ) ; } onEditPublisher() { gAppRef.dataCache.get( [ "publishers", "publications" ], () => { PublisherSearchResult2._doEditPublisher( this.props.data, (newVals,refs) => { // send the updated details to the server newVals.publ_id = this.props.data.publ_id ; axios.post( gAppRef.makeFlaskUrl( "/publisher/update" ), newVals ).then( resp => { gAppRef.dataCache.refresh( [ "publishers" ], () => { // update the UI const publ = resp.data.record ; updateRecord( this.props.data, publ ) ; for ( let pub of publ.publications ) gAppRef.updatePublication( pub.pub_id ) ; // update the UI if ( newVals.imageData ) gAppRef.forceFlaskImageReload( "publisher", newVals.publ_id ) ; this.forceUpdate() ; PreviewableImage.activatePreviewableImages( this ) ; // update the UI if ( resp.data.warnings ) gAppRef.showWarnings( "The publisher was updated OK.", resp.data.warnings ) ; else gAppRef.showInfoToast(
The publisher was updated OK.
) ; gAppRef.closeModalForm() ; } ) ; } ).catch( err => { gAppRef.showErrorMsg(
Couldn't update the publisher:
{err.toString()}
) ; } ) ; } ); } ) ; } onDeletePublisher() { let doDelete = ( nPubs, nArticles ) => { // confirm the operation let warning ; if ( typeof nPubs !== "number" ) { // something went wrong when getting the number of associated publications/articles // (we can continue, but we warn the user) warning = (
Error. WARNING: Couldn't check if any publications or articles will also be deleted:
{nPubs.toString()}
) ; } else if ( nPubs === 0 && nArticles === 0 ) warning =
No publications nor articles will be deleted.
; else { let vals = [] ; if ( nPubs > 0 ) vals.push( pluralString( nPubs, "publication" ) ) ; if ( nArticles > 0 ) vals.push( pluralString( nArticles, "article" ) ) ; warning =
{warning} { vals.join(" and ") + " will also be deleted." }
; } let content = (
Delete this publisher?
{warning}
) ; gAppRef.ask( content, "ask", { "OK": () => { // delete the publisher on the server axios.get( gAppRef.makeFlaskUrl( "/publisher/delete/" + this.props.data.publ_id ) ).then( resp => { gAppRef.dataCache.refresh( [ "publishers", "publications" ] ) ; // update the UI this.props.onDelete( "publ_id", this.props.data.publ_id ) ; resp.data.deletedPublications.forEach( pub_id => { this.props.onDelete( "pub_id", pub_id, true ) ; } ) ; resp.data.deletedArticles.forEach( article_id => { this.props.onDelete( "article_id", article_id, true ) ; } ) ; // update the UI if ( resp.data.warnings ) gAppRef.showWarnings( "The publisher was deleted.", resp.data.warnings ) ; else gAppRef.showInfoToast(
The publisher was deleted.
) ; } ).catch( err => { gAppRef.showErrorToast(
Couldn't delete the publisher:
{err.toString()}
) ; } ) ; }, "Cancel": null, } ) ; } ; // get the publisher details axios.get( gAppRef.makeFlaskUrl( "/publisher/" + this.props.data.publ_id ) ).then( resp => { doDelete( resp.data.nPublications, resp.data.nArticles ) ; } ).catch( err => { doDelete( err ) ; } ) ; } static makeDisplayName( vals ) { // return the publisher's display name return vals["publ_name!"] || vals.publ_name ; } _makeDisplayName() { return PublisherSearchResult.makeDisplayName( this.props.data ) ; } }