import { useEffect, useMemo, useState } from "react"; import { useTranslations } from "@/hooks/translations"; import { useApi, Assets } from "@/api"; import { saveFile, buildFilename } from "../WallpaperPage/utils"; import styles from "./styles.module.css"; import { useSelector } from "react-redux"; import { selectors } from "@/store"; import { getCategoryIdByZodiacSign, getZodiacSignByDate, } from "@/services/zodiac-sign"; import { getRandomArbitrary } from "@/services/random-value"; function WallpapersZodiacSign(): JSX.Element { const api = useApi(); const { i18n } = useTranslations(); const locale = i18n.language; const [asset, setAsset] = useState(); const birthdate = useSelector(selectors.selectBirthdate); const zodiacSign = useMemo(() => getZodiacSignByDate(birthdate), [birthdate]); const getZodiacWallpaper = async () => { const { asset_categories } = await api.getAssetCategories({ locale }); const categoryId = getCategoryIdByZodiacSign(zodiacSign, asset_categories); const { assets } = await api.getAssets({ category: String(categoryId || "1"), }); const asset = assets[getRandomArbitrary(0, assets.length - 1)]; setAsset(asset); }; useEffect(() => { getZodiacWallpaper(); }, [api, locale, zodiacSign]); const saveImage = () => asset && saveFile( asset.url.replace("http://", "https://"), buildFilename(zodiacSign) ); return (
Save image Get new image
); } export default WallpapersZodiacSign;