From c0a89d3f6923bb2e40b1f720f65e56e09be8d8f4 Mon Sep 17 00:00:00 2001 From: JaiPandey425 Date: Fri, 5 Dec 2025 17:47:58 +0530 Subject: [PATCH] profile payment update --- src/electron/ipc/ipcHandler.ts | 43 ++- src/electron/preload.cts | 6 + .../SettingModal/ChangePaymentModal.tsx | 275 ++++++++++-------- src/ui/pages/Settings/Settings.tsx | 19 +- types.d.ts | 13 +- 5 files changed, 216 insertions(+), 140 deletions(-) diff --git a/src/electron/ipc/ipcHandler.ts b/src/electron/ipc/ipcHandler.ts index 3c31f29..edaa1df 100644 --- a/src/electron/ipc/ipcHandler.ts +++ b/src/electron/ipc/ipcHandler.ts @@ -407,7 +407,7 @@ ipcMain.handle("get-payslips", async (event, vaId: string) => { console.log("get-payslips", vaId) try { const response = await api.get(`/paySlip/${vaId}`); - console.log(response.data , "Line 409") + console.log(response.data, "Line 409") return { success: true, data: response.data || [], @@ -493,6 +493,47 @@ ipcMain.handle("get-adjustments-list", async (event, vaId: string) => { }); +ipcMain.handle("update-payment", async (event, data: Record) => { + console.log("data-update-payment", data); + + // Build userDetails object + const userDetails: Record = { + paymentMethod: data.paymentMethod, + }; + + // Add payment-specific fields + if (data.paymentMethod === "wise" && data.wiseEmail) { + userDetails.wiseEmail = data.wiseEmail; + // Clear veemAccount when switching to wise + userDetails.veemAccount = ""; + } else if (data.paymentMethod === "veem" && data.veemAccount) { + userDetails.veemAccount = data.veemAccount; + // Clear wiseEmail when switching to veem + userDetails.wiseEmail = ""; + } + + // Build final payload + const payload = { + userDetails: userDetails, + }; + + console.log("Update Payment payload", payload); + + try { + const response = await api.patch("/profile", payload); + console.log("Update-payment-response", response); + return { success: true, data: response?.data?.userDetails }; + } catch (error: any) { + console.error("❌ Update payment error:", error.message); + return { + success: false, + message: error.response?.data?.message || "Payment update failed", + }; + } +}); + + + export const sendTokenExpired = () => { console.log("⚠️ Sending token-expired event to renderer"); diff --git a/src/electron/preload.cts b/src/electron/preload.cts index 668496d..870cf71 100644 --- a/src/electron/preload.cts +++ b/src/electron/preload.cts @@ -34,6 +34,12 @@ contextBridge.exposeInMainWorld('electron', { changePassword: (data: { oldPassword: string; password: string; cPassword: string }) => ipcRenderer.invoke("change-password", data), + updatePayment: (data: { + paymentMethod: string; + wiseEmail?: string; + veemAccount?: string; + }) => ipcRenderer.invoke('update-payment', data), + onTokenExpired: (callback) => { // Remove old listeners ipcRenderer.removeAllListeners("token-expired"); diff --git a/src/ui/pages/Settings/SettingModal/ChangePaymentModal.tsx b/src/ui/pages/Settings/SettingModal/ChangePaymentModal.tsx index 6c08447..3dc248e 100644 --- a/src/ui/pages/Settings/SettingModal/ChangePaymentModal.tsx +++ b/src/ui/pages/Settings/SettingModal/ChangePaymentModal.tsx @@ -2,156 +2,189 @@ import React, { useState, useEffect } from "react"; import { CreditCard } from "lucide-react"; import { useSelector, useDispatch } from "react-redux"; import { RootState } from "../../../store"; +import { setProfile } from "../../../store/profileSlice"; interface ChangePaymentModalProps { - isOpen: boolean; - onClose: () => void; + isOpen: boolean; + onClose: () => void; } const ChangePaymentModal: React.FC = ({ - isOpen, - onClose, + isOpen, + onClose, }) => { - const dispatch = useDispatch(); - const { profile } = useSelector((state: RootState) => state.profile); + const dispatch = useDispatch(); + const { profile } = useSelector((state: RootState) => state.profile); - const [method, setMethod] = useState(profile?.paymentMethod || ""); - const [wiseEmail, setWiseEmail] = useState(profile?.wiseEmail || ""); - const [veemAccount, setVeemAccount] = useState(profile?.veemAccount || ""); + const [method, setMethod] = useState(profile?.paymentMethod || ""); + const [wiseEmail, setWiseEmail] = useState(profile?.wiseEmail || ""); + const [veemAccount, setVeemAccount] = useState(profile?.veemAccount || ""); - const [loading, setLoading] = useState(false); - const [error, setError] = useState(""); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(""); - useEffect(() => { - if (isOpen) { - setMethod(profile?.paymentMethod || ""); - setWiseEmail(profile?.wiseEmail || ""); - setVeemAccount(profile?.veemAccount || ""); - setError(""); - } - }, [isOpen]); + useEffect(() => { + if (isOpen) { + setMethod(profile?.paymentMethod || ""); + setWiseEmail(profile?.wiseEmail || ""); + setVeemAccount(profile?.veemAccount || ""); + setError(""); + } + }, [isOpen]); - if (!isOpen) return null; + if (!isOpen) return null; - const handleSubmit = async () => { - setError(""); + const handleSubmit = async () => { + setError(""); - // Validation - if (!method) { - return setError("Please select a payment method."); - } + // Validation + if (!method) { + return setError("Please select a payment method."); + } - if (method === "wise" && !wiseEmail.trim()) { - return setError("Wise email is required."); - } + if (method === "wise" && !wiseEmail.trim()) { + return setError("Wise email is required."); + } - if (method === "wise" && !/^\S+@\S+\.\S+$/.test(wiseEmail)) { - return setError("Enter a valid Wise email."); - } + if (method === "wise" && !/^\S+@\S+\.\S+$/.test(wiseEmail)) { + return setError("Enter a valid Wise email."); + } - if (method === "veem" && !veemAccount.trim()) { - return setError("Veem account number is required."); - } + if (method === "veem" && !veemAccount.trim()) { + return setError("Veem account number is required."); + } - setLoading(true); + setLoading(true); - // try { - // // Replace with your electron API - // await window.electron.updatePayment({ - // method, - // wiseEmail: method === "wise" ? wiseEmail.trim() : "", - // veemAccount: method === "veem" ? veemAccount.trim() : "", - // }); + try { + // Prepare data for API + const updateData: any = { + paymentMethod: method + }; - // // Refresh or update Redux - // // dispatch(updateProfile({...})); + // Add appropriate field based on selection + if (method === "wise") { + updateData.wiseEmail = wiseEmail.trim(); + } else if (method === "veem") { + updateData.veemAccount = veemAccount.trim(); + } - // onClose(); - // } catch (err) { - // setError("Failed to update payment info. Try again."); - // } + console.log("Sending to electron:", updateData); - setLoading(false); - }; + // Call Electron API + const result = await window.electron.updatePayment(updateData); - return ( - -
+ console.log(result) -

- Change Payment Method -

+ if (result.success) { + // Prepare data for Redux update (clear unused field) + const reduxUpdateData: any = { + paymentMethod: method + }; - {/* Error */} - {error && ( -
{error}
- )} + if (method === "wise") { + reduxUpdateData.wiseEmail = wiseEmail.trim(); + reduxUpdateData.veemAccount = ""; // Clear veem + } else if (method === "veem") { + reduxUpdateData.veemAccount = veemAccount.trim(); + reduxUpdateData.wiseEmail = ""; // Clear wise + } - {/* Dropdown */} -
- + // Update Redux store + dispatch(setProfile(result.data)); - -
+ // Show success message if needed + // toast.success("Payment method updated successfully"); - {/* Wise Email */} - {method === "wise" && ( -
- - setWiseEmail(e.target.value)} - /> -
- )} + onClose(); + } else { + setError(result.message || "Failed to update payment info."); + } + } catch (err: any) { + setError(err.message || "Failed to update payment info. Try again."); + } finally { + setLoading(false); + } + }; - {/* Veem Account */} - {method === "veem" && ( -
- - setVeemAccount(e.target.value)} - /> -
- )} + return ( + +
-
- +

+ Change Payment Method +

- -
-
-
- ); + {/* Error */} + {error && ( +
{error}
+ )} + + {/* Dropdown */} +
+ + + +
+ + {/* Wise Email */} + {method === "wise" && ( +
+ + setWiseEmail(e.target.value)} + /> +
+ )} + + {/* Veem Account */} + {method === "veem" && ( +
+ + setVeemAccount(e.target.value)} + /> +
+ )} + +
+ + + +
+
+
+ ); }; export default ChangePaymentModal; diff --git a/src/ui/pages/Settings/Settings.tsx b/src/ui/pages/Settings/Settings.tsx index 80d8e26..458cf81 100644 --- a/src/ui/pages/Settings/Settings.tsx +++ b/src/ui/pages/Settings/Settings.tsx @@ -104,21 +104,10 @@ const Settings: React.FC = () => { - {profile?.paymentMethod === "wise" && ( + {profile?.veemAccount && (

- 💳 Wise -

-

- {maskLast5(profile?.wiseEmail)} -

-
- )} - - {profile?.paymentMethod === "veem" && ( -
-

- 🏦 Veem + 🏦 Veem Account

{profile?.veemAccount || "N/A"} @@ -128,10 +117,10 @@ const Settings: React.FC = () => { {profile?.wiseEmail&& (

-

+

🏦 Wise Email

-

+

{profile?.wiseEmail || "N/A"}

diff --git a/types.d.ts b/types.d.ts index 1c693fb..8f20363 100644 --- a/types.d.ts +++ b/types.d.ts @@ -154,16 +154,16 @@ interface Window { vaId: string; projectId: string; type: string; - time:string + time: string }) => Promise; stopProjectTracking: (data: { vaId: string; projectId: string; type: string; - time:string + time: string }) => Promise; trackingTabsOpeningClosing: (data: { - id?: string; + id?: string; logId: string; projectId: string; title: string; @@ -193,6 +193,13 @@ interface Window { cPassword: string; }) => Promise<{ success: boolean; message?: string; data?: any }>; + // In your electron API handler + updatePayment: (data: { + paymentMethod: string; + wiseEmail?: string; + veemAccount?: string; + }) => Promise<{ success: boolean; message?: string; data?: any }>; + onTokenExpired: (callback: () => void) => void;