2020-05-31 23:26:06 +02:00
|
|
|
import React, { useMemo, useState } from "react";
|
|
|
|
|
import { Redirect } from "react-router-dom";
|
|
|
|
|
import { getTopArtists } from "../api/api";
|
2020-11-07 19:14:41 +01:00
|
|
|
import { TimeOptions } from "../api/entities/time-options";
|
2020-05-31 23:26:06 +02:00
|
|
|
import { TimePreset } from "../api/entities/time-preset.enum";
|
2020-11-07 19:14:41 +01:00
|
|
|
import { TopArtistsItem } from "../api/entities/top-artists-item";
|
2020-05-31 23:26:06 +02:00
|
|
|
import { useAsync } from "../hooks/use-async";
|
|
|
|
|
import { useAuth } from "../hooks/use-auth";
|
2020-07-12 17:16:33 +02:00
|
|
|
import { ReportTimeOptions } from "./ReportTimeOptions";
|
2020-11-07 19:14:41 +01:00
|
|
|
|
|
|
|
|
const INITIAL_REPORT_DATA: TopArtistsItem[] = [];
|
2020-05-31 23:26:06 +02:00
|
|
|
|
|
|
|
|
export const ReportTopArtists: React.FC = () => {
|
|
|
|
|
const { user } = useAuth();
|
|
|
|
|
|
2020-07-12 17:16:33 +02:00
|
|
|
const [timeOptions, setTimeOptions] = useState<TimeOptions>({
|
2020-05-31 23:26:06 +02:00
|
|
|
timePreset: TimePreset.LAST_90_DAYS,
|
|
|
|
|
customTimeStart: new Date(0),
|
|
|
|
|
customTimeEnd: new Date(),
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-12 17:16:33 +02:00
|
|
|
const fetchData = useMemo(() => () => getTopArtists({ time: timeOptions }), [
|
|
|
|
|
timeOptions,
|
2020-05-31 23:26:06 +02:00
|
|
|
]);
|
|
|
|
|
|
2020-11-07 19:14:41 +01:00
|
|
|
const { value: report, pending: isLoading } = useAsync(
|
|
|
|
|
fetchData,
|
|
|
|
|
INITIAL_REPORT_DATA
|
|
|
|
|
);
|
2020-05-31 23:26:06 +02:00
|
|
|
|
|
|
|
|
const reportHasItems = !isLoading && report.length !== 0;
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
return <Redirect to="/" />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="md:flex md:justify-center p-4">
|
|
|
|
|
<div className="md:flex-shrink-0 min-w-full xl:min-w-0 xl:w-2/3 max-w-screen-lg">
|
|
|
|
|
<div className="flex justify-between">
|
|
|
|
|
<p className="text-2xl font-normal text-gray-700">Top Artists</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="shadow-xl bg-gray-100 rounded p-5 m-2">
|
2020-07-12 17:16:33 +02:00
|
|
|
<ReportTimeOptions
|
|
|
|
|
timeOptions={timeOptions}
|
|
|
|
|
setTimeOptions={setTimeOptions}
|
2020-05-31 23:26:06 +02:00
|
|
|
/>
|
|
|
|
|
{isLoading && (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="loader rounded-full border-8 h-64 w-64"></div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{!reportHasItems && (
|
|
|
|
|
<div>
|
|
|
|
|
<p>Report is emtpy! :(</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{reportHasItems &&
|
|
|
|
|
report.map(({ artist, count }) => (
|
2020-11-07 19:23:57 +01:00
|
|
|
<div key={artist.id}>
|
2020-05-31 23:26:06 +02:00
|
|
|
{count} - {artist.name}
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|