2020-11-15 02:43:23 +01:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import {
|
|
|
|
|
getListensReport,
|
|
|
|
|
getRecentListens,
|
|
|
|
|
getTopAlbums,
|
|
|
|
|
getTopArtists,
|
|
|
|
|
} from "../api/api";
|
2020-09-05 23:35:53 +02:00
|
|
|
import { ListenReportOptions } from "../api/entities/listen-report-options";
|
|
|
|
|
import { PaginationOptions } from "../api/entities/pagination-options";
|
2020-11-15 02:43:23 +01:00
|
|
|
import { TopAlbumsOptions } from "../api/entities/top-albums-options";
|
2020-09-05 23:35:53 +02:00
|
|
|
import { TopArtistsOptions } from "../api/entities/top-artists-options";
|
|
|
|
|
import { useApiClient } from "./use-api-client";
|
|
|
|
|
import { useAsync } from "./use-async";
|
|
|
|
|
|
|
|
|
|
const INITIAL_EMPTY_ARRAY: [] = [];
|
|
|
|
|
Object.freeze(INITIAL_EMPTY_ARRAY);
|
|
|
|
|
|
|
|
|
|
export const useRecentListens = (options: PaginationOptions) => {
|
|
|
|
|
const { client } = useApiClient();
|
|
|
|
|
|
|
|
|
|
const fetchData = useMemo(() => () => getRecentListens(options, client), [
|
|
|
|
|
options,
|
|
|
|
|
client,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const { value, pending: isLoading, error, reload } = useAsync(
|
|
|
|
|
fetchData,
|
|
|
|
|
undefined
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const recentListens = value ? value.items : [];
|
|
|
|
|
const paginationMeta = value ? value.meta : undefined;
|
|
|
|
|
|
|
|
|
|
return { recentListens, paginationMeta, isLoading, error, reload };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useListensReport = (options: ListenReportOptions) => {
|
|
|
|
|
const { client } = useApiClient();
|
|
|
|
|
|
|
|
|
|
const fetchData = useMemo(() => () => getListensReport(options, client), [
|
|
|
|
|
options,
|
|
|
|
|
client,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const { value: report, pending: isLoading, error } = useAsync(
|
|
|
|
|
fetchData,
|
2020-11-15 02:34:22 +01:00
|
|
|
INITIAL_EMPTY_ARRAY
|
2020-09-05 23:35:53 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { report, isLoading, error };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useTopArtists = (options: TopArtistsOptions) => {
|
|
|
|
|
const { client } = useApiClient();
|
|
|
|
|
|
|
|
|
|
const fetchData = useMemo(() => () => getTopArtists(options, client), [
|
|
|
|
|
options,
|
|
|
|
|
client,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const { value: topArtists, pending: isLoading, error } = useAsync(
|
|
|
|
|
fetchData,
|
2020-11-15 02:34:22 +01:00
|
|
|
INITIAL_EMPTY_ARRAY
|
2020-09-05 23:35:53 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { topArtists, isLoading, error };
|
|
|
|
|
};
|
2020-11-15 02:43:23 +01:00
|
|
|
|
|
|
|
|
export const useTopAlbums = (options: TopAlbumsOptions) => {
|
|
|
|
|
const { client } = useApiClient();
|
|
|
|
|
|
|
|
|
|
const fetchData = useMemo(() => () => getTopAlbums(options, client), [
|
|
|
|
|
options,
|
|
|
|
|
client,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const { value: topAlbums, pending: isLoading, error } = useAsync(
|
|
|
|
|
fetchData,
|
|
|
|
|
INITIAL_EMPTY_ARRAY
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { topAlbums, isLoading, error };
|
|
|
|
|
};
|