11import { useState , useEffect } from "react" ;
22import { useCuration } from "../../context/CurationContext" ;
33
4+ const PROXY = "https://corsproxy.io/?" ;
5+ const BASE = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils" ;
6+
47export default function Step1Publication ( ) {
58 const { publication, setPublication, goToNextStep } = useCuration ( ) ;
69
7- const [ query , setQuery ] = useState ( "" ) ; //input
8- const [ loading , setLoading ] = useState ( false ) ; //si està buscant
10+ // Estat local del formulari
11+ const [ query , setQuery ] = useState ( "" ) ;
12+ const [ loading , setLoading ] = useState ( false ) ;
913 const [ article , setArticle ] = useState ( null ) ;
1014 const [ error , setError ] = useState ( "" ) ;
1115
12- const PROXY = "https://corsproxy.io/?" ;
13- const BASE = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils" ;
14-
15- useEffect ( ( ) => { //mostra dades al anar enrere
16- if ( publication ) {
17- setArticle ( {
18- ...publication ,
19- doi : normalizeDOI ( publication . doi ) ,
20- } ) ;
21- if ( publication . pmid ) setQuery ( publication . pmid ) ;
22- else if ( publication . doi ) setQuery ( publication . doi ) ;
23- }
24- } , [ publication ] ) ;
16+ // Quan tornem enrere al pipeline, recuperem les dades guardades
17+ useEffect ( ( ) => {
18+ if ( ! publication ) return ;
2519
20+ setArticle ( {
21+ ...publication ,
22+ doi : normalizeDOI ( publication . doi ) ,
23+ } ) ;
24+
25+ if ( publication . pmid ) setQuery ( publication . pmid ) ;
26+ else if ( publication . doi ) setQuery ( publication . doi ) ;
27+ } , [ publication ] ) ;
2628
27- //Main search function ( PMID, DOI o TITLE)
29+ // Cerca principal: pot ser PMID, DOI o títol
2830 async function handleSearch ( e ) {
2931 e . preventDefault ( ) ;
3032 setError ( "" ) ;
@@ -40,14 +42,14 @@ export default function Step1Publication() {
4042 const isPMID = / ^ \d + $ / . test ( q ) ;
4143 const isDOI = q . includes ( "/" ) ;
4244
43- // SEARCH BY PMID
45+ // Cerca directa per PMID
4446 if ( isPMID ) {
4547 const url = `${ BASE } /esummary.fcgi?db=pubmed&id=${ q } &retmode=json` ;
4648 const res = await fetch ( PROXY + encodeURIComponent ( url ) ) ;
4749 const json = await res . json ( ) ;
4850 const rec = json . result ?. [ q ] ;
4951
50- if ( ! rec ) throw new Error ( "PMID not found. " ) ;
52+ if ( ! rec ) throw new Error ( "PMID no trobat " ) ;
5153
5254 data = {
5355 pmid : q ,
@@ -59,9 +61,8 @@ export default function Step1Publication() {
5961 } ;
6062 }
6163
62- // SEARCH BY DOI (PubMed → fallback CrossRef)
64+ // Cerca per DOI (primer PubMed, després CrossRef)
6365 if ( ! data && isDOI ) {
64- // Try PubMed first
6566 const esearchUrl = `${ BASE } /esearch.fcgi?db=pubmed&retmode=json&term=${ encodeURIComponent (
6667 q
6768 ) } [doi]`;
@@ -84,38 +85,33 @@ export default function Step1Publication() {
8485 journal : rec . fulljournalname || "Unknown" ,
8586 pubdate : rec . pubdate || "No date" ,
8687 doi : normalizeDOI ( rec . elocationid || q ) ,
87-
8888 } ;
8989 }
9090 }
9191
92- // Fallback: CrossRef
92+ // Fallback a CrossRef si PubMed no retorna res
9393 if ( ! data ) {
94- const crUrl = `https://api.crossref.org/works/${ encodeURIComponent (
95- q
96- ) } `;
94+ const crUrl = `https://api.crossref.org/works/${ encodeURIComponent ( q ) } ` ;
9795 const crRes = await fetch ( PROXY + encodeURIComponent ( crUrl ) ) ;
9896 const crJson = await crRes . json ( ) ;
9997 const m = crJson . message ;
10098
101- if ( ! m ) throw new Error ( "DOI not found in CrossRef. " ) ;
99+ if ( ! m ) throw new Error ( "DOI no trobat a CrossRef" ) ;
102100
103101 data = {
104102 pmid : null ,
105103 title : m . title ?. [ 0 ] || "Title not available" ,
106104 authors : ( m . author || [ ] )
107105 . map ( ( a ) => `${ a . family || "" } ${ a . given || "" } ` )
108106 . join ( ", " ) ,
109- journal :
110- m [ "container-title" ] ?. [ 0 ] || "Unknown journal" ,
111- pubdate :
112- m . issued ?. [ "date-parts" ] ?. [ 0 ] ?. join ( "-" ) || "No date" ,
107+ journal : m [ "container-title" ] ?. [ 0 ] || "Unknown journal" ,
108+ pubdate : m . issued ?. [ "date-parts" ] ?. [ 0 ] ?. join ( "-" ) || "No date" ,
113109 doi : q ,
114110 } ;
115111 }
116112 }
117113
118- // SEARCH BY TITLE (PubMed)
114+ // Cerca per títol (PubMed)
119115 if ( ! data && ! isPMID && ! isDOI ) {
120116 const esearchUrl = `${ BASE } /esearch.fcgi?db=pubmed&retmode=json&retmax=1&term=${ encodeURIComponent (
121117 q
@@ -125,9 +121,7 @@ export default function Step1Publication() {
125121 const js1 = await r1 . json ( ) ;
126122 const pmid = js1 . esearchresult ?. idlist ?. [ 0 ] ;
127123
128- if ( ! pmid ) {
129- throw new Error ( "No PubMed articles found with this title." ) ;
130- }
124+ if ( ! pmid ) throw new Error ( "Cap article trobat amb aquest títol" ) ;
131125
132126 const esumUrl = `${ BASE } /esummary.fcgi?db=pubmed&id=${ pmid } &retmode=json` ;
133127 const r2 = await fetch ( PROXY + encodeURIComponent ( esumUrl ) ) ;
@@ -144,31 +138,31 @@ export default function Step1Publication() {
144138 } ;
145139 }
146140
147- if ( ! data ) throw new Error ( "No results found. " ) ;
141+ if ( ! data ) throw new Error ( "Sense resultats " ) ;
148142
149143 setArticle ( data ) ;
150144 } catch ( e ) {
151145 console . error ( e ) ;
152146 setError (
153- "Error searching the article. Please enter a PMID, DOI or article title ."
147+ "Error cercant l’ article. Introdueix un PMID, un DOI o el títol ."
154148 ) ;
155149 } finally {
156150 setLoading ( false ) ;
157151 }
158152 }
159153
154+ // Normalitza el DOI quan ve incrustat en altres camps
160155 function normalizeDOI ( raw ) {
161- if ( ! raw ) return "No DOI " ;
156+ if ( ! raw ) return "" ;
162157 const m = raw . match ( / 1 0 \. \d { 4 , 9 } \/ \S + / i) ;
163158 return m ? m [ 0 ] : raw ;
164159 }
165160
166- const handleConfirm = ( ) => {
167- if ( article ) {
168- setPublication ( article ) ;
169- goToNextStep ( ) ;
170- }
171- } ;
161+ function handleConfirm ( ) {
162+ if ( ! article ) return ;
163+ setPublication ( article ) ;
164+ goToNextStep ( ) ;
165+ }
172166
173167 return (
174168 < div className = "space-y-4" >
@@ -177,12 +171,12 @@ export default function Step1Publication() {
177171 < div className = "flex gap-2" >
178172 < input
179173 className = "form-control"
180- placeholder = "PMID, DOI or article title "
174+ placeholder = "PMID, DOI o títol de l’article "
181175 value = { query }
182176 onChange = { ( e ) => setQuery ( e . target . value ) }
183177 />
184178 < button className = "btn" onClick = { handleSearch } disabled = { loading } >
185- { loading ? "Searching ..." : "Search " }
179+ { loading ? "Cercant ..." : "Cerca " }
186180 </ button >
187181 </ div >
188182
@@ -192,33 +186,23 @@ export default function Step1Publication() {
192186 rel = "noopener noreferrer"
193187 className = "inline-block text-sm text-blue-400 hover:text-blue-300 underline mt-1"
194188 >
195- Search article directly on PubMed
189+ Cercar directament a PubMed
196190 </ a >
197191
198192 { error && < p className = "text-red-400" > { error } </ p > }
199193
200194 { article && (
201195 < div className = "bg-surface border border-border rounded p-4 space-y-1" >
202196 < h3 className = "text-xl font-semibold" > { article . title } </ h3 >
203- < p >
204- < strong > Authors:</ strong > { article . authors }
205- </ p >
206- < p >
207- < strong > Journal:</ strong > { article . journal }
208- </ p >
209- < p >
210- < strong > Date:</ strong > { article . pubdate }
211- </ p >
212- < p >
213- < strong > PMID:</ strong > { article . pmid || "—" }
214- </ p >
215- < p >
216- < strong > DOI:</ strong > { article . doi }
217- </ p >
197+ < p > < strong > Autors:</ strong > { article . authors } </ p >
198+ < p > < strong > Revista:</ strong > { article . journal } </ p >
199+ < p > < strong > Data:</ strong > { article . pubdate } </ p >
200+ < p > < strong > PMID:</ strong > { article . pmid || "—" } </ p >
201+ < p > < strong > DOI:</ strong > { article . doi || "—" } </ p >
218202
219203 < div className = "pt-3" >
220204 < button className = "btn" onClick = { handleConfirm } >
221- Confirm and continue →
205+ Confirmar i continuar →
222206 </ button >
223207 </ div >
224208 </ div >
0 commit comments