123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- import { toast } from 'react-toastify';
- const axios = require('axios');
- axios.defaults.baseURL = 'http://localhost:3000/api';
- const error = (message:string) =>
- toast.error(`🔥 ${message}!`, {
- position: 'bottom-left',
- autoClose: 3000,
- hideProgressBar: false,
- closeOnClick: true,
- pauseOnHover: true,
- draggable: true,
- progress: undefined,
- });
- const success = (message:string) =>
- toast.success(`⚡ ${message}`, {
- position: 'bottom-left',
- autoClose: 3000,
- 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 authorizeUser = async (number:string,country:string):Promise<string | undefined> => {
- try {
- const { data : {data} } = await axios.post('/auth/register', { number,country });
- success(`check ${number}`);
- success(`code ${data}`);
- return data
- } catch (e) {
- return undefined
- }
- };
- const loginUser = async <T>(number:string,code:string):Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.post('/auth/login', { number,code });
- success(`AUTHORIZED`);
- return data
- } catch (e) {
- return undefined
- }
- };
- const logoutUser = async <T>():Promise<T | undefined> => {
- try {
- const { data } = await axios.post('/auth/logout');
- success(`LOGOUT`);
- return data
- } catch (e) {
- return undefined
- }
- };
- const onlineUser = async <T>():Promise<T | undefined> => {
- try {
- const { data } = await axios.post('/auth/online');
- success(`ONLINE`);
- return data
- } catch (e) {
- return undefined
- }
- };
- const updateCredentials = async <T>(name: string,lastName: string):Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.patch('/users/current', { name, lastName });
- success(`CREDENTIALS UPDATED`);
- return data
- } catch (e) {
- return undefined
- }
- };
- const updateUserAvatar = async <T>(formData: object): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.patch('/users/avatars', formData);
- success(`AVATAR UPDATED`);
- return data
- } catch (e) {
- return undefined
- }
- };
- const currentUser = async <T>(): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.get('/users/current');
- success(`LOGIN`);
- return data
- } catch (e) {
- return undefined
- }
- };
- const addContact = async <T>(number:string): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.post('/contacts', { number });
- success('CONTACT ADDED');
- return data
- } catch (e) {
- error('ALREADY ADDED OR NOT EXIST')
- return undefined
- }
- };
- const getContacts = async <T>(): Promise<T | undefined> => {
- try {
- const { data : {data} } = await axios.get('/contacts');
- return data
- } catch (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) {
- return undefined
- }
- };
- const getChats = async <T>(): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.get('/chats');
- return data
- } catch (e) {
- return undefined
- }
- };
- const sentMessageById = async <T>(id:string,message:any): Promise<T | undefined> => {
- try {
- const { data: { data } } = await axios.post('/messages', { id, message });
- return data
- } catch (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) {
- return undefined
- }
- };
- export {
- setToken,
- error,
- success,
- authorizeUser,
- loginUser,
- logoutUser,
- onlineUser,
- updateCredentials,
- updateUserAvatar,
- currentUser,
- addContact,
- getContacts,
- startChat,
- getChats,
- sentMessageById,
- getMessagesById
- };
|