-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Repris de #482
- Loading branch information
Showing
6 changed files
with
173 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { setLocalisation } from '../actions/actions' | ||
|
||
export const sampleIps = { | ||
guadeloupe: '104.250.27.0', | ||
france: '92.184.106.103', | ||
'polynésie française': '203.185.161.106', | ||
} | ||
|
||
export const correspondancePullRequests = { | ||
guadeloupe: '1339', | ||
'french polynesia': '1339', | ||
} | ||
|
||
const API = | ||
'https://api.ipgeolocation.io/ipgeo?apiKey=a6346b522995413a8f4578e025a3eae6' | ||
|
||
// Other alternatives : | ||
// https://positionstack.com/product | ||
// https://www.abstractapi.com/ip-geolocation-api?fpr=geekflare#pricing | ||
|
||
export default (ip) => { | ||
const dispatch = useDispatch() | ||
|
||
const localisation = useSelector((state) => state.localisation) | ||
|
||
useEffect(() => { | ||
if (localisation != null && localisation.ip == ip) return null | ||
|
||
fetch(API + (ip == null ? '' : `&ip=${ip}`)) | ||
.catch((e) => { | ||
console.log('erreur', e) | ||
}) | ||
.then((res) => res && res.json()) | ||
.then((data) => { | ||
dispatch( | ||
setLocalisation({ | ||
...data, | ||
ip, | ||
country_flag: | ||
//https://fr.wikipedia.org/wiki/Drapeau_de_la_Guadeloupe | ||
data.country_name.toLowerCase() === 'guadeloupe' | ||
? 'https://openmoji.org/data/color/svg/1F1EC-1F1F5.svg' | ||
: data.country_flag, | ||
}) | ||
) | ||
}) | ||
}, [ip]) | ||
|
||
return localisation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState } from 'react' | ||
import useLocalisation, { sampleIps } from '../../components/useLocalisation' | ||
import emoji from 'react-easy-emoji' | ||
import { capitalise0 } from '../../utils' | ||
|
||
export default () => { | ||
const [chosenIp, chooseIp] = useState() | ||
const localisation = useLocalisation(chosenIp) | ||
|
||
return ( | ||
<div> | ||
<h2>{emoji('📍')} Pays de simulation</h2> | ||
{localisation != null ? ( | ||
<p> | ||
Nous avons détecté que vous faites cette simulation depuis la{' '} | ||
<img | ||
src={localisation.country_flag} | ||
aria-hidden="true" | ||
css={` | ||
height: 1rem; | ||
margin: 0 0.3rem; | ||
vertical-align: middle; | ||
`} | ||
/> | ||
{localisation.country_name}. | ||
</p> | ||
) : ( | ||
<p>Nous n'avons pas pu détecter votre pays de simulation. </p> | ||
)} | ||
<details> | ||
<summary>Choisir un autre pays</summary> | ||
<ul> | ||
{Object.entries(sampleIps).map(([country, ip]) => ( | ||
<li key={country} onClick={() => chooseIp(ip)}> | ||
<button>{capitalise0(country)}</button> | ||
</li> | ||
))} | ||
</ul> | ||
</details> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters