-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from thanhdanh27600/uat
Uat
- Loading branch information
Showing
24 changed files
with
601 additions
and
214 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
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
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,62 @@ | ||
import Script from 'next/script'; | ||
import { useEffect, useState } from 'react'; | ||
import { Window } from 'types/constants'; | ||
|
||
interface Props { | ||
label: any[]; | ||
value: (string | number | null)[][]; | ||
title?: string; | ||
className?: string; | ||
} | ||
|
||
export const BarChart = (props: Props) => { | ||
const { className, title, label, value } = props; | ||
const [loaded, setLoaded] = useState(false); | ||
const google = Window().google; | ||
|
||
useEffect(() => { | ||
if (!google) return; | ||
|
||
google.charts.load('current', { packages: ['corechart', 'bar'] }); | ||
google.charts.setOnLoadCallback(drawBasic); | ||
|
||
function drawBasic() { | ||
var data = google.visualization.arrayToDataTable([label, ...value]); | ||
|
||
var options = { | ||
title, | ||
backgroundColor: '#fcfcfd', | ||
hAxis: { | ||
minValue: 0, | ||
}, | ||
fontName: 'Roboto Slab', | ||
fontSize: 12, | ||
legend: 'none', | ||
}; | ||
|
||
var chart = new google.visualization.BarChart(document.getElementById('bar-div')); | ||
|
||
chart.draw(data, options); | ||
|
||
return () => { | ||
chart.clearChart(); | ||
}; | ||
} | ||
}, [loaded, google]); | ||
|
||
if (value.length === 0) return null; | ||
|
||
return ( | ||
<> | ||
<Script | ||
onLoad={() => { | ||
setLoaded(true); | ||
}} | ||
type="text/javascript" | ||
src="https://www.gstatic.com/charts/loader.js" | ||
/> | ||
|
||
<div id="bar-div" className={className}></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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Script from 'next/script'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { Window } from 'types/constants'; | ||
|
||
interface Props { | ||
label: (string | number | null)[]; | ||
value: (string | number | null)[][]; | ||
className?: string; | ||
} | ||
|
||
export const GeoChart = (props: Props) => { | ||
const { className, label, value } = props; | ||
const [loaded, setLoaded] = useState(false); | ||
const google = Window().google; | ||
const id = 'regions_div'; | ||
|
||
const drawRegionsMap = useCallback( | ||
function () { | ||
const element = document.getElementById(id); | ||
|
||
var data = google.visualization.arrayToDataTable([label, ...value]); | ||
|
||
var options = { | ||
legend: 'none', | ||
backgroundColor: '#fcfcfd', | ||
tooltip: { | ||
textStyle: { | ||
fontName: 'Roboto Slab', | ||
fontSize: 12, | ||
}, | ||
}, | ||
colorAxis: { minValue: 0, colors: ['#9d8ed1', '#6644de'] }, | ||
}; | ||
|
||
const chart = new google.visualization.GeoChart(element); | ||
|
||
chart.draw(data, options); | ||
}, | ||
[loaded, google], | ||
); | ||
|
||
useEffect(() => { | ||
if (!google) return; | ||
google.charts.load('current', { | ||
packages: ['geochart'], | ||
}); | ||
google.charts.setOnLoadCallback(drawRegionsMap); | ||
}, [loaded, google]); | ||
|
||
return ( | ||
<> | ||
<Script | ||
onLoad={() => { | ||
setLoaded(true); | ||
}} | ||
type="text/javascript" | ||
src="https://www.gstatic.com/charts/loader.js" | ||
/> | ||
|
||
<div id={id} className={className}></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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import clsx from 'clsx'; | ||
import { BarChart } from 'components/atoms/BarChart'; | ||
import { GeoChart } from 'components/atoms/GeoChart'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import toast from 'react-hot-toast'; | ||
import { useQuery } from 'react-query'; | ||
import { getStatsGeo } from 'requests'; | ||
import { HistoryGeoItem } from 'types/stats'; | ||
import { getCountryName } from 'utils/country'; | ||
import { useDimensionWindow } from 'utils/dom'; | ||
import { useTrans } from 'utils/i18next'; | ||
import { parseIntSafe } from 'utils/number'; | ||
import { QueryKey, strictRefetch } from 'utils/requests'; | ||
|
||
const color = '#7354e5'; | ||
|
||
interface Props { | ||
hash: string; | ||
className?: string; | ||
} | ||
|
||
export const HistoryByCountry = (props: Props) => { | ||
const { hash } = props; | ||
const { t, locale } = useTrans(); | ||
const [rerender, setRerender] = useState(0); | ||
const { width } = useDimensionWindow(); | ||
const fetchStatsGeo = useQuery({ | ||
queryKey: QueryKey.STATS_GEO, | ||
queryFn: async () => getStatsGeo({ hash }), | ||
...strictRefetch, | ||
onSuccess(data) { | ||
if (data.history) setData(data.history); | ||
}, | ||
onError(error) { | ||
console.error(error); | ||
toast.error(t('errorNoTracking')); | ||
}, | ||
}); | ||
|
||
const [data, setData] = useState<HistoryGeoItem[]>(fetchStatsGeo.data?.history || []); | ||
|
||
const chartDataGeo = useMemo(() => { | ||
if (!data) return []; | ||
return data.map((data) => [getCountryName(data.countryCode || '') || 'world', data._count.countryCode]); | ||
}, [data]); | ||
|
||
const chartDataBar = useMemo(() => { | ||
if (!data) return []; | ||
let total = data.map((data) => [ | ||
getCountryName(data.countryCode || '') || t('unknown'), | ||
data._count.countryCode, | ||
`color: ${color}`, | ||
]); | ||
if (total.length > 3) { | ||
const others = total.slice(3); | ||
total = [ | ||
...total.slice(0, 3), | ||
others.reduce((prev, cur) => [ | ||
t('otherCountry'), | ||
parseIntSafe(prev[1]) + parseIntSafe(cur[1]), | ||
`color: ${color}`, | ||
]), | ||
]; | ||
} | ||
return total; | ||
}, [data]); | ||
|
||
useEffect(() => { | ||
setRerender((_) => _ + 1); | ||
}, [width, locale]); | ||
|
||
if (!fetchStatsGeo.data) return null; | ||
|
||
return ( | ||
<div> | ||
<div | ||
className={clsx('flex h-full w-full flex-col items-center justify-center gap-4 lg:flex-row', props.className)}> | ||
<GeoChart | ||
key={`geo-${rerender}`} | ||
label={['Country', t('totalClick')]} | ||
value={chartDataGeo} | ||
className="h-[240px] w-full sm:w-[400px] lg:h-[300px] lg:w-[500px]" | ||
/> | ||
<BarChart | ||
key={`bar-${rerender}`} | ||
label={['Country', t('totalClick'), { role: 'style' }]} | ||
value={chartDataBar} | ||
className="w-full sm:w-[400px] md:w-[600px]" | ||
/> | ||
</div> | ||
{chartDataGeo.length === 0 && <p className="mt-4 text-center text-base text-gray-500">{t('noData')}</p>} | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.