import React from "react" ; import Select from "react-select" ; import CreatableSelect from "react-select/creatable" ; import { NEW_ARTICLE_PUB_PRIORITY_CUTOFF } from "./constants.js" ; import { PublicationSearchResult } from "./PublicationSearchResult.js" ; import { gAppRef } from "./App.js" ; import { ImageFileUploader } from "./FileUploader.js" ; import { makeScenarioDisplayName, parseScenarioDisplayName, checkConstraints, confirmDiscardChanges, sortSelectableOptions, unloadCreatableSelect, makeTagLists, isNumeric } from "./utils.js" ; // -------------------------------------------------------------------- export class ArticleSearchResult2 { static _doEditArticle( vals, notify ) { // initialize let refs = {} ; const isNew = Object.keys( vals ).length === 0 ; // set the parent mode let parentMode = vals.publ_id ? "publisher" : "publication" ; let publicationParentRowRef = null ; let publisherParentRowRef = null ; let articleDateRef = null ; function onPublicationParent() { parentMode = "publication" ; publicationParentRowRef.style.display = "flex" ; publisherParentRowRef.style.display = "none" ; articleDateRef.style.display = "none" ; refs.pub_id.focus() ; } function onPublisherParent() { parentMode = "publisher" ; publicationParentRowRef.style.display = "none" ; publisherParentRowRef.style.display = "flex" ; articleDateRef.style.display = "flex" ; refs.publ_id.focus() ; } // prepare to save the initial values let initialVals = null ; function onReady() { if ( ! initialVals ) initialVals = unloadVals() ; } // initialize the image let imageFilename=null, imageData=null ; let imageRef=null, uploadImageRef=null, removeImageRef=null ; let imageUrl = gAppRef.makeFlaskImageUrl( "article", vals.article_id ) || "/force-404" ; function onImageLoaded() { onReady() ; } function onMissingImage() { imageRef.src = "/images/placeholder.png" ; removeImageRef.style.display = "none" ; onReady() ; } ; function onUploadImage( evt ) { if ( evt === null && !gAppRef.isFakeUploads() ) { // nb: the article 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 ; } ) ; } ; function onRemoveImage() { imageData = "{remove}" ; imageRef.src = "/images/placeholder.png" ; removeImageRef.style.display = "none" ; } ; // initialize the publications let publications = [ { value: null, label: (none), textLabel: "" } ] ; let mostRecentPub = null ; for ( let p of Object.entries( gAppRef.dataCache.data.publications ) ) { const pub_display_name = PublicationSearchResult.makeDisplayName( p[1] ) ; const pub = { value: p[1].pub_id, label: , textLabel: pub_display_name, } ; publications.push( pub ) ; if ( mostRecentPub === null || p[1].time_created > mostRecentPub[1] ) mostRecentPub = [ pub, p[1].time_created ] ; } sortSelectableOptions( publications ) ; if ( isNew && mostRecentPub ) { // NOTE: If the user is creating a new article, we check for the most recently-created publication // and put that at the the top of list. This makes things easier in the most common use-case: // the user has received a new magazine and is entering all the articles from it. const now = new Date() / 1000 | 0 ; const delta = now - mostRecentPub[1] ; // nb: we ignore server/client time zones if ( delta <= NEW_ARTICLE_PUB_PRIORITY_CUTOFF ) { publications = publications.filter( p => p !== mostRecentPub[0] ) ; publications.splice( 1, 0, mostRecentPub[0] ) ; } } let currPub = publications[0] ; for ( let i=1; i < publications.length ; ++i ) { if ( publications[i].value === vals.pub_id ) { currPub = publications[i] ; break ; } } // initialize the publishers let publishers = [ { value: null, label: (none), textLabel: "" } ] ; let currPubl = publishers[0] ; for ( let p of Object.entries( gAppRef.dataCache.data.publishers ) ) { publishers.push( { value: p[1].publ_id, label: , textLabel: p[1].publ_name, } ) ; if ( p[1].publ_id === vals.publ_id ) currPubl = publishers[ publishers.length-1 ] ; } sortSelectableOptions( publishers ) ; // initialize the authors let allAuthors = [] ; for ( let a of Object.entries( gAppRef.dataCache.data.authors ) ) allAuthors.push( { value: a[1].author_id, label: a[1].author_name } ); allAuthors.sort( (lhs,rhs) => { return lhs.label.localeCompare( rhs.label ) ; } ) ; let currAuthors = [] ; if ( vals.article_authors ) { currAuthors = vals.article_authors.map( a => { return { value: a.author_id, label: a.author_name } } ) ; } // initialize the scenarios let allScenarios = [] ; for ( let s of Object.entries( gAppRef.dataCache.data.scenarios ) ) allScenarios.push( { value: s[1].scenario_id, label: makeScenarioDisplayName(s[1]) } ) ; allScenarios.sort( (lhs,rhs) => { return lhs.label.localeCompare( rhs.label ) ; } ) ; let currScenarios = [] ; if ( vals.article_scenarios ) { currScenarios = vals.article_scenarios.map( s => { return { value: s.scenario_id, label: makeScenarioDisplayName(s) } } ) ; } function onScenarioCreated( val ) { const vals = parseScenarioDisplayName( val ) ; if ( ! vals[0] ) { // NOTE: It would be nice to show a dialog asking the user to enter the scenario name, ID and // ROAR ID, but it's more trouble than it's worth :-/ gAppRef.showWarningToast(
Couldn't extract the scenario ID.

Please use the format SCENARIO NAME [ID].

) ; } } // initialize the tags const tags = makeTagLists( vals.article_tags ) ; // prepare the form content /* eslint-disable jsx-a11y/img-redundant-alt */ const content =
onUploadImage(null) } ref = { r => imageRef=r } alt="Upload image." title="Click to upload an image for this article." /> removeImageRef=r } alt="Remove image." title="Remove the article's image." /> uploadImageRef=r } />
refs.article_title=r} />
refs.article_subtitle=r} />
publicationParentRowRef=r} > refs.article_pageno=r} title="Page number." />
publisherParentRowRef=r} > refs.article_date=r} />