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 { FileItem } from '@/app/components/tanstak-table/TanstakTable';
|
||||||
import { useEffect, useState, useRef } from 'react';
|
import { useEffect, useState, useRef } from 'react';
|
||||||
import { useClickOutside } from '@/app/hooks/useClickOutside';
|
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
import { createPortal } from 'react-dom';
|
import { createPortal } from 'react-dom';
|
||||||
|
import { setMonitoring } from '@/app/actions/monitoringActions';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
|
||||||
export function MonitoringDropDown({ file }: {
|
export function MonitoringDropDown({ file }: {
|
||||||
file: FileItem
|
file: FileItem
|
||||||
}) {
|
}) {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
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 [dropdownPosition, setDropdownPosition] = useState({ top: 0, left: 0, width: 0 });
|
||||||
|
|
||||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||||
const buttonRef = useRef<HTMLButtonElement>(null);
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
||||||
|
|
||||||
const t = useTranslations('Global')
|
const t = useTranslations('Monitoring-status');
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isOpen) return;
|
if (!isOpen) return;
|
||||||
|
|
||||||
const handleClick = (event: MouseEvent) => {
|
const handleClick = (event: MouseEvent) => {
|
||||||
console.log('click');
|
|
||||||
if (!dropdownRef?.current) {
|
if (!dropdownRef?.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -49,10 +49,10 @@ export function MonitoringDropDown({ file }: {
|
|||||||
}, [dropdownRef, setIsOpen, isOpen]);
|
}, [dropdownRef, setIsOpen, isOpen]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (file.status) {
|
if (file.monitoring) {
|
||||||
setCurrentMonitoringStatus(file.status === 'ACTIVE' ? 'without-monitoring' : 'without-monitoring');
|
setCurrentMonitoringStatus(file.monitoring ? file.monitoring : 'NONE');
|
||||||
}
|
}
|
||||||
}, [file]);
|
}, [file.monitoring]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isOpen && buttonRef.current) {
|
if (isOpen && buttonRef.current) {
|
||||||
@@ -66,10 +66,16 @@ export function MonitoringDropDown({ file }: {
|
|||||||
}, [isOpen]);
|
}, [isOpen]);
|
||||||
|
|
||||||
async function clickHandler(monitoring: string) {
|
async function clickHandler(monitoring: string) {
|
||||||
console.log('click', file.id);
|
|
||||||
setCurrentMonitoringStatus(monitoring);
|
const response = await setMonitoring(file.id, monitoring);
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
await queryClient.invalidateQueries({ queryKey: ['userFilesData'] });
|
await queryClient.invalidateQueries({ queryKey: ['userFilesData'] });
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
|
toast.success(t('the-file-is monitored', { fileName: file.fileName }));
|
||||||
|
} else {
|
||||||
|
toast.warning(t('failed-to-establish-monitoring'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -113,36 +119,36 @@ export function MonitoringDropDown({ file }: {
|
|||||||
>
|
>
|
||||||
<ul className="dropdown-list" role="listbox">
|
<ul className="dropdown-list" role="listbox">
|
||||||
<li
|
<li
|
||||||
className={`dropdown-item ${currentMonitoringStatus === 'without-monitoring' ? 'current' : ''}`}
|
className={`dropdown-item ${currentMonitoringStatus === 'NONE' ? 'current' : ''}`}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
clickHandler('without-monitoring')
|
clickHandler('NONE')
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{t('without-monitoring')}
|
{t('NONE')}
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li
|
||||||
className={`dropdown-item ${currentMonitoringStatus === 'every-day' ? 'current' : ''}`}
|
className={`dropdown-item ${currentMonitoringStatus === 'MONITORING_DAILY' ? 'current' : ''}`}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
clickHandler('every-day')
|
clickHandler('MONITORING_DAILY')
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{t('every-day')}
|
{t('MONITORING_DAILY')}
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li
|
||||||
className={`dropdown-item ${currentMonitoringStatus === 'every-week' ? 'current' : ''}`}
|
className={`dropdown-item ${currentMonitoringStatus === 'MONITORING_WEEKLY' ? 'current' : ''}`}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
clickHandler('every-week')
|
clickHandler('MONITORING_WEEKLY')
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{t('every-week')}
|
{t('MONITORING_WEEKLY')}
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li
|
||||||
className={`dropdown-item ${currentMonitoringStatus === 'every-month' ? 'current' : ''}`}
|
className={`dropdown-item ${currentMonitoringStatus === 'MONITORING_MONTHLY' ? 'current' : ''}`}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
clickHandler('every-month')
|
clickHandler('MONITORING_MONTHLY')
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{t('every-month')}
|
{t('MONITORING_MONTHLY')}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>,
|
</div>,
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export type FileItem = {
|
|||||||
size?: number | undefined;
|
size?: number | undefined;
|
||||||
uploadDate?: number;
|
uploadDate?: number;
|
||||||
status?: string;
|
status?: string;
|
||||||
|
monitoring: string;
|
||||||
protectStatus: string;
|
protectStatus: string;
|
||||||
_original?: ApiFile;
|
_original?: ApiFile;
|
||||||
supportId: number;
|
supportId: number;
|
||||||
@@ -51,6 +52,7 @@ type ApiFile = {
|
|||||||
fileSize: number;
|
fileSize: number;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
status: string;
|
status: string;
|
||||||
|
monitoring: string;
|
||||||
protectStatus: string;
|
protectStatus: string;
|
||||||
supportId: number;
|
supportId: number;
|
||||||
fileName: string;
|
fileName: string;
|
||||||
@@ -90,6 +92,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
size: item.fileSize,
|
size: item.fileSize,
|
||||||
uploadDate: newDate,
|
uploadDate: newDate,
|
||||||
status: item.status,
|
status: item.status,
|
||||||
|
monitoring: item.monitoring,
|
||||||
protectStatus: item.protectStatus,
|
protectStatus: item.protectStatus,
|
||||||
supportId: item.supportId,
|
supportId: item.supportId,
|
||||||
_original: item
|
_original: item
|
||||||
@@ -370,7 +373,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
enableColumnFilter: false,
|
enableColumnFilter: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorKey: 'status',
|
accessorKey: 'monitoring',
|
||||||
header: ({ column }) => (
|
header: ({ column }) => (
|
||||||
<div className="column">
|
<div className="column">
|
||||||
<span>
|
<span>
|
||||||
|
|||||||
@@ -232,10 +232,6 @@
|
|||||||
"do-not-convert": "Do not convert",
|
"do-not-convert": "Do not convert",
|
||||||
"payment-error": "Payment error",
|
"payment-error": "Payment error",
|
||||||
"buy": "Buy",
|
"buy": "Buy",
|
||||||
"without-monitoring": "Without monitoring",
|
|
||||||
"every-day": "Every day",
|
|
||||||
"every-week": "Every week",
|
|
||||||
"every-month": "Every day",
|
|
||||||
"amount": "Amount",
|
"amount": "Amount",
|
||||||
"date": "Date",
|
"date": "Date",
|
||||||
"operation-type": "Operation type",
|
"operation-type": "Operation type",
|
||||||
@@ -390,5 +386,13 @@
|
|||||||
"server-error": "Server error",
|
"server-error": "Server error",
|
||||||
"network-error": "Network error",
|
"network-error": "Network error",
|
||||||
"token-not-found-or-time-expired": "Token not found or time expired"
|
"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": "Не конвертировать",
|
"do-not-convert": "Не конвертировать",
|
||||||
"payment-error": "Ошибка оплаты",
|
"payment-error": "Ошибка оплаты",
|
||||||
"buy": "Купить",
|
"buy": "Купить",
|
||||||
"without-monitoring": "Без мониторинга",
|
|
||||||
"every-day": "Каждый день",
|
|
||||||
"every-week": "Каждую неделю",
|
|
||||||
"every-month": "Каждый месяц",
|
|
||||||
"amount": "Сумма",
|
"amount": "Сумма",
|
||||||
"date": "Дата",
|
"date": "Дата",
|
||||||
"operation-type": "Тип операции",
|
"operation-type": "Тип операции",
|
||||||
@@ -390,5 +386,13 @@
|
|||||||
"server-error": "Ошибка сервера",
|
"server-error": "Ошибка сервера",
|
||||||
"network-error": "Сетевая ошибка",
|
"network-error": "Сетевая ошибка",
|
||||||
"token-not-found-or-time-expired": "Токен не найден или истек срок его действия."
|
"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