123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- import { toast } from 'react-toastify';
- import { prodBaseURL } from '../helpers';
- const axios = require('axios');
- axios.defaults.baseURL = `${prodBaseURL}/api`;
- const error = (message:string,closeTime: false| number) =>
- toast.error(`🔥 ${message}!`, {
- position: 'bottom-left',
- autoClose: closeTime,
- hideProgressBar: false,
- closeOnClick: true,
- pauseOnHover: true,
- draggable: true,
- progress: undefined,
- });
- const success = (message:string,closeTime: false| number) =>
- toast.success(`⚡ ${message}`, {
- position: 'bottom-left',
- autoClose: closeTime,
- hideProgressBar: false,
- closeOnClick: true,
- pauseOnHover: true,
- draggable: true,
- progress: undefined,
- });
- const setToken = {
- set(token:string) {
- axios.defaults.headers.common.Authorization = `Bearer ${token}`;
- },
- unset() {
- axios.defaults.headers.common.Authorization = '';
- },
- };
- const forbidden = ({ message }: any) => {
- if (message.slice(-3) === '403') {
- localStorage.removeItem('persist:auth')
- window.location.reload(true);
- }
- }
- const authorizeUser = async (number:string,country:string):Promise<string | undefined> => {
- try {
- const { data : {data} } = await axios.post('/auth/register', { number,country });
- success(`Use this code ${data} to sign in to the app "w-telegram". The real code cannot be sent via SMS to your phone because the Twilio account is not paid.`,20000);
- return data
- } catch (e) {
- return undefined
- }
- };
- const loginUser = async <T>(number:string,code:string):Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.patch('/auth/login', { number,code });
- return data
- } catch (e) {
- return undefined
- }
- };
- const logoutUser = async <T>():Promise<T | undefined> => {
- try {
- const { data } = await axios.patch('/auth/logout');
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const onlineUser = async <T>():Promise<T | undefined> => {
- try {
- const { data } = await axios.patch('/auth/online');
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const updateCredentials = async <T>(body:object):Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.patch('/users/current/credentials', body);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const updateUser = async <T>(body:object):Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.patch('/users/current/update', body);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const removeUserAvatar = async <T>(index:number):Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.delete(`/users/current/${index}`);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const updateUserAvatar = async <T>(formData: object): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.patch('/users/avatars', formData);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const currentUser = async <T>(): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.get('/users/current');
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const addContact = async <T>(number:string): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.post('/contacts', { number });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const removeContact = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.delete(`/contacts/${id}`);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const updateContact = async <T>(id:string,_id:string,name:string,lastName:string,companionId:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/contacts', { id, _id, name, lastName,companionId });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const pinContact = async <T>(id:string,pinned:boolean): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/contacts/pin', { id, pinned });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const getContacts = async <T>(): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.get('/contacts');
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const getChatById = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.get(`/chats/${id}`);
- return data._doc
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const startChat = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post('/chats', { id });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const removeChatForBoth = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.delete(`/chats/both/${id}`);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const muteChat = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/chats/mute', {id});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sortChat = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/chats/sort', {id});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const seenChat = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/chats/seen', { id });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const typingChat = async <T>(id:string,typing:boolean): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/chats/typing', { id,typing});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const pinChat = async <T>(id:string,pinned:boolean): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/chats/pin', { id,pinned});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const getChats = async <T>(): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.get('/chats');
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const removeMessageById = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.delete(`/messages/${id}`);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const removeSelectedMessagesById = async <T>(companionId:string,selectedArr:string[]): Promise<T | undefined> => {
- try {
- const { data } = await axios.patch('/messages/selected',{companionId,selectedArr});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const updateMessageById = async <T>(id:string,emoji:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch(`/messages/${id}`,{emoji});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const pinMessageById = async <T>(id:string,pinned:boolean): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch(`/messages/pin/${id}`,{pinned});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const unpinAllMessagesById = async <T>(pinnedMessages:string[]): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.patch('/messages/unpin/all/',{pinnedMessages});
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentMessageById = async <T>(id:string,message:string,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post('/messages', { id, message,caption });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentMessageEditById = async <T>(id:string,message:string,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post('/messages/edit', { id, message,caption });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentMessageReplyById = async <T>(id:string,message:string,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post('/messages/reply', { id, message,caption });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentMessageForwardById = async <T>(id:string,companionIdForwardToAndFrom:string,message:string,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post('/messages/forward', { id,companionIdForwardToAndFrom,message,caption });
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentImgMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post(`/messages/image/${id} ${caption}}`, formData);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentAudioMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post(`/messages/audio/${id} ${caption}`, formData);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentVideoMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post(`/messages/video/${id} ${caption}`, formData);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const sentFileMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post(`/messages/file/${id} ${caption}`, formData);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const getMessagesById = async <T>(id:string): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.get(`/messages/${id}`);
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- const getAllMessages = async <T>(): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.get('/messages');
- return data
- } catch (e) {
- forbidden(e)
- return undefined
- }
- };
- export {
- setToken,
- error,
- success,
- authorizeUser,
- loginUser,
- logoutUser,
- onlineUser,
- updateCredentials,
- updateUser,
- removeUserAvatar,
- updateUserAvatar,
- currentUser,
- addContact,
- removeContact,
- updateContact,
- pinContact,
- getContacts,
- startChat,
- removeChatForBoth,
- getChatById,
- muteChat,
- sortChat,
- seenChat,
- typingChat,
- pinChat,
- getChats,
- removeMessageById,
- removeSelectedMessagesById,
- updateMessageById,
- pinMessageById,
- unpinAllMessagesById,
- sentMessageById,
- sentMessageEditById,
- sentMessageReplyById,
- sentMessageForwardById,
- sentImgMessageById,
- sentAudioMessageById,
- sentVideoMessageById,
- sentFileMessageById,
- getMessagesById,
- getAllMessages,
- };
|