fix : fix send request before open modal

This commit is contained in:
alij
2024-06-22 18:49:38 +04:30
parent 3dfed214a8
commit 5ff7ec4613
2 changed files with 29 additions and 10 deletions

View File

@@ -4,11 +4,17 @@ import Alert from "../../../../../components/alert/Alert";
import Button from "../../../../../components/button"; import Button from "../../../../../components/button";
import ModalBuy from "./ModalBuy"; import ModalBuy from "./ModalBuy";
import { useState } from "react"; import { useState } from "react";
import { useSelector } from "react-redux";
import { RootState } from "../../../../../redux/store";
import { useNavigate } from "react-router-dom";
import toast from "react-hot-toast";
const ListAllProduct = ({ activeId }: { activeId: number }) => { const ListAllProduct = ({ activeId }: { activeId: number }) => {
const axios = useAxios(); const axios = useAxios();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [configId, setConfigId] = useState(1); const [configId, setConfigId] = useState(1);
const { token } = useSelector((state: RootState) => state.token);
const navigate = useNavigate();
const { data, error, isLoading, isFetching } = useQuery({ const { data, error, isLoading, isFetching } = useQuery({
queryKey: ["configs", activeId], queryKey: ["configs", activeId],
queryFn: () => { queryFn: () => {
@@ -67,15 +73,27 @@ const ListAllProduct = ({ activeId }: { activeId: number }) => {
<div className="w-full h-full flex flex-col" key={item.id}> <div className="w-full h-full flex flex-col" key={item.id}>
<img src={item.image} alt={item.title} /> <img src={item.image} alt={item.title} />
<div className="w-full h-full flex justify-center items-center"> <div className="w-full h-full flex justify-center items-center">
<Button {token ? (
extra="!w-4/5 !bg-secondTheme" <Button
onClick={() => { extra="!w-4/5 !bg-secondTheme"
setIsOpen(true); onClick={() => {
setConfigId(Number(item.id)); setIsOpen(true);
}} setConfigId(Number(item.id));
> }}
خرید >
</Button> خرید
</Button>
) : (
<Button
extra="!w-4/5 !bg-secondTheme"
onClick={() => {
toast.error("برای خرید باید وارد شوید");
navigate("/login");
}}
>
ورود
</Button>
)}
</div> </div>
</div> </div>
); );

View File

@@ -24,6 +24,7 @@ const ModalBuy = ({ isOpen, setIsOpen, configId }: ModalBuyProps) => {
queryFn: () => { queryFn: () => {
return axios.get(`/home/config/${configId}/see`); return axios.get(`/home/config/${configId}/see`);
}, },
enabled: isOpen,
}); });
const [price, setPrice] = useState(0); const [price, setPrice] = useState(0);
@@ -204,7 +205,7 @@ const ModalBuy = ({ isOpen, setIsOpen, configId }: ModalBuyProps) => {
مبلغ قابل پرداخت :{" "} مبلغ قابل پرداخت :{" "}
<span className="px-[2px] rounded-md bg-secondTheme text-gray-950"> <span className="px-[2px] rounded-md bg-secondTheme text-gray-950">
{separateByThree( {separateByThree(
price === 0 ? data?.data?.result?.price : price price === 0 ? data?.data?.result?.price || 0 : price
)} )}
</span> </span>
</h1> </h1>