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 => { 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 (number:string,code:string):Promise => { try { const { data : {data} } = await axios.patch('/auth/login', { number,code }); return data } catch (e) { return undefined } }; const logoutUser = async ():Promise => { try { const { data } = await axios.patch('/auth/logout'); return data } catch (e) { forbidden(e) return undefined } }; const onlineUser = async ():Promise => { try { const { data } = await axios.patch('/auth/online'); return data } catch (e) { forbidden(e) return undefined } }; const updateCredentials = async (body:object):Promise => { try { const { data : {data} } = await axios.patch('/users/current/credentials', body); return data } catch (e) { forbidden(e) return undefined } }; const updateUser = async (body:object):Promise => { try { const { data : {data} } = await axios.patch('/users/current/update', body); return data } catch (e) { forbidden(e) return undefined } }; const removeUserAvatar = async (index:number):Promise => { try { const { data: { data } } = await axios.delete(`/users/current/${index}`); return data } catch (e) { forbidden(e) return undefined } }; const updateUserAvatar = async (formData: object): Promise => { try { const { data : {data} } = await axios.patch('/users/avatars', formData); return data } catch (e) { forbidden(e) return undefined } }; const currentUser = async (): Promise => { try { const { data : {data} } = await axios.get('/users/current'); return data } catch (e) { forbidden(e) return undefined } }; const addContact = async (number:string): Promise => { try { const { data : {data} } = await axios.post('/contacts', { number }); return data } catch (e) { forbidden(e) return undefined } }; const removeContact = async (id:string): Promise => { try { const { data : {data} } = await axios.delete(`/contacts/${id}`); return data } catch (e) { forbidden(e) return undefined } }; const updateContact = async (id:string,_id:string,name:string,lastName:string,companionId:string): Promise => { try { const { data: { data } } = await axios.patch('/contacts', { id, _id, name, lastName,companionId }); return data } catch (e) { forbidden(e) return undefined } }; const pinContact = async (id:string,pinned:boolean): Promise => { try { const { data: { data } } = await axios.patch('/contacts/pin', { id, pinned }); return data } catch (e) { forbidden(e) return undefined } }; const getContacts = async (): Promise => { try { const { data : {data} } = await axios.get('/contacts'); return data } catch (e) { forbidden(e) return undefined } }; const getChatById = async (id:string): Promise => { try { const { data: { data } } = await axios.get(`/chats/${id}`); return data._doc } catch (e) { forbidden(e) return undefined } }; const startChat = async (id:string): Promise => { try { const { data: { data } } = await axios.post('/chats', { id }); return data } catch (e) { forbidden(e) return undefined } }; const removeChatForBoth = async (id:string): Promise => { try { const { data: { data } } = await axios.delete(`/chats/both/${id}`); return data } catch (e) { forbidden(e) return undefined } }; const muteChat = async (id:string): Promise => { try { const { data: { data } } = await axios.patch('/chats/mute', {id}); return data } catch (e) { forbidden(e) return undefined } }; const sortChat = async (id:string): Promise => { try { const { data: { data } } = await axios.patch('/chats/sort', {id}); return data } catch (e) { forbidden(e) return undefined } }; const socketIdChat = async (socketId:string): Promise => { try { const { data: { data } } = await axios.patch('/chats/socketId', {socketId}); return data } catch (e) { forbidden(e) return undefined } }; const seenChat = async (id:string): Promise => { try { const { data: { data } } = await axios.patch('/chats/seen', { id }); return data } catch (e) { forbidden(e) return undefined } }; const typingChat = async (id:string,typing:boolean): Promise => { try { const { data: { data } } = await axios.patch('/chats/typing', { id,typing}); return data } catch (e) { forbidden(e) return undefined } }; const pinChat = async (id:string,pinned:boolean): Promise => { try { const { data: { data } } = await axios.patch('/chats/pin', { id,pinned}); return data } catch (e) { forbidden(e) return undefined } }; const getChats = async (): Promise => { try { const { data: { data } } = await axios.get('/chats'); return data } catch (e) { forbidden(e) return undefined } }; const removeMessageById = async (id:string): Promise => { try { const { data: { data } } = await axios.delete(`/messages/${id}`); return data } catch (e) { forbidden(e) return undefined } }; const removeSelectedMessagesById = async (companionId:string,selectedArr:string[]): Promise => { try { const { data } = await axios.patch('/messages/selected',{companionId,selectedArr}); return data } catch (e) { forbidden(e) return undefined } }; const updateMessageById = async (id:string,emoji:string): Promise => { try { const { data: { data } } = await axios.patch(`/messages/${id}`,{emoji}); return data } catch (e) { forbidden(e) return undefined } }; const pinMessageById = async (id:string,pinned:boolean): Promise => { try { const { data: { data } } = await axios.patch(`/messages/pin/${id}`,{pinned}); return data } catch (e) { forbidden(e) return undefined } }; const unpinAllMessagesById = async (pinnedMessages:string[]): Promise => { try { const { data: { data } } = await axios.patch('/messages/unpin/all/',{pinnedMessages}); return data } catch (e) { forbidden(e) return undefined } }; const sentMessageById = async (id:string,message:string,caption:string): Promise => { try { const { data: { data } } = await axios.post('/messages', { id, message,caption }); return data } catch (e) { forbidden(e) return undefined } }; const sentMessageCallById = async (id:string,duration:number): Promise => { try { const { data: { data } } = await axios.post('/messages/call', { id,duration }); return data } catch (e) { forbidden(e) return undefined } }; const updateMessageCallById = async (id:string,duration:number): Promise => { try { const { data: { data } } = await axios.patch('/messages/call/end', { id,duration }); return data } catch (e) { forbidden(e) return undefined } }; const sentMessageEditById = async (id:string,message:string,caption:string): Promise => { try { const { data: { data } } = await axios.post('/messages/edit', { id, message,caption }); return data } catch (e) { forbidden(e) return undefined } }; const sentMessageReplyById = async (id:string,message:string,caption:string): Promise => { try { const { data: { data } } = await axios.post('/messages/reply', { id, message,caption }); return data } catch (e) { forbidden(e) return undefined } }; const sentMessageForwardById = async (id:string,companionIdForwardToAndFrom:string,message:string,caption:string): Promise => { try { const { data: { data } } = await axios.post('/messages/forward', { id,companionIdForwardToAndFrom,message,caption }); return data } catch (e) { forbidden(e) return undefined } }; const sentImgMessageById = async (id:string,formData: object,caption:string): Promise => { try { const { data: { data } } = await axios.post(`/messages/image/${id} ${caption}}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const sentAudioMessageById = async (id:string,formData: object,caption:string): Promise => { try { const { data: { data } } = await axios.post(`/messages/audio/${id} ${caption}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const sentVideoMessageById = async (id:string,formData: object,caption:string): Promise => { try { const { data: { data } } = await axios.post(`/messages/video/${id} ${caption}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const sentFileMessageById = async (id:string,formData: object,caption:string): Promise => { try { const { data: { data } } = await axios.post(`/messages/file/${id} ${caption}`, formData); return data } catch (e) { forbidden(e) return undefined } }; const getMessagesById = async (id:string): Promise => { try { const { data : {data} } = await axios.get(`/messages/${id}`); return data } catch (e) { forbidden(e) return undefined } }; const getAllMessages = async (): Promise => { 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, socketIdChat, seenChat, typingChat, pinChat, getChats, removeMessageById, removeSelectedMessagesById, updateMessageById, pinMessageById, unpinAllMessagesById, sentMessageById, sentMessageCallById, updateMessageCallById, sentMessageEditById, sentMessageReplyById, sentMessageForwardById, sentImgMessageById, sentAudioMessageById, sentVideoMessageById, sentFileMessageById, getMessagesById, getAllMessages, };