11import React from "react" ;
22import { connect } from "react-redux" ;
33
4- import { Button , Modal } from "react-bootstrap" ;
4+ import { Modal } from "react-bootstrap" ;
55import { hideExportPaper } from "../../actions" ;
66
77import { useLocalizationContext } from "../../components/LocalizationProvider" ;
8- import CopyButton from "../CopyButton" ;
9- import usePaperExport , {
10- PAPER_EXPORT_ENDPOINT ,
11- } from "../../utils/usePaperExport" ;
12- import useMatomo from "../../utils/useMatomo" ;
8+ import usePaperExport from "../../utils/usePaperExport" ;
9+ import Loading from "./exportmodal/Loading" ;
10+ import Content from "./exportmodal/Content" ;
11+ import Error from "./exportmodal/Error" ;
1312
1413const ExportPaperModal = ( { open, onClose, paper, serverUrl } ) => {
1514 const loc = useLocalizationContext ( ) ;
16- const { trackEvent } = useMatomo ( ) ;
1715
18- const exportContent = usePaperExport ( paper , serverUrl ) ;
16+ const exp = usePaperExport ( paper , serverUrl ) ;
1917
20- const handleCopyClick = ( ) => {
21- trackEvent ( "List document" , "Copy paper export" , "Copy export button" ) ;
22- } ;
18+ const hasFailed = exp && ( exp . error || exp . content === "" ) ;
2319
24- const handleDownloadClick = ( ) => {
25- startExportDownload ( paper , serverUrl ) ;
26- trackEvent (
27- "List document" ,
28- "Download paper export" ,
29- "Download export button"
30- ) ;
20+ const renderBody = ( ) => {
21+ // no data = closed modal
22+ if ( ! paper ) {
23+ return null ;
24+ }
25+ // loading in progress
26+ if ( ! exp ) {
27+ return < Loading /> ;
28+ }
29+ // error while loading
30+ if ( hasFailed ) {
31+ return < Error /> ;
32+ }
33+ // successfully loaded
34+ if ( exp . content ) {
35+ return (
36+ < Content paper = { paper } serverUrl = { serverUrl } >
37+ { exp . content }
38+ </ Content >
39+ ) ;
40+ }
41+ // fallback
42+ return null ;
3143 } ;
3244
3345 return (
@@ -37,48 +49,12 @@ const ExportPaperModal = ({ open, onClose, paper, serverUrl }) => {
3749 < Modal . Title
3850 id = "export-paper-title"
3951 className = "export-paper-modal-title"
40- style = { { fontSize : 20 } }
52+ style = { { fontSize : 20 , color : hasFailed ? "#e55137" : undefined } }
4153 >
42- { loc . export_paper }
54+ { loc . export_paper + ( hasFailed ? " - connection lost" : "" ) }
4355 </ Modal . Title >
4456 </ Modal . Header >
45- < Modal . Body className = "modal-body" >
46- { ! ! paper && ! exportContent && (
47- < div id = "spinner-iframe" >
48- < p className = "wait-message" > { loc . pdf_load_text } </ p >
49- < p className = "wait-spinner" >
50- < span
51- id = "spinner-iframe-icon"
52- className = "glyphicon glyphicon-refresh glyphicon-refresh-animate"
53- > </ span > { " " }
54- </ p >
55- </ div >
56- ) }
57- { ( ! paper || ! ! exportContent ) && (
58- < >
59- < p >
60- < strong className = "hs-strong" style = { { fontWeight : 800 } } > BibTeX</ strong >
61- </ p >
62- < div id = "copy-paper-export" className = "citation code" >
63- { exportContent }
64- </ div >
65- < CopyButton
66- className = "indented-modal-btn"
67- textId = { "copy-paper-export" }
68- textContent = { exportContent }
69- onClick = { handleCopyClick }
70- />
71- < Button
72- className = "indented-modal-btn"
73- bsStyle = "primary"
74- onClick = { handleDownloadClick }
75- >
76- < i className = "fa fa-download" > </ i >
77- { loc . download }
78- </ Button >
79- </ >
80- ) }
81- </ Modal . Body >
57+ < Modal . Body className = "modal-body" > { renderBody ( ) } </ Modal . Body >
8258 </ Modal >
8359 // html template ends here
8460 ) ;
@@ -95,24 +71,3 @@ const mapDispatchToProps = (dispatch) => ({
9571} ) ;
9672
9773export default connect ( mapStateToProps , mapDispatchToProps ) ( ExportPaperModal ) ;
98-
99- // the alternative solution is to really create a form in the JSX
100- const startExportDownload = ( paper , serverUrl ) => {
101- const url = serverUrl + PAPER_EXPORT_ENDPOINT + "&download=true" ;
102-
103- const form = document . createElement ( "form" ) ;
104-
105- form . setAttribute ( "method" , "post" ) ;
106- form . setAttribute ( "action" , url ) ;
107- form . setAttribute ( "target" , "_blank" ) ;
108-
109- const input = document . createElement ( "input" ) ;
110- input . type = "hidden" ;
111- input . name = "paper" ;
112- input . value = JSON . stringify ( paper ) ;
113- form . appendChild ( input ) ;
114-
115- document . body . appendChild ( form ) ;
116- form . submit ( ) ;
117- document . body . removeChild ( form ) ;
118- } ;
0 commit comments