2023-02-27 01:54:28 +01:00
|
|
|
import { createContext } from 'react';
|
2022-04-30 00:09:53 +02:00
|
|
|
import { ChatMessage } from '../interfaces/chat-message.model';
|
2022-05-03 02:45:22 +02:00
|
|
|
import { getUnauthedData } from '../utils/apis';
|
2022-07-21 05:42:23 +02:00
|
|
|
|
2022-05-09 08:28:54 +02:00
|
|
|
const ENDPOINT = `/api/chat`;
|
|
|
|
const URL_CHAT_REGISTRATION = `/api/chat/register`;
|
2022-04-26 23:04:35 +02:00
|
|
|
|
2023-02-27 01:54:28 +01:00
|
|
|
export interface UserRegistrationResponse {
|
2022-04-26 23:04:35 +02:00
|
|
|
id: string;
|
|
|
|
accessToken: string;
|
|
|
|
displayName: string;
|
2022-08-10 04:56:45 +02:00
|
|
|
displayColor: number;
|
2022-04-26 23:04:35 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 01:54:28 +01:00
|
|
|
export interface ChatStaticService {
|
|
|
|
getChatHistory(accessToken: string): Promise<ChatMessage[]>;
|
|
|
|
registerUser(username: string): Promise<UserRegistrationResponse>;
|
|
|
|
}
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
class ChatService {
|
|
|
|
public static async getChatHistory(accessToken: string): Promise<ChatMessage[]> {
|
2022-05-03 02:45:22 +02:00
|
|
|
const response = await getUnauthedData(`${ENDPOINT}?accessToken=${accessToken}`);
|
|
|
|
return response;
|
2022-04-30 00:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static async registerUser(username: string): Promise<UserRegistrationResponse> {
|
2022-04-26 23:04:35 +02:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ displayName: username }),
|
|
|
|
};
|
2022-04-28 08:19:20 +02:00
|
|
|
|
2022-05-03 07:13:36 +02:00
|
|
|
const response = await getUnauthedData(URL_CHAT_REGISTRATION, options);
|
|
|
|
return response;
|
2022-04-26 23:04:35 +02:00
|
|
|
}
|
2022-04-28 08:19:20 +02:00
|
|
|
}
|
2022-04-30 00:09:53 +02:00
|
|
|
|
2023-02-27 01:54:28 +01:00
|
|
|
export const ChatServiceContext = createContext<ChatStaticService>(ChatService);
|