-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwithTurf.js
39 lines (34 loc) · 1.21 KB
/
withTurf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React, {useEffect, useState} from 'react';
import {getAbbreviatedPackument} from 'query-registry';
export default function withTurf (WrappedComponent) {
return function () {
const [state, setState] = useState({
turfVersion: '',
turf: '',
turfLoading: true,
});
useEffect(() => {
async function fetchData () {
const [turf, npmData] = await Promise.all([
// import Turf and package info
fetch('https://npmcdn.com/@turf/turf', {redirect: 'follow'}).then(r => r.text()),
getAbbreviatedPackument({name: '@turf/turf'}),
]);
setState(s => ({
...s,
turf,
turfVersion: npmData.distTags.latest,
turfLoading: false
}));
}
try {
fetchData();
} catch (e) {
console.error(e);
alert('Sorry, an error occurred loading Turf.');
setState(s => ({...s, turfLoading: false,}));
}
}, []);
return <WrappedComponent {...state}/>;
};
}