mirror of
https://github.com/covidpass-org/covidpass.git
synced 2025-02-23 15:07:44 +01:00
Create custom hook to get pass count
This commit is contained in:
parent
f4010d8918
commit
8a33d2097e
@ -1,9 +1,13 @@
|
||||
import {useTranslation} from 'next-i18next';
|
||||
|
||||
import usePassCount from '../src/hooks/use_pass_count';
|
||||
|
||||
import Link from 'next/link'
|
||||
|
||||
function Logo(): JSX.Element {
|
||||
const { t } = useTranslation('common');
|
||||
const passCount = usePassCount();
|
||||
const displayPassCount = (passCount? ` - ${passCount.toLocaleString()} receipts processed to date!` : '');
|
||||
|
||||
return (
|
||||
<Link href="/">
|
||||
@ -21,6 +25,7 @@ function Logo(): JSX.Element {
|
||||
</svg>
|
||||
<h1 className="text-3xl font-bold">
|
||||
{t('common:title')}
|
||||
{displayPassCount}
|
||||
</h1>
|
||||
</a>
|
||||
</Link>
|
||||
|
@ -1,6 +1,8 @@
|
||||
import React, {useEffect, useState} from "react";
|
||||
import React from "react";
|
||||
import {useTranslation} from 'next-i18next';
|
||||
|
||||
import usePassCount from "../src/hooks/use_pass_count";
|
||||
|
||||
import Head from 'next/head'
|
||||
import Logo from './Logo'
|
||||
import Link from 'next/link'
|
||||
@ -11,36 +13,8 @@ interface PageProps {
|
||||
|
||||
function Page(props: PageProps): JSX.Element {
|
||||
const { t } = useTranslation('common');
|
||||
const [passCount, setPassCount] = useState<string>('');
|
||||
const hitcountHost = 'https://stats.vaccine-ontario.ca';
|
||||
|
||||
useEffect(() => {
|
||||
if (passCount.length == 0) {
|
||||
getPassCount();
|
||||
}
|
||||
}, []);
|
||||
|
||||
const getPassCount = async () => {
|
||||
const hitCount = await getHitCount();
|
||||
setPassCount(hitCount);
|
||||
};
|
||||
|
||||
async function getHitCount() {
|
||||
|
||||
try {
|
||||
const request = `${hitcountHost}/nocount?url=pass.vaccine-ontario.ca`;
|
||||
|
||||
let response = await fetch(request);
|
||||
const counter = await response.text();
|
||||
|
||||
return Promise.resolve(counter);
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
const passCount = usePassCount();
|
||||
const displayPassCount = (passCount? ` - ${passCount.toLocaleString()} receipts processed to date!` : '');
|
||||
|
||||
return (
|
||||
|
35
src/hooks/use_pass_count.tsx
Normal file
35
src/hooks/use_pass_count.tsx
Normal file
@ -0,0 +1,35 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
export default function usePassCount() {
|
||||
const [passCount, setPassCount] = useState<string>('');
|
||||
const hitcountHost = 'https://stats.vaccine-ontario.ca';
|
||||
|
||||
useEffect(() => {
|
||||
if (passCount.length == 0) {
|
||||
getPassCount();
|
||||
}
|
||||
}, []);
|
||||
|
||||
const getPassCount = async () => {
|
||||
const hitCount = await getHitCount();
|
||||
setPassCount(hitCount);
|
||||
};
|
||||
|
||||
async function getHitCount() {
|
||||
|
||||
try {
|
||||
const request = `${hitcountHost}/nocount?url=pass.vaccine-ontario.ca`;
|
||||
|
||||
let response = await fetch(request);
|
||||
const counter = await response.text();
|
||||
|
||||
return Promise.resolve(counter);
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
return passCount;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user