import React from "react" ; import { Menu, MenuList, MenuButton, MenuItem } from "@reach/menu-button" ; import { PublisherSearchResult2 } from "./PublisherSearchResult2.js" import "./PublisherSearchResult.css" ; import { gAppRef } from "./index.js" ; import { pluralString, applyUpdatedVals, removeSpecialFields } from "./utils.js" ; const axios = require( "axios" ) ; // -------------------------------------------------------------------- export class PublisherSearchResult extends React.Component { render() { const display_name = this.props.data[ "publ_name!" ] || this.props.data.publ_name ; const display_description = this.props.data[ "publ_description!" ] || this.props.data.publ_description ; const image_url = gAppRef.makeFlaskImageUrl( "publisher", this.props.data.publ_image_id, true ) ; const menu = ( Edit Delete ) ; return (
gAppRef.setTestAttribute( r, "publ_id", this.props.data.publ_id ) } >
{menu} { this.props.data.publ_url && Open publisher. }
{ image_url && Publisher. }
) ; } static onNewPublisher( notify ) { PublisherSearchResult2._doEditPublisher( {}, (newVals,refs) => { axios.post( gAppRef.makeFlaskUrl( "/publisher/create", {list:1} ), newVals ) .then( resp => { // update the cached publishers gAppRef.caches.publishers = resp.data.publishers ; // unload any updated values applyUpdatedVals( newVals, newVals, resp.data.updated, refs ) ; // update the UI with the new details notify( resp.data.publ_id, newVals ) ; 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() { 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", {list:1} ), newVals ) .then( resp => { // update the cached publishers gAppRef.caches.publishers = resp.data.publishers ; // update the UI with the new details applyUpdatedVals( this.props.data, newVals, resp.data.updated, refs ) ; removeSpecialFields( this.props.data ) ; this.forceUpdate() ; 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, {list:1} ) ) .then( resp => { // update the cached publishers gAppRef.caches.publishers = resp.data.publishers ; gAppRef.caches.publications = resp.data.publications ; // nb: because of cascading deletes // 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 ) ; } ) ; resp.data.deletedArticles.forEach( article_id => { this.props.onDelete( "article_id", article_id ) ; } ) ; 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 ) ; } ) ; } }