Fix chat list pagination in frontend
This commit is contained in:
parent
1b7559a296
commit
3d99226296
@ -69,8 +69,7 @@ export default function EmployeesPage() {
|
||||
setCreatingChatFor(employee.id);
|
||||
try {
|
||||
// Ищем существующий приватный чат с этим пользователем
|
||||
const chatsResponse = await apiClient.getChats();
|
||||
const allChats = chatsResponse.results || chatsResponse;
|
||||
const allChats = await apiClient.getAllChats();
|
||||
|
||||
const existingChat = allChats.find((chat: any) => {
|
||||
if (chat.type !== 'private') return false;
|
||||
|
||||
@ -92,8 +92,7 @@ export default function ChatscapeMessenger() {
|
||||
const loadChats = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await apiClient.getChats();
|
||||
const chatsList = response.results || [];
|
||||
const chatsList = await apiClient.getAllChats();
|
||||
setChats(chatsList);
|
||||
|
||||
// Автоматически открываем первый чат
|
||||
@ -114,8 +113,7 @@ export default function ChatscapeMessenger() {
|
||||
const handleChatCreated = async (chatId: number) => {
|
||||
try {
|
||||
// Перезагружаем список чатов
|
||||
const response = await apiClient.getChats();
|
||||
const chatsList = response.results || [];
|
||||
const chatsList = await apiClient.getAllChats();
|
||||
setChats(chatsList);
|
||||
|
||||
// Открываем новый чат
|
||||
|
||||
@ -112,8 +112,7 @@ export default function MessagesPage() {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const response = await apiClient.getChats();
|
||||
const items = response.results || [];
|
||||
const items = await apiClient.getAllChats();
|
||||
|
||||
// FRONTEND-ONLY: добираем детали чатов, чтобы получить имя/аватар собеседника
|
||||
// для private/direct в случае, когда list не отдает avatar/participant_details.
|
||||
|
||||
@ -154,8 +154,8 @@ export function useChats() {
|
||||
useEffect(() => {
|
||||
async function fetchChats() {
|
||||
try {
|
||||
const data = await apiClient.getChats();
|
||||
setChats(data.results || data);
|
||||
const data = await apiClient.getAllChats();
|
||||
setChats(data);
|
||||
} catch (err) {
|
||||
setError(err as Error);
|
||||
} finally {
|
||||
|
||||
@ -19,23 +19,12 @@ import {
|
||||
} from "@/lib/users/userDetailUtils";
|
||||
import type { Chat, EmployeeAction, User } from "@/types/api";
|
||||
|
||||
type ChatListResponse = Chat[] | { results?: Chat[] };
|
||||
type ChatWithMemberIds = Chat & { member_ids?: number[] };
|
||||
|
||||
const getErrorMessage = (error: unknown, fallback: string): string => (
|
||||
String((error as Error)?.message || fallback)
|
||||
);
|
||||
|
||||
const getChatResults = (response: ChatListResponse): ChatWithMemberIds[] => {
|
||||
if (Array.isArray(response)) {
|
||||
return response as ChatWithMemberIds[];
|
||||
}
|
||||
|
||||
return Array.isArray(response.results)
|
||||
? (response.results as ChatWithMemberIds[])
|
||||
: [];
|
||||
};
|
||||
|
||||
export function useUserDetailPage(userId: number, currentUser: User | null) {
|
||||
const router = useRouter();
|
||||
|
||||
@ -107,8 +96,7 @@ export function useUserDetailPage(userId: number, currentUser: User | null) {
|
||||
try {
|
||||
setCreatingChat(true);
|
||||
|
||||
const chatsResponse = await apiClient.getChats() as ChatListResponse;
|
||||
const allChats = getChatResults(chatsResponse);
|
||||
const allChats = await apiClient.getAllChats() as ChatWithMemberIds[];
|
||||
const existingChat = allChats.find((chat) => {
|
||||
if (chat.type !== "private") return false;
|
||||
|
||||
|
||||
@ -20,13 +20,39 @@ export function createMessagesApi(request: RequestFn, getToken: GetTokenFn) {
|
||||
return result.message || result;
|
||||
}
|
||||
|
||||
function buildChatsQuery(params?: { search?: string; page?: number; page_size?: number }) {
|
||||
const qp = new URLSearchParams();
|
||||
if (params?.search) qp.append('search', params.search);
|
||||
if (params?.page) qp.append('page', params.page.toString());
|
||||
if (params?.page_size) qp.append('page_size', params.page_size.toString());
|
||||
const qs = qp.toString();
|
||||
return `/api/v1/communications/chats/${qs ? '?' + qs : ''}`;
|
||||
}
|
||||
|
||||
return {
|
||||
getChats: (params?: { search?: string; page?: number }) => {
|
||||
const qp = new URLSearchParams();
|
||||
if (params?.search) qp.append('search', params.search);
|
||||
if (params?.page) qp.append('page', params.page.toString());
|
||||
const qs = qp.toString();
|
||||
return request(`/api/v1/communications/chats/${qs ? '?' + qs : ''}`);
|
||||
getChats: (params?: { search?: string; page?: number; page_size?: number }) => request(buildChatsQuery(params)),
|
||||
getAllChats: async (params?: { search?: string; page_size?: number }) => {
|
||||
const pageSize = params?.page_size ?? 200;
|
||||
const chats: any[] = [];
|
||||
let page = 1;
|
||||
|
||||
for (;;) {
|
||||
const response = await request(buildChatsQuery({
|
||||
search: params?.search,
|
||||
page,
|
||||
page_size: pageSize,
|
||||
})) as { results?: any[]; next?: string | null } | any[];
|
||||
const results = Array.isArray(response) ? response : (response.results || []);
|
||||
chats.push(...results);
|
||||
|
||||
if (Array.isArray(response) || !response.next) {
|
||||
break;
|
||||
}
|
||||
|
||||
page += 1;
|
||||
}
|
||||
|
||||
return chats;
|
||||
},
|
||||
getChat: (chatId: number) => request(`/api/v1/communications/chats/${chatId}/`),
|
||||
createChat: (data: { type: string; participants?: number[]; name?: string; description?: string; department?: number; include_all_employees?: boolean; avatar?: File }) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user