2022-04-26 08:10:07 +02:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { atom, useRecoilState } from 'recoil';
|
2022-04-28 08:19:20 +02:00
|
|
|
import { makeEmptyClientConfig, ClientConfig } from '../../interfaces/client-config.model';
|
|
|
|
import ClientConfigService from '../../services/client-config-service';
|
2022-04-30 00:09:53 +02:00
|
|
|
import ChatService from '../../services/chat-service';
|
2022-04-28 23:36:05 +02:00
|
|
|
import { ChatMessage } from '../../interfaces/chat-message.model';
|
2022-04-30 00:09:53 +02:00
|
|
|
import { getLocalStorage, setLocalStorage } from '../../utils/helpers';
|
|
|
|
import { ChatVisibilityState } from '../../interfaces/application-state';
|
2022-04-26 08:10:07 +02:00
|
|
|
|
2022-04-28 08:19:20 +02:00
|
|
|
// The config that comes from the API.
|
2022-04-26 08:10:07 +02:00
|
|
|
export const clientConfigState = atom({
|
|
|
|
key: 'clientConfigState',
|
|
|
|
default: makeEmptyClientConfig(),
|
|
|
|
});
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
export const chatVisibility = atom<ChatVisibilityState>({
|
|
|
|
key: 'chatVisibility',
|
|
|
|
default: ChatVisibilityState.Hidden,
|
2022-04-28 08:19:20 +02:00
|
|
|
});
|
|
|
|
|
2022-04-28 23:36:05 +02:00
|
|
|
export const chatDisplayName = atom({
|
2022-04-28 08:19:20 +02:00
|
|
|
key: 'chatDisplayName',
|
2022-04-30 00:09:53 +02:00
|
|
|
default: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const accessTokenAtom = atom({
|
|
|
|
key: 'accessToken',
|
|
|
|
default: null,
|
2022-04-28 08:19:20 +02:00
|
|
|
});
|
|
|
|
|
2022-04-28 23:36:05 +02:00
|
|
|
export const chatMessages = atom({
|
|
|
|
key: 'chatMessages',
|
|
|
|
default: [] as ChatMessage[],
|
|
|
|
});
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
export function ClientConfigStore() {
|
2022-04-26 08:10:07 +02:00
|
|
|
const [, setClientConfig] = useRecoilState<ClientConfig>(clientConfigState);
|
2022-04-30 00:09:53 +02:00
|
|
|
const [, setChatMessages] = useRecoilState<ChatMessage[]>(chatMessages);
|
|
|
|
const [accessToken, setAccessToken] = useRecoilState<string>(accessTokenAtom);
|
|
|
|
const [, setChatDisplayName] = useRecoilState<string>(chatDisplayName);
|
2022-04-26 08:10:07 +02:00
|
|
|
|
|
|
|
const updateClientConfig = async () => {
|
|
|
|
try {
|
|
|
|
const config = await ClientConfigService.getConfig();
|
|
|
|
console.log(`ClientConfig: ${JSON.stringify(config)}`);
|
|
|
|
setClientConfig(config);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`ClientConfigService -> getConfig() ERROR: \n${error}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
const handleUserRegistration = async (optionalDisplayName: string) => {
|
|
|
|
try {
|
|
|
|
const response = await ChatService.registerUser(optionalDisplayName);
|
|
|
|
console.log(`ChatService -> registerUser() response: \n${JSON.stringify(response)}`);
|
|
|
|
const { accessToken: newAccessToken, displayName } = response;
|
|
|
|
if (!newAccessToken) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setAccessToken(accessToken);
|
|
|
|
setLocalStorage('accessToken', newAccessToken);
|
|
|
|
setChatDisplayName(displayName);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`ChatService -> registerUser() ERROR: \n${e}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Requires access token.
|
|
|
|
const getChatHistory = async () => {
|
|
|
|
try {
|
|
|
|
const messages = await ChatService.getChatHistory(accessToken);
|
|
|
|
console.log(`ChatService -> getChatHistory() messages: \n${JSON.stringify(messages)}`);
|
|
|
|
setChatMessages(messages);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`ChatService -> getChatHistory() ERROR: \n${error}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-26 08:10:07 +02:00
|
|
|
useEffect(() => {
|
|
|
|
updateClientConfig();
|
2022-04-30 00:09:53 +02:00
|
|
|
handleUserRegistration();
|
2022-04-26 08:10:07 +02:00
|
|
|
}, []);
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
useEffect(() => {
|
|
|
|
getChatHistory();
|
|
|
|
}, [accessToken]);
|
|
|
|
|
2022-04-26 08:10:07 +02:00
|
|
|
return null;
|
|
|
|
}
|