mirror of
https://github.com/BreizhHardware/Jellystat.git
synced 2026-01-18 16:27:20 +01:00
77 lines
1.7 KiB
JavaScript
77 lines
1.7 KiB
JavaScript
import { useState, useEffect } from "react";
|
|
import axios from "../../../lib/axios_instance";
|
|
import Config from "../../../lib/config";
|
|
import ItemStatComponent from "./ItemStatComponent";
|
|
import { Trans } from "react-i18next";
|
|
|
|
function MVMovies(props) {
|
|
const [data, setData] = useState();
|
|
const [days, setDays] = useState(30);
|
|
|
|
const [config, setConfig] = useState(null);
|
|
|
|
|
|
useEffect(() => {
|
|
const fetchConfig = async () => {
|
|
try {
|
|
const newConfig = await Config.getConfig();
|
|
setConfig(newConfig);
|
|
} catch (error) {
|
|
if (error.code === "ERR_NETWORK") {
|
|
console.log(error);
|
|
}
|
|
}
|
|
};
|
|
|
|
const fetchLibraries = () => {
|
|
if (config) {
|
|
const url = `/stats/getMostViewedByType`;
|
|
|
|
axios
|
|
.post(url, {days:props.days, type:'Audio'}, {
|
|
headers: {
|
|
Authorization: `Bearer ${config.token}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
})
|
|
.then((data) => {
|
|
setData(data.data);
|
|
})
|
|
.catch((error) => {
|
|
console.log(error);
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
if (!config) {
|
|
fetchConfig();
|
|
}
|
|
|
|
if (!data) {
|
|
fetchLibraries();
|
|
}
|
|
if (days !== props.days) {
|
|
setDays(props.days);
|
|
fetchLibraries();
|
|
}
|
|
|
|
|
|
const intervalId = setInterval(fetchLibraries, 60000 * 5);
|
|
return () => clearInterval(intervalId);
|
|
}, [data, config, days,props.days]);
|
|
|
|
if (!data || data.length === 0) {
|
|
return <></>;
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
<ItemStatComponent base_url={config.hostUrl} data={data} heading={<Trans i18nKey="STAT_CARDS.MOST_LISTENED_MUSIC" />} units={<Trans i18nKey="UNITS.PLAYS" />} isAudio={true}/>
|
|
);
|
|
}
|
|
|
|
export default MVMovies;
|