payment bug fixed
This commit is contained in:
@@ -1,104 +1,171 @@
|
||||
'use client';
|
||||
import React from 'react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { HistoryTableRowProps } from '../lib/types';
|
||||
import { formatDate } from '../lib/utils';
|
||||
import { ResultBadge } from './resultBadge';
|
||||
import { useRouter } from '@/shared/config/i18n/navigation';
|
||||
import { useUserPlagiatStore } from '@/shared/zustand/user';
|
||||
import PaymentStatus from '@/widgets/detail/paidStatus';
|
||||
import { PaymentModal } from '@/widgets/paymentModal/ui/Paymentmodal';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { apiRequest } from '@/shared/request/apiRequest';
|
||||
import { links } from '@/shared/request/links';
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
export const HistoryTableRow: React.FC<HistoryTableRowProps> = ({ item }) => {
|
||||
const router = useRouter();
|
||||
const t = useTranslations('HistoryPage');
|
||||
const tPay = useTranslations('Payment');
|
||||
const tUnknown = useTranslations();
|
||||
const user = useUserPlagiatStore((state) => state.user);
|
||||
const [isPaymentOpen, setIsPaymentOpen] = useState(false);
|
||||
const [localUser, setLocalUser] = useState<{
|
||||
id: number;
|
||||
name: string;
|
||||
surname: string;
|
||||
} | null>(null);
|
||||
|
||||
const userName = user
|
||||
? `${user.name} ${user.surname}`
|
||||
useEffect(() => {
|
||||
const data = localStorage.getItem('user');
|
||||
|
||||
if (data) {
|
||||
setLocalUser(JSON.parse(data));
|
||||
} else {
|
||||
setLocalUser(null);
|
||||
}
|
||||
}, [user]);
|
||||
|
||||
const userName = localUser
|
||||
? `${localUser.name} ${localUser.surname}`
|
||||
: tUnknown('unknownUser');
|
||||
|
||||
const payment = useMutation({
|
||||
mutationKey: ['payload'],
|
||||
mutationFn: ({ order_id }: { order_id: number }) =>
|
||||
apiRequest<{ payment_link: string }>('POST', links.payment(order_id)),
|
||||
onSuccess: (res) => {
|
||||
console.log('payment res: ', res);
|
||||
window.open(res.data.payment_link, '_self');
|
||||
//route.push(`/${document_id}`);
|
||||
setIsPaymentOpen(false);
|
||||
},
|
||||
onError: (err) => {
|
||||
const message =
|
||||
err instanceof Error ? err.message : 'An unexpected error occurred.';
|
||||
toast.error(message);
|
||||
setIsPaymentOpen(false);
|
||||
},
|
||||
});
|
||||
|
||||
const handleSubmit = ({ document_id }: { document_id: number }) => {
|
||||
payment.mutate({ order_id: document_id });
|
||||
};
|
||||
|
||||
return (
|
||||
<tr className="border-b border-slate-100 hover:bg-slate-50/70 transition-colors duration-100 group">
|
||||
{/* Sender */}
|
||||
<td className="px-4 py-3.5">
|
||||
<span className="text-sm font-medium text-slate-800 whitespace-nowrap">
|
||||
{userName}
|
||||
</span>
|
||||
</td>
|
||||
|
||||
{/* File Name */}
|
||||
<td className="px-4 py-3.5">
|
||||
<a
|
||||
href={item.file}
|
||||
target="_blank"
|
||||
className="flex items-center gap-2 underline"
|
||||
>
|
||||
<svg
|
||||
width="14"
|
||||
height="14"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.75"
|
||||
className="text-slate-400 shrink-0"
|
||||
>
|
||||
<path d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
||||
</svg>
|
||||
<span className="text-sm text-slate-600 font-mono" title={item.file}>
|
||||
{tUnknown('file')}
|
||||
<>
|
||||
<tr className="border-b border-slate-100 hover:bg-slate-50/70 transition-colors duration-100 group">
|
||||
{/* Sender */}
|
||||
<td className="px-4 py-3.5">
|
||||
<span className="text-sm font-medium text-slate-800 whitespace-nowrap">
|
||||
{userName}
|
||||
</span>
|
||||
</a>
|
||||
</td>
|
||||
</td>
|
||||
|
||||
{/* Date */}
|
||||
<td className="px-4 py-3.5">
|
||||
<span className="text-sm text-slate-500 whitespace-nowrap">
|
||||
{formatDate(item.created_at)}
|
||||
</span>
|
||||
</td>
|
||||
|
||||
{/* Amount */}
|
||||
{/* <td className="px-4 py-3.5">
|
||||
<span className="text-sm font-medium text-slate-700 whitespace-nowrap tabular-nums">
|
||||
{item.} UZS
|
||||
</span>
|
||||
</td> */}
|
||||
|
||||
{/* Result */}
|
||||
<td className="px-4 py-3.5">
|
||||
<ResultBadge result={'clean'} />
|
||||
</td>
|
||||
|
||||
{/* View Button */}
|
||||
<td className="px-4 py-3.5 text-right">
|
||||
<button
|
||||
onClick={() => router.push(`/${item.id}`)}
|
||||
aria-label={t('viewDetails', { sender: item.title })}
|
||||
className="
|
||||
inline-flex items-center gap-1.5 px-3 py-1.5
|
||||
text-xs font-medium text-slate-600
|
||||
bg-white border border-slate-200 rounded-lg
|
||||
hover:border-slate-300 hover:text-slate-900 hover:bg-slate-50
|
||||
active:scale-95
|
||||
transition-all duration-150
|
||||
focus:outline-none focus-visible:ring-2 focus-visible:ring-slate-300
|
||||
"
|
||||
>
|
||||
{t('view')}
|
||||
<svg
|
||||
width="11"
|
||||
height="11"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
{/* File Name */}
|
||||
<td className="px-4 py-3.5">
|
||||
<a
|
||||
href={item.file}
|
||||
target="_blank"
|
||||
className="flex items-center gap-2 underline"
|
||||
>
|
||||
<path d="M5 12h14M12 5l7 7-7 7" />
|
||||
</svg>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
<svg
|
||||
width="14"
|
||||
height="14"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.75"
|
||||
className="text-slate-400 shrink-0"
|
||||
>
|
||||
<path d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
||||
</svg>
|
||||
<span
|
||||
className="text-sm text-slate-600 font-mono"
|
||||
title={item.file}
|
||||
>
|
||||
{tUnknown('file')}
|
||||
</span>
|
||||
</a>
|
||||
</td>
|
||||
|
||||
{/* Date */}
|
||||
<td className="px-4 py-3.5">
|
||||
<span className="text-sm text-slate-500 whitespace-nowrap">
|
||||
{formatDate(item.created_at)}
|
||||
</span>
|
||||
</td>
|
||||
|
||||
{/* State */}
|
||||
<td className="px-4 py-3.5">
|
||||
<span
|
||||
onClick={() => {
|
||||
if (item.state === 'unpaid') {
|
||||
setIsPaymentOpen(true);
|
||||
}
|
||||
}}
|
||||
className="text-sm font-medium text-slate-700 whitespace-nowrap tabular-nums"
|
||||
>
|
||||
<PaymentStatus status={item.state} />
|
||||
</span>
|
||||
</td>
|
||||
|
||||
{/* View Button */}
|
||||
<td className="px-4 py-3.5 text-right">
|
||||
<button
|
||||
onClick={() => {
|
||||
if (item.state === 'paid') {
|
||||
router.push(`/${item.id}`);
|
||||
} else {
|
||||
toast.error(tPay('paymentRequired'));
|
||||
}
|
||||
}}
|
||||
aria-label={t('viewDetails', { sender: item.title })}
|
||||
className="
|
||||
inline-flex items-center gap-1.5 px-3 py-1.5
|
||||
text-xs font-medium text-slate-600
|
||||
bg-white border border-slate-200 rounded-lg
|
||||
hover:border-slate-300 hover:text-slate-900 hover:bg-slate-50
|
||||
active:scale-95
|
||||
transition-all duration-150
|
||||
focus:outline-none focus-visible:ring-2 focus-visible:ring-slate-300
|
||||
"
|
||||
>
|
||||
{t('view')}
|
||||
<svg
|
||||
width="11"
|
||||
height="11"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<path d="M5 12h14M12 5l7 7-7 7" />
|
||||
</svg>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<PaymentModal
|
||||
isOpen={isPaymentOpen}
|
||||
onClose={() => setIsPaymentOpen(false)}
|
||||
hasCertificate={false}
|
||||
onConfirmPayment={() => {
|
||||
handleSubmit({ document_id: Number(item.order_id) });
|
||||
}}
|
||||
isLoading={payment.isPending}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user