add fetch users data, add loagout endpoints
This commit is contained in:
+16
-3
@@ -112,16 +112,29 @@ export async function authorization(
|
||||
|
||||
export async function logout() {
|
||||
const token = await getSessionData('token');
|
||||
console.log('logout');
|
||||
|
||||
try {
|
||||
await fetch(`${API_BASE_URL}/v1/api/auth/logout`, {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 100,
|
||||
token: token,
|
||||
message_body: {
|
||||
action: "logout"
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
console.log(parsed);
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
'use server'
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
import { createSession, deleteSession, getSessionData, updateSession } from '@/app/actions/session';
|
||||
import { loginFormSchema, API_BASE_URL } from '@/app/actions/definitions';
|
||||
|
||||
export async function fetchUsesData(page: number, size: number) {
|
||||
const token = await getSessionData('token');
|
||||
console.log('fetchUsesData');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/get-users`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 30014,
|
||||
message_body: {
|
||||
/* token: token, */
|
||||
action: 'getAllWithPagination',
|
||||
page: page,
|
||||
size: size
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
console.log(response);
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
|
||||
if (parsed.message_code === 0) {
|
||||
return parsed.message_body;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new Error(`${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { fetchUsesData } from '@/app/actions/usersActions';
|
||||
|
||||
interface ApiResponse {
|
||||
files: ApiUser[];
|
||||
}
|
||||
|
||||
interface ApiUser {
|
||||
id: string;
|
||||
mimeType: string;
|
||||
userEmail: number;
|
||||
userCompany: number;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export const useUsersData = (page: number, size: number) => {
|
||||
return useQuery<ApiResponse, Error, any>({
|
||||
queryKey: ['usersData'],
|
||||
queryFn: () => {
|
||||
return fetchUsesData(page, size)
|
||||
},
|
||||
|
||||
select: (data: ApiResponse): any => {
|
||||
if (!data?.files) return [
|
||||
{
|
||||
id: 1,
|
||||
userId: 1,
|
||||
userName: 'userName',
|
||||
userEmail: 'userEmail',
|
||||
userCompany: 'userCompany',
|
||||
userSubscription: 'userSubscription',
|
||||
userRole: 'role',
|
||||
userContent: 0,
|
||||
}
|
||||
];
|
||||
|
||||
return data.files.map((item: ApiUser) => {
|
||||
const [datePart, timePart] = item.updatedAt.split(' ');
|
||||
const [day, month, year] = datePart.split('-').map(Number);
|
||||
const [hours, minutes, seconds] = timePart.split(':').map(Number);
|
||||
const newDate = new Date(year, month - 1, day, hours, minutes, seconds).getTime();
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
userName: item.mimeType.toLocaleLowerCase(),
|
||||
userEmail: item.userEmail,
|
||||
userCompany: item.userCompany,
|
||||
userSubscription: newDate,
|
||||
userRole: 'role',
|
||||
userContent: 0,
|
||||
_original: item
|
||||
};
|
||||
});
|
||||
},
|
||||
retry: false,
|
||||
placeholderData: (previousData) => previousData // для оптимистичных обновлений
|
||||
});
|
||||
};
|
||||
@@ -18,6 +18,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import ModalWindow from '@/app/components/modalWindow';
|
||||
import { toast } from 'sonner';
|
||||
import { pluralize } from '@/app/lib/pluralize';
|
||||
import { useUsersData } from '@/app/hooks/react-query/useUsersData';
|
||||
|
||||
type FileItem = {
|
||||
id: string;
|
||||
@@ -103,51 +104,12 @@ const cutFileName = (userId: string) => {
|
||||
}
|
||||
|
||||
export default function TanstakUsersTable() {
|
||||
const {
|
||||
data: tableData,
|
||||
|
||||
const { data: tableData,
|
||||
isLoading,
|
||||
isError,
|
||||
error,
|
||||
} = useQuery<ApiResponse, Error, any>({
|
||||
queryKey: ['userFilesData'],
|
||||
queryFn: () => {
|
||||
return []
|
||||
},
|
||||
|
||||
select: (data: ApiResponse): any => {
|
||||
if (!data?.files) return [
|
||||
{
|
||||
id: 1,
|
||||
userId: 1,
|
||||
userName: 'userName',
|
||||
userEmail: 'userEmail',
|
||||
userCompany: 'userCompany',
|
||||
userSubscription: 'userSubscription',
|
||||
userRole: 'role',
|
||||
userContent: 0,
|
||||
}
|
||||
];
|
||||
|
||||
return data.files.map((item: ApiFile) => {
|
||||
const [datePart, timePart] = item.updatedAt.split(' ');
|
||||
const [day, month, year] = datePart.split('-').map(Number);
|
||||
const [hours, minutes, seconds] = timePart.split(':').map(Number);
|
||||
const newDate = new Date(year, month - 1, day, hours, minutes, seconds).getTime();
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
userId: item.originalFileName,
|
||||
userName: item.mimeType.toLocaleLowerCase(),
|
||||
userEmail: item.userEmail,
|
||||
userCompany: item.userCompany,
|
||||
userSubscription: newDate,
|
||||
userRole: 'role',
|
||||
userContent: 0,
|
||||
_original: item
|
||||
};
|
||||
});
|
||||
},
|
||||
});
|
||||
error
|
||||
} = useUsersData(10, 10);
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user