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 = ( < Menu >
< MenuButton className = "sr-menu" / >
< MenuList >
< MenuItem className = "edit"
onSelect = { this . onEditPublisher . bind ( this ) }
> Edit < / M e n u I t e m >
< MenuItem className = "delete"
onSelect = { this . onDeletePublisher . bind ( this ) }
> Delete < / M e n u I t e m >
< / M e n u L i s t >
< / M e n u > ) ;
return ( < div className = "search-result publisher"
ref = { r => gAppRef . setTestAttribute ( r , "publ_id" , this . props . data . publ _id ) }
>
< div className = "header" >
{ menu }
< span className = "name" dangerouslySetInnerHTML = { { _ _html : display _name } } / >
{ this . props . data . publ _url && < a href = { this . props . data . publ _url } className = "open-link" target = "_blank" rel = "noopener noreferrer" > < img src = "/images/open-link.png" alt = "Open publisher." title = "Go to this publisher." / > < / a > }
< / d i v >
< div className = "content" >
{ image _url && < img src = { image _url } className = "image" alt = "Publisher." / > }
< div className = "description" dangerouslySetInnerHTML = { { _ _html : display _description } } / >
< / d i v >
< / d i v > ) ;
}
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 ( < div > The new publisher was created OK . < / d i v > ) ;
gAppRef . closeModalForm ( ) ;
} )
. catch ( err => {
gAppRef . showErrorMsg ( < div > Couldn ' t create the publisher : < div className = "monospace" > { err . toString ( ) } < / d i v > < / d i v > ) ;
} ) ;
} ) ;
}
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 ( < div > The publisher was updated OK . < / d i v > ) ;
gAppRef . closeModalForm ( ) ;
} )
. catch ( err => {
gAppRef . showErrorMsg ( < div > Couldn ' t update the publisher : < div className = "monospace" > { err . toString ( ) } < / d i v > < / d i v > ) ;
} ) ;
} ) ;
}
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 = ( < div > < img className = "icon" src = "/images/error.png" alt = "Error." / >
WARNING : Couldn ' t check if any publications or articles will also be deleted :
< div className = "monospace" > { nPubs . toString ( ) } < / d i v >
< / d i v > ) ;
} else if ( nPubs === 0 && nArticles === 0 )
warning = < div > No publications nor articles will be deleted . < / d i v > ;
else {
let vals = [ ] ;
if ( nPubs > 0 )
vals . push ( pluralString ( nPubs , "publication" ) ) ;
if ( nArticles > 0 )
vals . push ( pluralString ( nArticles , "article" ) ) ;
warning = < div > { warning } { vals . join ( " and " ) + " will also be deleted." } < / d i v > ;
}
let content = ( < div >
Delete this publisher ?
< div style = { { margin : "0.5em 0 0.5em 2em" , fontStyle : "italic" } } dangerouslySetInnerHTML = { { _ _html : this . props . data . publ _name } } / >
{ warning }
< / d i v > ) ;
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 ( < div > The publisher was deleted . < / d i v > ) ;
} )
. catch ( err => {
gAppRef . showErrorToast ( < div > Couldn ' t delete the publisher : < div className = "monospace" > { err . toString ( ) } < / d i v > < / d i v > ) ;
} ) ;
} ,
"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 ) ;
} ) ;
}
}