mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-08-03 21:17:44 +00:00
fix(devices): fix payment amount generation by passing actual values 🐛
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 7m43s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 7m43s
This commit is contained in:
@ -1,5 +1,10 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { useAtom, useAtomValue, useSetAtom } from "jotai";
|
||||||
|
import { CircleDollarSign, Loader2 } from "lucide-react";
|
||||||
|
import { redirect, usePathname } from "next/navigation";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { toast } from "sonner";
|
||||||
import { createPayment } from "@/actions/payment";
|
import { createPayment } from "@/actions/payment";
|
||||||
import DeviceCard from "@/components/device-card";
|
import DeviceCard from "@/components/device-card";
|
||||||
import NumberInput from "@/components/number-input";
|
import NumberInput from "@/components/number-input";
|
||||||
@ -7,41 +12,21 @@ import { Button } from "@/components/ui/button";
|
|||||||
import { deviceCartAtom, numberOfMonths } from "@/lib/atoms";
|
import { deviceCartAtom, numberOfMonths } from "@/lib/atoms";
|
||||||
import type { NewPayment } from "@/lib/backend-types";
|
import type { NewPayment } from "@/lib/backend-types";
|
||||||
import { tryCatch } from "@/utils/tryCatch";
|
import { tryCatch } from "@/utils/tryCatch";
|
||||||
import { useAtom, useAtomValue, useSetAtom } from "jotai";
|
|
||||||
import { CircleDollarSign, Loader2 } from "lucide-react";
|
|
||||||
import { redirect, usePathname } from "next/navigation";
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { toast } from "sonner";
|
|
||||||
import FullPageLoader from "./full-page-loader";
|
import FullPageLoader from "./full-page-loader";
|
||||||
export default function DevicesForPayment() {
|
export default function DevicesForPayment() {
|
||||||
const baseAmount = 100;
|
|
||||||
const discountPercentage = 75;
|
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const devices = useAtomValue(deviceCartAtom);
|
const devices = useAtomValue(deviceCartAtom);
|
||||||
const setDeviceCart = useSetAtom(deviceCartAtom);
|
const setDeviceCart = useSetAtom(deviceCartAtom);
|
||||||
const [months, setMonths] = useAtom(numberOfMonths);
|
const [months, setMonths] = useAtom(numberOfMonths);
|
||||||
const [message, setMessage] = useState("");
|
|
||||||
const [disabled, setDisabled] = useState(false);
|
const [disabled, setDisabled] = useState(false);
|
||||||
const [total, setTotal] = useState(0);
|
|
||||||
console.log(total);
|
|
||||||
useEffect(() => {
|
|
||||||
if (months === 7) {
|
|
||||||
setMessage("You will get 1 month free.");
|
|
||||||
} else if (months === 12) {
|
|
||||||
setMessage("You will get 2 months free.");
|
|
||||||
} else {
|
|
||||||
setMessage("");
|
|
||||||
}
|
|
||||||
setTotal(baseAmount + (devices.length + 1 - 1) * discountPercentage);
|
|
||||||
}, [months, devices.length]);
|
|
||||||
|
|
||||||
if (pathname === "/payments") {
|
if (pathname === "/payments") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data: NewPayment = {
|
const data: NewPayment = {
|
||||||
amount: 100,
|
number_of_months: months,
|
||||||
number_of_months: 2,
|
|
||||||
device_ids: devices.map((device) => device.id),
|
device_ids: devices.map((device) => device.id),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -63,11 +48,7 @@ export default function DevicesForPayment() {
|
|||||||
maxAllowed={12}
|
maxAllowed={12}
|
||||||
isDisabled={devices.length === 0}
|
isDisabled={devices.length === 0}
|
||||||
/>
|
/>
|
||||||
{message && (
|
|
||||||
<span className="title-bg text-lime-800 bg-lime-100/50 dark:text-lime-100 rounded text-center p-2 w-full">
|
|
||||||
{message}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
|
@ -95,7 +95,6 @@ export interface Payment {
|
|||||||
export interface NewPayment {
|
export interface NewPayment {
|
||||||
device_ids: number[];
|
device_ids: number[];
|
||||||
number_of_months: number;
|
number_of_months: number;
|
||||||
amount: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WalletTransaction {
|
export interface WalletTransaction {
|
||||||
|
@ -190,12 +190,11 @@ export async function blockDeviceAction(
|
|||||||
(action === "simple-block" ? "Blocked by admin" : "")
|
(action === "simple-block" ? "Blocked by admin" : "")
|
||||||
: isBlocking
|
: isBlocking
|
||||||
? "Blocked by parent"
|
? "Blocked by parent"
|
||||||
: "",
|
: "-",
|
||||||
blocked_by: session?.user?.is_superuser ? blocked_by : "PARENT",
|
blocked_by: session?.user?.is_superuser ? blocked_by : "PARENT",
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await handleApiResponse<Device>(response, "blockDeviceAction");
|
await handleApiResponse<Device>(response, "blockDeviceAction");
|
||||||
|
|
||||||
revalidatePath("/devices");
|
revalidatePath("/devices");
|
||||||
|
Reference in New Issue
Block a user