connect monitoring endpoints
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
'use server'
|
||||
|
||||
import { getSessionData } from '@/app/actions/session';
|
||||
import { API_BASE_URL } from '@/app/actions/definitions';
|
||||
|
||||
export async function setMonitoring(fileId: string, monitoringType: string) {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 30007,
|
||||
message_body: {
|
||||
monitoring_type: monitoringType,
|
||||
file_id: fileId,
|
||||
auth_token: token
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
if (parsed.message_code === 0) {
|
||||
return {
|
||||
success: true,
|
||||
errorMessage: null
|
||||
};
|
||||
} else {
|
||||
throw parsed;
|
||||
}
|
||||
} else {
|
||||
throw (`${response.status}`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
errorMessage: 'error'
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,29 +2,29 @@
|
||||
|
||||
import { FileItem } from '@/app/components/tanstak-table/TanstakTable';
|
||||
import { useEffect, useState, useRef } from 'react';
|
||||
import { useClickOutside } from '@/app/hooks/useClickOutside';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { setMonitoring } from '@/app/actions/monitoringActions';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
export function MonitoringDropDown({ file }: {
|
||||
file: FileItem
|
||||
}) {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [currentMonitoringStatus, setCurrentMonitoringStatus] = useState('without-monitoring');
|
||||
const [currentMonitoringStatus, setCurrentMonitoringStatus] = useState(file.monitoring);
|
||||
const [dropdownPosition, setDropdownPosition] = useState({ top: 0, left: 0, width: 0 });
|
||||
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
const buttonRef = useRef<HTMLButtonElement>(null);
|
||||
|
||||
const t = useTranslations('Global')
|
||||
const t = useTranslations('Monitoring-status');
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen) return;
|
||||
|
||||
const handleClick = (event: MouseEvent) => {
|
||||
console.log('click');
|
||||
if (!dropdownRef?.current) {
|
||||
return;
|
||||
}
|
||||
@@ -49,10 +49,10 @@ export function MonitoringDropDown({ file }: {
|
||||
}, [dropdownRef, setIsOpen, isOpen]);
|
||||
|
||||
useEffect(() => {
|
||||
if (file.status) {
|
||||
setCurrentMonitoringStatus(file.status === 'ACTIVE' ? 'without-monitoring' : 'without-monitoring');
|
||||
if (file.monitoring) {
|
||||
setCurrentMonitoringStatus(file.monitoring ? file.monitoring : 'NONE');
|
||||
}
|
||||
}, [file]);
|
||||
}, [file.monitoring]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen && buttonRef.current) {
|
||||
@@ -66,10 +66,16 @@ export function MonitoringDropDown({ file }: {
|
||||
}, [isOpen]);
|
||||
|
||||
async function clickHandler(monitoring: string) {
|
||||
console.log('click', file.id);
|
||||
setCurrentMonitoringStatus(monitoring);
|
||||
await queryClient.invalidateQueries({ queryKey: ['userFilesData'] });
|
||||
setIsOpen(false);
|
||||
|
||||
const response = await setMonitoring(file.id, monitoring);
|
||||
|
||||
if (response.success) {
|
||||
await queryClient.invalidateQueries({ queryKey: ['userFilesData'] });
|
||||
setIsOpen(false);
|
||||
toast.success(t('the-file-is monitored', { fileName: file.fileName }));
|
||||
} else {
|
||||
toast.warning(t('failed-to-establish-monitoring'));
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -113,36 +119,36 @@ export function MonitoringDropDown({ file }: {
|
||||
>
|
||||
<ul className="dropdown-list" role="listbox">
|
||||
<li
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'without-monitoring' ? 'current' : ''}`}
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'NONE' ? 'current' : ''}`}
|
||||
onClick={() => {
|
||||
clickHandler('without-monitoring')
|
||||
clickHandler('NONE')
|
||||
}}
|
||||
>
|
||||
{t('without-monitoring')}
|
||||
{t('NONE')}
|
||||
</li>
|
||||
<li
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'every-day' ? 'current' : ''}`}
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'MONITORING_DAILY' ? 'current' : ''}`}
|
||||
onClick={() => {
|
||||
clickHandler('every-day')
|
||||
clickHandler('MONITORING_DAILY')
|
||||
}}
|
||||
>
|
||||
{t('every-day')}
|
||||
{t('MONITORING_DAILY')}
|
||||
</li>
|
||||
<li
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'every-week' ? 'current' : ''}`}
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'MONITORING_WEEKLY' ? 'current' : ''}`}
|
||||
onClick={() => {
|
||||
clickHandler('every-week')
|
||||
clickHandler('MONITORING_WEEKLY')
|
||||
}}
|
||||
>
|
||||
{t('every-week')}
|
||||
{t('MONITORING_WEEKLY')}
|
||||
</li>
|
||||
<li
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'every-month' ? 'current' : ''}`}
|
||||
className={`dropdown-item ${currentMonitoringStatus === 'MONITORING_MONTHLY' ? 'current' : ''}`}
|
||||
onClick={() => {
|
||||
clickHandler('every-month')
|
||||
clickHandler('MONITORING_MONTHLY')
|
||||
}}
|
||||
>
|
||||
{t('every-month')}
|
||||
{t('MONITORING_MONTHLY')}
|
||||
</li>
|
||||
</ul>
|
||||
</div>,
|
||||
|
||||
@@ -38,6 +38,7 @@ export type FileItem = {
|
||||
size?: number | undefined;
|
||||
uploadDate?: number;
|
||||
status?: string;
|
||||
monitoring: string;
|
||||
protectStatus: string;
|
||||
_original?: ApiFile;
|
||||
supportId: number;
|
||||
@@ -51,6 +52,7 @@ type ApiFile = {
|
||||
fileSize: number;
|
||||
updatedAt: string;
|
||||
status: string;
|
||||
monitoring: string;
|
||||
protectStatus: string;
|
||||
supportId: number;
|
||||
fileName: string;
|
||||
@@ -90,6 +92,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
||||
size: item.fileSize,
|
||||
uploadDate: newDate,
|
||||
status: item.status,
|
||||
monitoring: item.monitoring,
|
||||
protectStatus: item.protectStatus,
|
||||
supportId: item.supportId,
|
||||
_original: item
|
||||
@@ -370,7 +373,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
||||
enableColumnFilter: false,
|
||||
},
|
||||
{
|
||||
accessorKey: 'status',
|
||||
accessorKey: 'monitoring',
|
||||
header: ({ column }) => (
|
||||
<div className="column">
|
||||
<span>
|
||||
|
||||
@@ -232,10 +232,6 @@
|
||||
"do-not-convert": "Do not convert",
|
||||
"payment-error": "Payment error",
|
||||
"buy": "Buy",
|
||||
"without-monitoring": "Without monitoring",
|
||||
"every-day": "Every day",
|
||||
"every-week": "Every week",
|
||||
"every-month": "Every day",
|
||||
"amount": "Amount",
|
||||
"date": "Date",
|
||||
"operation-type": "Operation type",
|
||||
@@ -386,9 +382,17 @@
|
||||
"fullName-required": "Please fill in the field correctly.",
|
||||
"fullName-too-short": "Name must be at least 2 characters long.",
|
||||
"company-already-registered": "The company is already registered.",
|
||||
"confirming" : "Confirming",
|
||||
"confirming": "Confirming",
|
||||
"server-error": "Server error",
|
||||
"network-error": "Network error",
|
||||
"token-not-found-or-time-expired": "Token not found or time expired"
|
||||
},
|
||||
"Monitoring-status": {
|
||||
"MONITORING_DAILY": "Every day",
|
||||
"MONITORING_WEEKLY": "Every week",
|
||||
"MONITORING_MONTHLY": "Every day",
|
||||
"NONE": "Without monitoring",
|
||||
"the-file-is monitored": "Monitoring is set for the \"{fileName}\" file",
|
||||
"failed-to-establish-monitoring": "Failed to establish monitoring"
|
||||
}
|
||||
}
|
||||
@@ -232,10 +232,6 @@
|
||||
"do-not-convert": "Не конвертировать",
|
||||
"payment-error": "Ошибка оплаты",
|
||||
"buy": "Купить",
|
||||
"without-monitoring": "Без мониторинга",
|
||||
"every-day": "Каждый день",
|
||||
"every-week": "Каждую неделю",
|
||||
"every-month": "Каждый месяц",
|
||||
"amount": "Сумма",
|
||||
"date": "Дата",
|
||||
"operation-type": "Тип операции",
|
||||
@@ -390,5 +386,13 @@
|
||||
"server-error": "Ошибка сервера",
|
||||
"network-error": "Сетевая ошибка",
|
||||
"token-not-found-or-time-expired": "Токен не найден или истек срок его действия."
|
||||
},
|
||||
"Monitoring-status": {
|
||||
"MONITORING_DAILY": "Каждый день",
|
||||
"MONITORING_WEEKLY": "Каждую неделю",
|
||||
"MONITORING_MONTHLY": "Каждый месяц",
|
||||
"NONE": "Без мониторинга",
|
||||
"the-file-is monitored": "Для файла \"{fileName}\" установлен мониторинг",
|
||||
"failed-to-establish-monitoring": "Не удалось установить мониторинг"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user