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 "./index.js" ; import { ImageFileUploader } from "./FileUploader.js" ; import { makeScenarioDisplayName, parseScenarioDisplayName, sortSelectableOptions, unloadCreatableSelect } from "./utils.js" ; // -------------------------------------------------------------------- export class ArticleSearchResult2 { static _doEditArticle( vals, notify ) { // initialize let refs = {} ; const isNew = Object.keys( vals ).length === 0 ; // initialize the image let imageFilename=null, imageData=null ; let imageRef=null, uploadImageRef=null, removeImageRef=null ; let imageUrl = gAppRef.makeFlaskUrl( "/images/article/" + vals.article_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 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 ; } ) ; } ; let onRemoveImage = () => { imageData = "{remove}" ; imageRef.src = "/images/placeholder.png" ; removeImageRef.style.display = "none" ; } ; // initialize the publications let publications = [ { value: null, label: (none) } ] ; let mostRecentPub = null ; for ( let p of Object.entries(gAppRef.caches.publications) ) { const pub_display_name = PublicationSearchResult.makeDisplayName( p[1] ) ; const pub = { value: p[1].pub_id, label: , } ; 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 authors let allAuthors = [] ; for ( let a of Object.entries(gAppRef.caches.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, label: gAppRef.caches.authors[a].author_name } } ) ; } // initialize the scenarios let allScenarios = [] ; for ( let s of Object.entries(gAppRef.caches.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, label: makeScenarioDisplayName(gAppRef.caches.scenarios[s]) } } ) ; } // initialize the tags const tags = gAppRef.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} />
refs.article_pageno=r} title="Page number." />