расчёт стоимости в корзине
This commit is contained in:
parent
b7f06fcf28
commit
cdc56c900b
@ -16,7 +16,7 @@ import {
|
||||
} from "@mui/material";
|
||||
import Input from "@kitUI/input";
|
||||
import { useCartStore } from "@root/stores/cart";
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { GridSelectionModel } from "@mui/x-data-grid";
|
||||
import { testUser } from "@root/stores/mocks/user";
|
||||
import { useDiscountStore } from "@root/stores/discounts";
|
||||
@ -24,11 +24,30 @@ import { calcCartData, createCartItem, findDiscountFactor, formatDiscountFactor
|
||||
import { useTariffStore } from "@root/stores/tariffs";
|
||||
import { AnyDiscount, CartItemTotal } from "@root/model/cart";
|
||||
import { findPrivilegeById } from "@root/stores/privileges";
|
||||
import { Privilege } from "@root/model/tariff";
|
||||
import { enqueueSnackbar } from "notistack";
|
||||
import axios from "axios";
|
||||
|
||||
interface Props {
|
||||
selectedTariffs: GridSelectionModel;
|
||||
}
|
||||
|
||||
interface MergedTariff {
|
||||
_id: string;
|
||||
id: string;
|
||||
name: string;
|
||||
privilegeId: string;
|
||||
serviceName: string;
|
||||
price: number;
|
||||
isCustom: boolean;
|
||||
createdAt: string;
|
||||
isDeleted: boolean;
|
||||
amount: number;
|
||||
customPricePerUnit?: number;
|
||||
privilegies: Privilege[];
|
||||
isFront: boolean;
|
||||
}
|
||||
|
||||
export default function Cart({ selectedTariffs }: Props) {
|
||||
const tariffs = useTariffStore((store) => store.tariffs);
|
||||
const discounts = useDiscountStore((store) => store.discounts);
|
||||
@ -39,6 +58,43 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||
const [isNonCommercial, setIsNonCommercial] = useState<boolean>(false);
|
||||
|
||||
const exampleTariffs = useTariffStore((state) => state.tariffs);
|
||||
const [tariffss, setTariffs] = useState<any>();
|
||||
const mergeTariffs: MergedTariff[] = [...exampleTariffs, ...(tariffss || [])];
|
||||
|
||||
console.log(cartTotal, "cartTotal");
|
||||
|
||||
const getTariffs = () => {
|
||||
axios({
|
||||
method: "get",
|
||||
url: "https://admin.pena.digital/strator/tariff",
|
||||
})
|
||||
.then((data: any) => {
|
||||
setTariffs(data.data.tariffs);
|
||||
|
||||
// data.data.tariffs.forEach(async (t:any) => {
|
||||
// if (t._id) {
|
||||
// console.log(t._id)
|
||||
// await axios({
|
||||
// method: "delete",
|
||||
// url: "https://admin.pena.digital/strator/tariff/delete",
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${token}`,
|
||||
// },
|
||||
// data: { id: t._id },
|
||||
// });
|
||||
// }
|
||||
|
||||
// })
|
||||
})
|
||||
.catch((error) => {
|
||||
enqueueSnackbar("Ошибка получения тарифов");
|
||||
});
|
||||
};
|
||||
useEffect(() => {
|
||||
getTariffs();
|
||||
}, []);
|
||||
|
||||
const cartRows = cartTotal?.items.map((cartItemTotal) => {
|
||||
const privilege = findPrivilegeById(cartItemTotal.tariff.privilegeId);
|
||||
|
||||
@ -96,13 +152,14 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
</Box>
|
||||
);
|
||||
|
||||
function handleCalcCartClick() { //рассчитать
|
||||
const cartTariffs = tariffs.filter((tariff) => selectedTariffs.includes(tariff.id));
|
||||
function handleCalcCartClick() {
|
||||
//рассчитать
|
||||
const cartTariffs = mergeTariffs.filter((tariff) => selectedTariffs.includes(tariff._id ? tariff._id : tariff.id));
|
||||
const cartItems = cartTariffs.map((tariff) => createCartItem(tariff));
|
||||
console.log(cartTariffs)
|
||||
console.log(cartItems)
|
||||
console.log("selectedTariffs")
|
||||
console.log(selectedTariffs)
|
||||
console.log(cartTariffs);
|
||||
console.log(cartItems);
|
||||
console.log("selectedTariffs");
|
||||
console.log(selectedTariffs);
|
||||
|
||||
let loyaltyValue = parseInt(loyaltyField);
|
||||
|
||||
@ -110,12 +167,14 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
|
||||
const activeDiscounts = discounts.filter((discount) => !discount.disabled);
|
||||
|
||||
console.log({ user: testUser,
|
||||
console.log({
|
||||
user: testUser,
|
||||
purchasesAmount: loyaltyValue,
|
||||
cartItems,
|
||||
discounts: activeDiscounts,
|
||||
isNonCommercial,
|
||||
coupon: couponField,})
|
||||
coupon: couponField,
|
||||
});
|
||||
const cartData = calcCartData({
|
||||
user: testUser,
|
||||
purchasesAmount: loyaltyValue,
|
||||
|
@ -16,7 +16,7 @@ import axios from "axios";
|
||||
import { authStore } from "@root/stores/auth";
|
||||
import DeleteModal from "@root/kitUI/DeleteModal";
|
||||
import EditModal from "./EditModal";
|
||||
import {TariffDG, Privilege} from "./types"
|
||||
import { TariffDG, Privilege } from "./types";
|
||||
|
||||
interface Props {
|
||||
selectedTariffs: GridSelectionModel;
|
||||
@ -36,12 +36,12 @@ interface MergedTariff {
|
||||
amount?: number;
|
||||
customPricePerUnit?: number;
|
||||
privilegies: Privilege[];
|
||||
isFront: boolean
|
||||
isFront: boolean;
|
||||
}
|
||||
|
||||
export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Props) {
|
||||
const { token } = authStore();
|
||||
|
||||
|
||||
const exampleTariffs = useTariffStore((state) => state.tariffs);
|
||||
const [tariffs, setTariffs] = useState<any>();
|
||||
const [deletedRows, setDeletedRows] = useState<string[]>([]);
|
||||
@ -51,8 +51,6 @@ export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Pr
|
||||
const [errorDelete, setErrorDelete] = useState(false);
|
||||
|
||||
const mergeTariffs: MergedTariff[] = [...exampleTariffs, ...(tariffs || [])];
|
||||
console.log("exampleTariffs")
|
||||
console.log(exampleTariffs)
|
||||
|
||||
const tariffDeleteDataGrid = async (tarifIid: string) => {
|
||||
if (exampleTariffs.find((tariff) => tariff.id === tarifIid)) {
|
||||
@ -72,7 +70,7 @@ console.log(exampleTariffs)
|
||||
});
|
||||
setDeletedRows((prevDeletedRows) => [...prevDeletedRows, tarifIid]);
|
||||
enqueueSnackbar("Тариф удалён");
|
||||
getTariffs()
|
||||
getTariffs();
|
||||
} catch (error: any) {
|
||||
setErrorDelete(true);
|
||||
enqueueSnackbar("Ошибка удаления :", error.message);
|
||||
@ -109,7 +107,7 @@ console.log(exampleTariffs)
|
||||
enqueueSnackbar(error.message);
|
||||
}
|
||||
}
|
||||
getTariffs()
|
||||
getTariffs();
|
||||
|
||||
enqueueSnackbar(`Deleted: ${deleted.join(", ")}`);
|
||||
enqueueSnackbar(`Not deleted: ${notDeleted.join(", ")}`);
|
||||
@ -120,33 +118,31 @@ console.log(exampleTariffs)
|
||||
method: "get",
|
||||
url: "https://admin.pena.digital/strator/tariff",
|
||||
})
|
||||
.then((data:any) => {
|
||||
setTariffs(data.data.tariffs)
|
||||
.then((data: any) => {
|
||||
setTariffs(data.data.tariffs);
|
||||
|
||||
// data.data.tariffs.forEach(async (t:any) => {
|
||||
// if (t._id) {
|
||||
// console.log(t._id)
|
||||
// await axios({
|
||||
// method: "delete",
|
||||
// url: "https://admin.pena.digital/strator/tariff/delete",
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${token}`,
|
||||
// },
|
||||
// data: { id: t._id },
|
||||
// });
|
||||
// }
|
||||
|
||||
// })
|
||||
})
|
||||
.catch((error) => {
|
||||
enqueueSnackbar("Ошибка получения тарифов");
|
||||
})
|
||||
}
|
||||
// data.data.tariffs.forEach(async (t:any) => {
|
||||
// if (t._id) {
|
||||
// console.log(t._id)
|
||||
// await axios({
|
||||
// method: "delete",
|
||||
// url: "https://admin.pena.digital/strator/tariff/delete",
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${token}`,
|
||||
// },
|
||||
// data: { id: t._id },
|
||||
// });
|
||||
// }
|
||||
|
||||
// })
|
||||
})
|
||||
.catch((error) => {
|
||||
enqueueSnackbar("Ошибка получения тарифов");
|
||||
});
|
||||
};
|
||||
useEffect(() => {
|
||||
getTariffs()
|
||||
}, [])
|
||||
|
||||
|
||||
getTariffs();
|
||||
}, []);
|
||||
|
||||
const columns: GridColDef[] = [
|
||||
{ field: "id", headerName: "ID", width: 100 },
|
||||
@ -181,10 +177,9 @@ console.log(exampleTariffs)
|
||||
renderCell: ({ row }) => {
|
||||
// console.log(row)
|
||||
return (
|
||||
<IconButton onClick={() => setChangedTariff(row)}>
|
||||
<ModeEditOutlineOutlinedIcon />
|
||||
</IconButton>
|
||||
|
||||
<IconButton onClick={() => setChangedTariff(row)}>
|
||||
<ModeEditOutlineOutlinedIcon />
|
||||
</IconButton>
|
||||
);
|
||||
},
|
||||
},
|
||||
@ -195,13 +190,12 @@ console.log(exampleTariffs)
|
||||
return name[0];
|
||||
};
|
||||
|
||||
|
||||
console.log(mergeTariffs)
|
||||
console.log(mergeTariffs);
|
||||
const gridData = mergeTariffs
|
||||
.filter((tariff) => !tariff.isDeleted)
|
||||
.map((tariff) => {
|
||||
console.log(tariff)
|
||||
return {
|
||||
.filter((tariff) => !tariff.isDeleted)
|
||||
.map((tariff) => {
|
||||
console.log(tariff);
|
||||
return {
|
||||
id: tariff._id ? tariff._id : tariff.id,
|
||||
name: tariff.name,
|
||||
serviceName: SERVICE_LIST.find(
|
||||
@ -221,9 +215,9 @@ console.log(mergeTariffs)
|
||||
total: tariff.amount
|
||||
? tariff.amount * (tariff.customPricePerUnit ?? findPrivilegeById(tariff.privilegeId)?.price ?? 0)
|
||||
: 0,
|
||||
isFront: tariff.isFront ? tariff.isFront : false
|
||||
|
||||
}});
|
||||
isFront: tariff.isFront ? tariff.isFront : false,
|
||||
};
|
||||
});
|
||||
|
||||
const selectedTariff = gridData.find((tariff) => tariff.id === selectedTariffs[0]);
|
||||
const selectedTariffPrivilege = selectedTariff ? selectedTariff.privilege : [];
|
||||
@ -269,10 +263,12 @@ console.log(mergeTariffs)
|
||||
tariffId={selectedTariffs}
|
||||
tariffName={allName()}
|
||||
open={openDeleteModal}
|
||||
handleClose={() => {setOpenDeleteModal(false)}}
|
||||
handleClose={() => {
|
||||
setOpenDeleteModal(false);
|
||||
}}
|
||||
/>
|
||||
{console.log(changedTariff)}
|
||||
<EditModal tariff={changedTariff} getTariffs={getTariffs}/>
|
||||
<EditModal tariff={changedTariff} getTariffs={getTariffs} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { Tariff } from "@root/model/tariff";
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
import { exampleTariffs } from "./mocks/tariffs";
|
||||
|
||||
interface TariffStore {
|
||||
tariffs: Tariff[];
|
||||
@ -22,8 +21,7 @@ export const useTariffStore = create<TariffStore>()(
|
||||
export const addTariffs = (newTariffs: Tariff[]) =>
|
||||
useTariffStore.setState((state) => ({ tariffs: [...state.tariffs, ...newTariffs] }));
|
||||
|
||||
export const updateTariffs = (newTariff: Tariff[]) =>
|
||||
useTariffStore.setState((state) => ({ tariffs: newTariff }));
|
||||
export const updateTariffs = (newTariff: Tariff[]) => useTariffStore.setState((state) => ({ tariffs: newTariff }));
|
||||
|
||||
export const deleteTariffs = (tariffId: string) =>
|
||||
useTariffStore.setState((state) => ({
|
||||
|
Loading…
Reference in New Issue
Block a user