diff --git a/src/components/common/button.tsx b/src/components/common/button.tsx index 6cdd875..8c7880a 100644 --- a/src/components/common/button.tsx +++ b/src/components/common/button.tsx @@ -6,7 +6,8 @@ type Props = { title: string type?: "reset" | "button" | "submit" | undefined, pending?: boolean, - theme?: "primary" | "secondary" | "glass" | "light" + theme?: "primary" | "secondary" | "glass" | "light", + onClick?: () => void } export const ButtonComponent: FC = ({ title, type = "button", pending = false, theme = "primary" }) => { diff --git a/src/components/installation-reports/index.tsx b/src/components/installation-reports/index.tsx index 36f35e8..f4e99eb 100644 --- a/src/components/installation-reports/index.tsx +++ b/src/components/installation-reports/index.tsx @@ -1,19 +1,17 @@ -import { useQuery } from "@tanstack/react-query" import { installationReportsCol } from "../../utility/reports" import { Table } from "../common/table" -import { getReportsList } from "../../services/api/reports" import { useState } from "react" import { SortingState } from "@tanstack/react-table" export const InstallationReports = () => { const [sorting, setSorting] = useState([]) - const { data, isLoading } = useQuery({ - queryKey: ["installation-reports-list"], - queryFn: getReportsList - }) + // const { data, isLoading } = useQuery({ + // queryKey: ["installation-reports-list"], + // queryFn: getReportsList + // }) - if (isLoading) return

LODING .....

+ // if (isLoading) return

LODING .....

return ( - +
) } \ No newline at end of file diff --git a/src/components/management-bank-accounts/add-new-cart-dialog.tsx b/src/components/management-bank-accounts/add-new-cart-dialog.tsx index 1897e6c..2061c32 100644 --- a/src/components/management-bank-accounts/add-new-cart-dialog.tsx +++ b/src/components/management-bank-accounts/add-new-cart-dialog.tsx @@ -2,34 +2,40 @@ import { CardAdd } from "iconsax-react" import { DialogComponent } from "../common/dialog" import { FC } from "react" import { Input } from "../common/input" -import { Button } from "@headlessui/react" -import { bankCarts, findBankByPAN } from "../../utility/cart" import { Controller, SubmitHandler, useForm } from "react-hook-form" import { AddNewCartInterface } from "../../types" import { yupResolver } from "@hookform/resolvers/yup" import { AddNewCardSchema } from "../../schema" import { ErrorComponent } from "../common/error" +import { useMutation } from "@tanstack/react-query" +import { createNewCardMutation } from "../../services/api/bank-card" +import toast from "react-hot-toast" +import { ButtonComponent } from "../common/button" type Props = { isOpen: boolean, close: () => void } export const AddNewCardDialog: FC = ({ isOpen, close }) => { + const { mutate, isPending } = useMutation({ + mutationFn: createNewCardMutation + }) const { handleSubmit, formState: { errors }, control } = useForm({ resolver: yupResolver(AddNewCardSchema) }) const createNewCard: SubmitHandler = async (data) => { - const finalData: any = { - id: bankCarts?.length + 1, - bankName: findBankByPAN(data.cartNumber).name, - bankPersianName: findBankByPAN(data.cartNumber).name_farsi, - cartNumber: data?.cartNumber.toString(), - cartOwner: `${data.name} ${data?.family}`, - shabaNumber: data?.shabaNumber, - } - console.log("final-data =>", finalData) - bankCarts.push(finalData) - close() + mutate({ + holderName: `${data?.name} ${data?.family}`, + PAN: data?.PAN, + IBAN: data?.IBAN, + }, { + onSuccess: () => { + toast.success("کارت با موفقیت اضافه شد") + }, + onError: () => { + toast.error("کارت با موفقیت اضافه نشد") + } + }) } return ( }> @@ -47,22 +53,22 @@ export const AddNewCardDialog: FC = ({ isOpen, close }) => { )} /> - ( + (
- +
)} /> - ( + (
- +
)} />
- - + +
diff --git a/src/components/management-bank-accounts/bank-cart.tsx b/src/components/management-bank-accounts/bank-cart.tsx index 829cd8b..7478be0 100644 --- a/src/components/management-bank-accounts/bank-cart.tsx +++ b/src/components/management-bank-accounts/bank-cart.tsx @@ -5,10 +5,11 @@ import { findBankByPAN, formatCartNumber } from "../../utility/cart" type Props = { item: BankCartInterface, - handleOpenDeleteDialog: () => void + handleOpenDeleteDialog: () => void, + setSelectedCard: (value: string | number | null) => void, } -export const BankCart: FC = ({ item, handleOpenDeleteDialog }) => { +export const BankCart: FC = ({ item, handleOpenDeleteDialog, setSelectedCard }) => { return (
@@ -18,7 +19,7 @@ export const BankCart: FC = ({ item, handleOpenDeleteDialog }) => {
- + setSelectedCard(item?.id)} />

{formatCartNumber(item?.cartNumber)}

diff --git a/src/components/management-bank-accounts/delete-card.tsx b/src/components/management-bank-accounts/delete-card.tsx index e408aad..b30838a 100644 --- a/src/components/management-bank-accounts/delete-card.tsx +++ b/src/components/management-bank-accounts/delete-card.tsx @@ -1,19 +1,34 @@ import { FC } from "react" import { DialogComponent } from "../common/dialog" import { Trash } from "iconsax-react" -import { Button } from "@headlessui/react" +import { useMutation } from "@tanstack/react-query" +import { deleteCardMutation } from "../../services/api/bank-card" +import toast from "react-hot-toast" +import { ButtonComponent } from "../common/button" type Props = { isOpen: boolean, - close: () => void + close: () => void, + id: string | number | null } -export const DeleteCardDialog: FC = ({ isOpen, close }) => { +export const DeleteCardDialog: FC = ({ isOpen, close, id }) => { + const { mutate, isPending } = useMutation({ + mutationFn: deleteCardMutation + }) + const handleDeleteSelectedCard = async () => { + await mutate({ + id + }, { + onSuccess: () => toast.success("کارت مورد نظر با موفقیت حذف شد"), + onError: () => toast.error("حذف کارت نا موفق") + }) + } return ( }>
- - + +
) diff --git a/src/components/management-bank-accounts/index.tsx b/src/components/management-bank-accounts/index.tsx index 836bcfb..5695b89 100644 --- a/src/components/management-bank-accounts/index.tsx +++ b/src/components/management-bank-accounts/index.tsx @@ -1,6 +1,5 @@ import { useEffect, useState } from "react" import { BankCartInterface } from "../../types" -import { bankCarts } from "../../utility/cart" import { BankCart } from "./bank-cart" import { CreateNewCart } from "./create-new-cart" import { AddNewCardDialog } from "./add-new-cart-dialog" @@ -9,6 +8,7 @@ import { useQuery } from "@tanstack/react-query" import { getUserCardsList } from "../../services/api/bank-card" export const ManagementBankAccounts = () => { + const [selectedCard, setSelectedCard] = useState(null) const { data, isLoading } = useQuery({ queryKey: ["bank-card"], queryFn: getUserCardsList @@ -29,10 +29,10 @@ export const ManagementBankAccounts = () => { <>
- {bankCarts?.map((item: BankCartInterface) => )} + {data?.data?.map((item: BankCartInterface) => )}
- + ) } \ No newline at end of file diff --git a/src/components/score/index.tsx b/src/components/score/index.tsx index 59bfd9f..c56617a 100644 --- a/src/components/score/index.tsx +++ b/src/components/score/index.tsx @@ -2,19 +2,17 @@ import { useState } from "react" import { Table } from "../common/table" import { SortingState } from "@tanstack/react-table" import { scoresCol } from "../../utility/score" -import { useQuery } from "@tanstack/react-query" -import { getScoreList } from "../../services/api/score" export const Score = () => { const [sorting, setSorting] = useState([]) - const { data, isLoading } = useQuery({ - queryKey: ["score-list"], - queryFn: getScoreList - }) + // const { data, isLoading } = useQuery({ + // queryKey: ["score-list"], + // queryFn: getScoreList + // }) - if (isLoading) return

LOADING ...

+ // if (isLoading) return

LOADING ...

return ( -
+
) } \ No newline at end of file diff --git a/src/components/subscription-report/index.tsx b/src/components/subscription-report/index.tsx index 87f48eb..4bbddca 100644 --- a/src/components/subscription-report/index.tsx +++ b/src/components/subscription-report/index.tsx @@ -2,17 +2,15 @@ import { useState } from "react" import { Table } from "../common/table" import { SortingState } from "@tanstack/react-table" import { subscriptionsCol } from "../../utility/subscription" -import { useQuery } from "@tanstack/react-query" -import { getSubscriptionsList } from "../../services/api/subscription" export const SubscriptionReport = () => { const [sorting, setSorting] = useState([]) - const { data, isLoading } = useQuery({ - queryKey: ["suscriptions-list"], - queryFn: getSubscriptionsList - }) - if (isLoading) return

LOADIN...

+ // const { data, isLoading } = useQuery({ + // queryKey: ["suscriptions-list"], + // queryFn: getSubscriptionsList + // }) + // if (isLoading) return

LOADIN...

return ( -
+
) } \ No newline at end of file diff --git a/src/components/transactions/index.tsx b/src/components/transactions/index.tsx index 99b3443..3f5e5e6 100644 --- a/src/components/transactions/index.tsx +++ b/src/components/transactions/index.tsx @@ -2,19 +2,17 @@ import { useState } from "react" import { Table } from "../common/table" import { SortingState } from "@tanstack/react-table" import { transactionsCol } from "../../utility/transactions" -import { useQuery } from "@tanstack/react-query" -import { getTransactionsList } from "../../services/api/transactions" export const Transactions = () => { const [sorting, setSorting] = useState([]) - const { data, isLoading } = useQuery({ - queryKey: ["transactions-list"], - queryFn: getTransactionsList - }) + // const { data, isLoading } = useQuery({ + // queryKey: ["transactions-list"], + // queryFn: getTransactionsList + // }) - if (isLoading) return

LOADING ....

+ // if (isLoading) return

LOADING ....

return ( -
+
) } \ No newline at end of file diff --git a/src/schema/index.ts b/src/schema/index.ts index 857215f..030b639 100644 --- a/src/schema/index.ts +++ b/src/schema/index.ts @@ -100,14 +100,14 @@ export const MyAccountSchema = yup.object({ export const AddNewCardSchema = yup.object({ name: yup.string().required("وارد کردن نام الزامی میباشد").min(3, "نام نمیتواند کمتر از 3 کاراکتر باشد").max(20, "نام نمیتواند بیشتر از 20 کاراکتر باشد"), family: yup.string().required("وارد کردن نام خانوادگی الزامی میباشد").min(3, "نام خانوادگی نمیتواند کمتر از 3 کاراکتر باشد").max(25, "نام خانوادگی نمیتواند بیشتر از 25 کاراکتر باشد"), - cartNumber: yup.string() + PAN: yup.string() .required("وارد کردن شماره کارت الزامی میباشد") .min(16, "شماره کارت نمیتوند کمتر از 16 رقم باشد") .max(16, " شماره کارت نمیتواند بیشتر از 16 رقم باشد") .matches(/^[0-9]{16}$/, "شماره کارت نامعتبر"), - shabaNumber: yup.string() + IBAN: yup.string() .required("وارد کردن شماره شبا الزامی میباشد") - .min(24, "شماره شبا نمیتوند کمتر از 24 رقم باشد") - .max(24, " شماره شبا نمیتواند بیشتر از 24 رقم باشد") - .matches(/^[0-9]{24}$/, "شماره شبا نامعتبر"), + .min(26, "شماره شبا نمیتوند کمتر از 26 رقم باشد") + .max(26, " شماره شبا نمیتواند بیشتر از 26 رقم باشد") + .matches(/^IR[0-9]{24}$/, "شماره شبا نامعتبر"), }) \ No newline at end of file diff --git a/src/services/api/auth.ts b/src/services/api/auth.ts index 415e24c..624460d 100644 --- a/src/services/api/auth.ts +++ b/src/services/api/auth.ts @@ -1,3 +1,3 @@ -import { axiosInstance } from "../axios"; +import axiosInstance from "../axios"; export const loginMutation = (payload: any) => axiosInstance.post("/auth/gps/login", payload) \ No newline at end of file diff --git a/src/services/api/bank-card.ts b/src/services/api/bank-card.ts index 1688843..62a91d2 100644 --- a/src/services/api/bank-card.ts +++ b/src/services/api/bank-card.ts @@ -1,4 +1,15 @@ -import { LoginFormInterface } from "../../types"; -import { axiosInstance } from "../axios"; +import axiosInstance from "../axios"; -export const getUserCardsList = () => axiosInstance.get("/gps/bank-card") \ No newline at end of file +type CreateNewCard = { + holderName: string, + PAN: string, + IBAN: string +} + +type DeleteCard = { + id: string | number | null +} + +export const createNewCardMutation = (payload: CreateNewCard) => axiosInstance.post("/gps/bank-card", payload) +export const deleteCardMutation = (payload: DeleteCard) => axiosInstance.delete(`/gps/bank-card/:${payload?.id}`) +export const getUserCardsList = () => axiosInstance.get("/gps/bank-card") \ No newline at end of file diff --git a/src/services/api/reports.ts b/src/services/api/reports.ts deleted file mode 100644 index 73c328b..0000000 --- a/src/services/api/reports.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { installationReportsInterface } from "../../types"; -import { axiosInstance } from "../axios"; - -export const getReportsList = () => axiosInstance.get("/reports") \ No newline at end of file diff --git a/src/services/api/score.ts b/src/services/api/score.ts deleted file mode 100644 index dbbc39f..0000000 --- a/src/services/api/score.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { ScoreInterface } from "../../types"; -import { axiosInstance } from "../axios"; - -export const getScoreList = () => axiosInstance.get("/score") \ No newline at end of file diff --git a/src/services/api/subscription.ts b/src/services/api/subscription.ts deleted file mode 100644 index 0c4d9bd..0000000 --- a/src/services/api/subscription.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { SubscriptionReportInterface } from "../../types"; -import { axiosInstance } from "../axios"; - -export const getSubscriptionsList = () => axiosInstance.get("/subscriptions") \ No newline at end of file diff --git a/src/services/api/transactions.ts b/src/services/api/transactions.ts deleted file mode 100644 index f093beb..0000000 --- a/src/services/api/transactions.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { TransactionsInterface } from "../../types"; -import { axiosInstance } from "../axios"; - -export const getTransactionsList = () => axiosInstance.get("/transactions") \ No newline at end of file diff --git a/src/services/axios.ts b/src/services/axios.ts deleted file mode 100644 index 0164a22..0000000 --- a/src/services/axios.ts +++ /dev/null @@ -1,10 +0,0 @@ -import axios, { AxiosRequestConfig } from "axios"; - -const axiosParam: AxiosRequestConfig = { - baseURL: "https://asan-service.com/api/", - headers: { - Authorization: `Bearer ${window.localStorage.getItem("token") && window.localStorage.getItem("token")}`, - } -} - -export const axiosInstance = axios.create(axiosParam) \ No newline at end of file diff --git a/src/services/axios/index.ts b/src/services/axios/index.ts new file mode 100644 index 0000000..90e7413 --- /dev/null +++ b/src/services/axios/index.ts @@ -0,0 +1,16 @@ +import axios from "axios"; + +const axiosInstance = axios.create({ + baseURL: process.env.REACT_APP_API_Address, +}); + +axiosInstance.interceptors.request.use(async function (config) { + const search = window.location.search; + const params = new URLSearchParams(search) + const token = localStorage.getItem("token") ? localStorage.getItem("token") : params.get('token') + + config.headers.Authorization = 'Bearer ' + token + return config; +}); + +export default axiosInstance; \ No newline at end of file diff --git a/src/store/user-token.ts b/src/store/user-token.ts new file mode 100644 index 0000000..00df048 --- /dev/null +++ b/src/store/user-token.ts @@ -0,0 +1,14 @@ +import { create } from "zustand"; + +type UserType = { + token: string | null +} + +const useUserStore = create((set) => ({ + token: '', + setToken: (token: string | null) => { + set({ token: token }); + }, +})); + +export default useUserStore; \ No newline at end of file diff --git a/src/types/index.ts b/src/types/index.ts index 88b34c9..a88cfba 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -101,8 +101,8 @@ export interface BanksInterface { export interface AddNewCartInterface { name: string family: string - cartNumber: string - shabaNumber: string + PAN: string + IBAN: string } export interface installationReportsInterface {