utils#useFetch TypeScript Examples
The following examples show how to use
utils#useFetch.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.tsx From covid19-visualized with MIT License | 6 votes |
Summary: FunctionComponent = () => {
const { data, loading } = useFetch<SummaryType>(API_BASEURL)()
return (
<>
<div className="text-center my-12">
<h1 className="my-2">Summary</h1>
<h6>Last updatated at: {!loading ? dateFormat(data.lastUpdate, true) : 'Loading...'}</h6>
</div>
<div className="divider-line" />
<SummaryComponent
loading={loading}
data={{
confirmed: data?.confirmed.value,
recovered: data?.recovered.value,
deaths: data?.deaths.value
}}
/>
</>
)
}
Example #2
Source File: index.tsx From covid19-visualized with MIT License | 6 votes |
Daily: FunctionComponent = () => {
const { data, loading } = useFetch<DailyType[]>(API_BASEURL + 'daily')(
data => data
.map((item, index) => {
item.confirmed.perDay = getPerDayStats({ data, index, stats: 'confirmed' })
item.recovered.perDay = getPerDayStats({ data, index, stats: 'recovered' })
item.deaths.perDay = getPerDayStats({ data, index, stats: 'deaths' })
return item
})
.sort(({ reportDate: prev }, { reportDate: next }) => (
new Date(next).getTime() - new Date(prev).getTime()
))
)
return (
<ScrollableList<DailyType> title="Daily Update" data={data} loading={loading}>
{daily => (
<Card
className="text-center"
header={<h5 className="text-center">{dateFormat(daily.reportDate)}</h5>}
footer={
<>
<h3>Total</h3>
<div className="divider-line mt-2 mb-4" style={{ width: '30%' }} />
<p>Confirmed: <span className="font is-weight-bold color is-txt-warning">{daily.confirmed.total}</span></p>
{/* <p>Recovered: <span className="font is-weight-bold color is-txt-success">{daily.recovered.total} ({getPercentage(daily.recovered.total, daily.confirmed.total)})</span></p> */}
<p>Deaths: <span className="font is-weight-bold color is-txt-danger">{daily.deaths.total} ({getPercentage(daily.deaths.total, daily.confirmed.total)})</span></p>
</>
}
>
<p>Confirmed: <span className="font is-weight-bold color is-txt-warning">{daily.confirmed.perDay}</span></p>
{/* <p>Recovered: <span className="font is-weight-bold color is-txt-success">{daily.recovered.perDay}</span></p> */}
<p>Deaths: <span className="font is-weight-bold color is-txt-danger">{daily.deaths.perDay}</span></p>
</Card>
)}
</ScrollableList>
)
}
Example #3
Source File: index.tsx From covid19-visualized with MIT License | 6 votes |
Page: NextPage = () => {
const { data } = useFetch<Country[]>(API_BASEURL + 'confirmed')()
return (
<>
<Summary />
<div className="btn-link mb-24">
<Link href="/region">
<Button block color="primary" text="See Country and Region Cases" />
</Link>
<Link href="/indonesia">
<Button className="mt-8" block color="danger" text="See Indonesia Cases" />
</Link>
</div>
<h2 className="text-center mt-32 mb-12">World Visualization</h2>
<Visualization
id="world-visualization"
data={data}
visualize={visualize.world}
legends={worldLegends.map(({ color, value }) => (
<div key={color} className="legends-item font is-size-small">
<div className="legends-detail">{value === 0 ? `No case infected` : `${value} or more cases infected`}</div>
<div className="legends-color mx-4" style={{ backgroundColor: color }} />
</div>
))}
/>
<Daily />
</>
)
}
Example #4
Source File: index.tsx From covid19-visualized with MIT License | 6 votes |
Summary: FunctionComponent = () => {
const { data, loading } = useFetch<IDSummary>(API_INDONESIA)()
return (
<SummaryComponent
loading={loading}
data={{
confirmed: data?.jumlahKasus,
recovered: data?.sembuh,
deaths: data?.meninggal
}}
/>
)
}
Example #5
Source File: index.tsx From covid19-visualized with MIT License | 6 votes |
Daily: FunctionComponent = () => {
const { data, loading } = useFetch<IDFormat<IDDaily[]>>(API_INDONESIA + 'harian')(
data => {
data.data = data.data
.filter(({ jumlahKasusKumulatif }) => jumlahKasusKumulatif)
.sort(({ tanggal: prev }, { tanggal: next }) => next - prev)
return data
}
)
return (
<ScrollableList<IDDaily> title="Daily Update" loading={loading} data={data?.data}>
{data => (
<Card
className="text-center"
header={<h5 className="text-center">(Hari ke-#{data.harike}) {dateFormat(data.tanggal, false, 'id-ID')}</h5>}
footer={
<>
<h3>Akumulasi</h3>
<div className="divider-line mt-2 mb-4" style={{ width: '30%' }} />
<p>Positif: <span className="font is-weight-bold color is-txt-warning">{data.jumlahKasusKumulatif}</span></p>
<p>Aktif: <span className="font is-weight-bold color is-txt-warning">{data.jumlahpasiendalamperawatan} ({getPercentage(data.jumlahpasiendalamperawatan, data.jumlahKasusKumulatif)})</span></p>
<p>Sembuh: <span className="font is-weight-bold color is-txt-success">{data.jumlahPasienSembuh} ({getPercentage(data.jumlahPasienSembuh, data.jumlahKasusKumulatif)})</span></p>
<p>Meninggal: <span className="font is-weight-bold color is-txt-danger">{data.jumlahPasienMeninggal} ({getPercentage(data.jumlahPasienMeninggal, data.jumlahKasusKumulatif)})</span></p>
</>
}
>
<p>Positif: <span className="font is-weight-bold color is-txt-warning">{data.jumlahKasusBaruperHari}</span></p>
<p>Sembuh: <span className="font is-weight-bold color is-txt-success">{data.jumlahKasusSembuhperHari}</span></p>
<p>Meninggal: <span className="font is-weight-bold color is-txt-danger">{data.jumlahKasusMeninggalperHari}</span></p>
</Card>
)}
</ScrollableList>
)
}
Example #6
Source File: cases.tsx From covid19-visualized with MIT License | 5 votes |
Page: NextPage = () => {
const { data, loading } = useFetch<IDFormat<IDCases[]>>(API_INDONESIA + 'kasus/old')()
return (
<>
<Head>
<title>Indonesia Case Details | COVID-19 Visualized</title>
{meta}
</Head>
<div className="text-center my-12">
<h1 className="my-2">Indonesia Detail Cases</h1>
<h6 className="color is-txt-warning">{data?.warning}</h6>
</div>
<div className="divider-line" />
<div className="btn-link my-24">
<Link href="/indonesia">
<Button block color="secondary" text="< Back to Indonesia Cases" />
</Link>
</div>
<ScrollableList<IDCases> title="Data Cases" data={data?.data} loading={loading}>
{data => (
<Card
className="text-center"
header={<h5 className="text-center">(#{data.no}) {data.kota ? `${data.kota}, ${data.provinsi}` : data.provinsi}</h5>}
footer={
<span className="font is-size-small">
{data.keterangan || 'Tidak ada keterangan'}; {data.dirawatdi ? `(Sedang dirawat di ${data.dirawatdi}); ` : ''}{data.kondisiKesehatan ? `Kodisi: ${data.kondisiKesehatan};` : ''}
</span>
}
>
<p>Usia: <span className="font is-weight-bold">{data.usia ? `${data.usia} tahun` : 'Tidak diketahui'}</span></p>
<p>Jenis Kelamin: <span className="font is-weight-bold">{data.jk === 'P'
? 'Wanita'
: data.jk === 'L'
? 'Pria'
: 'Tidak diketahui'
}</span></p>
<p>Status: <span className={`font color is-weight-bold is-txt-${data.status === 'Aktif' ? 'warning' : data.status === 'Sembuh' ? 'success' : data.status === 'Meninggal' ? 'danger' : ''}`}>{data.status || 'Tidak diketahui'}</span></p>
<p>Kluster: <span className="font is-weight-bold ">{data.kluster || 'Tidak diketahui'}</span></p>
<p className="mt-8">Positif: <span className="font is-weight-bold">{data.positif ? dateFormat(+data.positif, false, 'id-ID') : 'Tidak diketahui'}</span></p>
<p>Mulai Gejala: <span className="font is-weight-bold">{data.mulaiGejala ? dateFormat(+data.mulaiGejala, false, 'id-ID') : 'Tidak diketahui'}</span></p>
<p>Mulai Di Isolasi: <span className="font is-weight-bold">{data.mulaiDiisolasi ? dateFormat(+data.mulaiDiisolasi, false, 'id-ID') : 'Tidak diketahui'}</span></p>
</Card>
)}
</ScrollableList>
</>
)
}
Example #7
Source File: index.tsx From covid19-visualized with MIT License | 5 votes |
Page: NextPage = () => {
const { data } = useFetch<IDFormat<IDProvince[]>>(API_INDONESIA + 'provinsi')(
data => {
data.data = data.data.filter(({ kodeProvi }) => kodeProvi)
return data
}
)
return (
<>
<Head>
<title>Indonesia Daily Update | COVID-19 Visualized</title>
{meta}
</Head>
<div className="text-center my-12">
<h1 className="my-2">Indonesia Update</h1>
</div>
<div className="divider-line" />
<Summary />
<div className="btn-link my-24">
<Link href="/indonesia/province">
<Button className="my-4" block color="primary" text="See Province Cases" />
</Link>
<Link href="/indonesia/cases">
<Button className="my-4" block color="success" text="See Case details" />
</Link>
<Link href="/">
<Button className="my-4" block color="secondary" text="< Back to Home" />
</Link>
</div>
<h2 className="text-center mt-32 mb-12">Indonesia Visualization</h2>
<Visualization
id="indonesia-visualization"
data={data?.data}
visualize={visualize.indonesia}
legends={indonesiaLegends.map(({ color, value }) => (
<div key={color} className="legends-item font is-size-small">
<div className="legends-detail">{value === 0 ? `Tidak ada kasus positif` : `${value} atau lebih kasus positif`}</div>
<div className="legends-color mx-4" style={{ backgroundColor: color }} />
</div>
))}
/>
<Daily />
</>
)
}
Example #8
Source File: province.tsx From covid19-visualized with MIT License | 5 votes |
Page: NextPage = () => {
const { data, loading } = useFetch<IDFormat<IDProvince[]>>(API_INDONESIA + 'provinsi')(
data => {
data.data = data.data.filter(({ kodeProvi }) => kodeProvi)
return data
}
)
return (
<>
<Head>
<title>Indonesia Province | COVID-19 Visualized</title>
{meta}
</Head>
<div className="text-center my-12">
<h1 className="my-2">Indonesia Province</h1>
</div>
<div className="divider-line mb-32" />
<div className="btn-link mb-24">
<Link href="/indonesia">
<Button block color="secondary" text="< Back to Indonesia Cases" />
</Link>
</div>
<DataSearch<IDProvince>
data={data?.data}
loading={loading}
searchPlaceholder="Search province... (eg: jakarta)"
searchFilter={(keyword, item) => item.provinsi.toLowerCase().includes(keyword)}
>
{(data, keyword) => data.length
? (
<FlexList<IDProvince> data={data} wrapperClass="my-12" itemClass="my-12">
{province => (
<Region
chart={{
confirmed: province.kasusPosi,
recovered: province.kasusSemb,
deaths: province.kasusMeni
}}
header={`(#${province.kodeProvi}) ${province.provinsi}`}
>
<p>Total Positif: <span className="font is-weight-bold color is-txt-warning">{province.kasusPosi}</span></p>
<p className="mt-8">Aktif: <span className="font is-weight-bold color is-txt-warning">{getActiveCaseID(province)}</span></p>
<p>Sembuh: <span className="font is-weight-bold color is-txt-success">{province.kasusSemb}</span></p>
<p>Meninggal: <span className="font is-weight-bold color is-txt-danger">{province.kasusMeni}</span></p>
</Region>
)}
</FlexList>
) : (
<h3 className="text-center my-24">{
keyword.length
? `No matching province "${keyword}" found.`
: 'Please type the name of province that you want to search.'
}</h3>
)
}
</DataSearch>
</>
)
}
Example #9
Source File: region.tsx From covid19-visualized with MIT License | 5 votes |
Page: NextPage = () => {
const countries = useFetch<Country[]>(API_BASEURL + 'confirmed')(
data => (
data.sort(({ countryRegion: prev }, { countryRegion: next }) => (next > prev) ? -1 : 1)
)
)
return (
<>
<Head>
<title>Country and Region | COVID-19 Visualized</title>
{meta}
</Head>
<div className="text-center my-12">
<h1 className="my-2">Country and Region</h1>
</div>
<div className="divider-line mb-32" />
<div className="btn-link mb-24">
<Link href="/">
<Button block color="secondary" text="< Back to Home" />
</Link>
</div>
<DataSearch<Country>
data={countries.data}
loading={countries.loading}
searchPlaceholder="Search country state or province... (eg: indonesia)"
searchFilter={(keyword, item) => {
const region = item.countryRegion.toLowerCase()
const province = item.provinceState?.toLowerCase()
return region.includes(keyword) || (province ? province.includes(keyword) : false)
}}
>
{(data, keyword) => data.length
? (
<FlexList<Country> data={data} wrapperClass="my-12" itemClass="my-12">
{country => (
<Region
chart={{
confirmed: country.confirmed,
recovered: country.recovered,
deaths: country.deaths
}}
header={
country.provinceState
? `${country.provinceState}, ${country.countryRegion}`
: country.countryRegion
}
footer={`Last updated at: ${dateFormat(country.lastUpdate, true)}`}
>
<p>Total Confirmed: <span className="font is-weight-bold color is-txt-warning">{country.confirmed}</span></p>
<p className="mt-8">Active: <span className="font is-weight-bold color is-txt-warning">{getActiveCase(country)}</span></p>
<p>Recovered: <span className="font is-weight-bold color is-txt-success">{country.recovered}</span></p>
<p>Deaths: <span className="font is-weight-bold color is-txt-danger">{country.deaths}</span></p>
</Region>
)}
</FlexList>
) : (
<h3 className="text-center my-24">{
keyword.length
? `No matching country or province "${keyword}" found.`
: 'Please type the name of the country that you want to search.'
}</h3>
)
}
</DataSearch>
</>
)
}