1- import { useState } from 'react' ;
1+ import { useState , useEffect } from 'react' ;
22import PropTypes from 'prop-types' ;
3- import CountryPicker from './CountryPicker' ;
4- import DatePicker from './DatePicker' ;
5- import AccessPicker from './AccessPicker' ;
3+ import CountryPickList from './CountryPicker' ;
4+ import axios from 'axios' ;
65
76const ArticleForm = ( { onSubmit, loading } ) => {
87 const [ formErrors , setFormErrors ] = useState ( { } ) ;
@@ -11,7 +10,43 @@ const ArticleForm = ({ onSubmit, loading }) => {
1110 date : '' ,
1211 access : 'all-access' ,
1312 } ) ;
13+ const [ locationError , setLocationError ] = useState ( '' ) ;
1414
15+ useEffect ( ( ) => {
16+ const fetchLocation = async ( lat , lon ) => {
17+ try {
18+ const response = await axios . get ( 'https://api.bigdatacloud.net/data/reverse-geocode-client' , {
19+ params : {
20+ latitude : lat ,
21+ longitude : lon ,
22+ localityLanguage : 'en' ,
23+ } ,
24+ } ) ;
25+
26+ const { countryCode } = response . data ;
27+ setForm ( ( prevForm ) => ( {
28+ ...prevForm ,
29+ country : countryCode ,
30+ } ) ) ;
31+ } catch ( error ) {
32+ setLocationError ( 'Error fetching location data.' ) ;
33+ }
34+ } ;
35+
36+ if ( navigator . geolocation ) {
37+ navigator . geolocation . getCurrentPosition (
38+ ( position ) => {
39+ const { latitude, longitude } = position . coords ;
40+ fetchLocation ( latitude , longitude ) ;
41+ } ,
42+ ( ) => {
43+ setLocationError ( 'Error getting geolocation.' ) ;
44+ } ,
45+ ) ;
46+ } else {
47+ setLocationError ( 'Geolocation is not supported by this browser.' ) ;
48+ }
49+ } , [ ] ) ;
1550 const [ country , setCountry ] = useState ( 'CD' ) ;
1651 const [ continent , setContinent ] = useState ( 'Africa' ) ;
1752
@@ -44,16 +79,17 @@ const ArticleForm = ({ onSubmit, loading }) => {
4479 }
4580 } ;
4681
82+ // const countryData = Object.keys(country.all);
83+
4784 return (
48- < form onSubmit = { handleSubmit } className = 'w-full flex flex-col items-center ' >
49- < div className = 'flex flex-col gap-[0.5rem] justify-between items-center w-full py-3 ' >
50- < div className = 'text-start mb-2' >
51- < span className = 'date text-[20px] ' > Veuillez remplir le formulaire pour obtenir les articles souhaités</ span >
85+ < form onSubmit = { handleSubmit } className = 'w-full formBorder py-5 rounded-xl max-md:w-[95vw] ' >
86+ < div className = 'flex flex-col gap-[0.5rem] justify-between items-center w-full' >
87+ < div className = 'text-start mb-2 py-5 ' >
88+ < p className = 'date text-[20px] max-md:text-xs text-center ' > Veuillez remplir le formulaire pour obtenir les articles souhaités</ p >
5289 </ div >
5390
54- < div className = 'inputs flex gap-[1rem] items-start' >
55- < CountryPicker
56- label = { 'Choisir un pays' }
91+ < div className = 'inputs flex gap-[1rem] max-md:flex-col max-md:text-xs' >
92+ < CountryPickList
5793 country = { country }
5894 onChangeCountry = { ( country ) => {
5995 setForm ( { ...form , country } ) ;
@@ -64,16 +100,35 @@ const ArticleForm = ({ onSubmit, loading }) => {
64100 onChangeContinent = { ( continent ) => setContinent ( continent ) }
65101 defaultContinent = 'Africa'
66102 />
103+ < div className = 'select_container country_select' >
104+ < div >
105+ < label className = 'select_label' > Date</ label >
67106
68- < DatePicker label = { 'Date' } date = { form . date } onChange = { handleChange } error = { formErrors . date } />
107+ < input id = 'fullDate' type = 'date' name = 'date' className = 'select_options' value = { form . date } onChange = { handleChange } />
108+ </ div >
109+ { formErrors . date && < div className = 'text-red-500' > { formErrors . date } </ div > }
110+ </ div >
69111
70- < AccessPicker label = { 'Plateforme' } access = { form . access } onChange = { handleChange } error = { formErrors . access } />
112+ < div className = 'select_container country_select' >
113+ < div >
114+ < label htmlFor = '' className = 'select_label' >
115+ Platform
116+ </ label >
117+ < select className = 'select_options' name = 'access' value = { form . access } onChange = { handleChange } >
118+ < option value = 'all-access' > all-access</ option >
119+ < option value = 'desktop' > desktop</ option >
120+ < option value = 'mobile-app' > mobile-app</ option >
121+ < option value = 'mobile-web' > mobile-web</ option >
122+ </ select >
123+ { formErrors . access && < div className = 'error' > { formErrors . access } </ div > }
124+ </ div >
125+ </ div >
71126 </ div >
72-
73- < button type = 'submit' className = 'py-[0.5rem] bg-green-500 text-white px-6 text-[18px] capitalize font-600 w-56' >
74- { loading ? 'Submitting' : 'Envoyer' }
127+ < button type = 'submit' className = ' py-[0.7rem] my-5 bg-green-500 text-white px-6 text-[18px] font-600 w-56 max-md:text-xs' >
128+ { loading ? 'Envoie en cours...' : 'Envoyer' }
75129 </ button >
76130 </ div >
131+ { locationError && < div className = 'error' > { locationError } </ div > }
77132 </ form >
78133 ) ;
79134} ;
0 commit comments