SHARE
TWEET

Untitled

a guest Dec 13th, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Axios, { AxiosInstance, AxiosResponse } from 'axios';
  2. import { AsyncStorage } from 'react-native';
  3. import { store } from '../components/router/router';
  4. import { ActionTypes } from '../entities/enums/action-types';
  5. import { CreateActivitiyPrivateDto } from '../entities/interfaces/create-activitiy-private.dto';
  6. import { CreateActivityRequestDto } from '../entities/interfaces/create-activity-request.dto';
  7. import { CreateStoryRequestDto } from '../entities/interfaces/create-story-request.dto';
  8. import { EditVolunteerRequest } from '../entities/interfaces/edit-volunteer-request';
  9. import { EventRequestDto } from '../entities/interfaces/event-request.dto';
  10. import { EventsForFarmerDto } from '../entities/interfaces/events-for-farmer.dto';
  11. import { FarmerEditRequest } from '../entities/interfaces/farmer-edit-request';
  12. import { LoginResponseDto } from '../entities/interfaces/login-response.dto';
  13. import { ProfileRequestDto } from '../entities/interfaces/profile-request.dto';
  14. import { RegisterVolunteerRequestDto } from '../entities/interfaces/register-volunteer-request.dto';
  15. import { SearchActivityDto } from '../entities/interfaces/search-activity.dto';
  16. import { VolunteerEditRequest } from '../entities/interfaces/volunteer-edit-request';
  17. import { removeToken } from '../state-managment/actions/token-actions';
  18. import { Log, LOG_TYPES } from '../utils/log';
  19.  
  20. const baseUrl = __DEV__ ? 'http://sundo.compie.tech/api' : 'https://services.sundoapp.com/api';
  21.  
  22. const axiosInstance: AxiosInstance = Axios.create({
  23.     baseURL: baseUrl,
  24.     timeout: 10000,
  25.     headers: {
  26.         'Content-Type': 'application/json',
  27.         // authorization: 'JSON token',
  28.     },
  29. });
  30.  
  31. axiosInstance.interceptors.request.use(async (config) => {
  32.     try {
  33.         config.headers['Authorization'] = 'Bearer ' + (await AsyncStorage.getItem('@Sundo:appToken'));
  34.         return config;
  35.     } catch (err) {
  36.         console.log('interceptor error', err);
  37.         throw err;
  38.     }
  39. });
  40.  
  41. axiosInstance.interceptors.response.use(
  42.     (response) => {
  43.         // Do something with response data
  44.         // TODO if response status is 401 dispatch action that will delete the token key from state, asyncstorage & moves user to login page
  45.         // console.log('---------');
  46.         // console.log(response);
  47.         return response;
  48.     },
  49.     (error) => {
  50.         if (error.response === 408 || error.code === 'ECONNABORTED') {
  51.             store.dispatch({ type: ActionTypes.REQUEST_TIMED_OUT });
  52.         } else if (error.response.data.data === 'TOKEN_NOT_PROVIDED') {
  53.             console.log('token removed');
  54.             store.dispatch(removeToken());
  55.         }
  56.         // Do something with response error
  57.         return Promise.reject(error);
  58.     }
  59. );
  60.  
  61. export const sendPhoneNumberRequest = async (phoneNumber: string): Promise<AxiosResponse> => {
  62.     try {
  63.         console.log('sendPhoneNumberRequest 1', phoneNumber);
  64.         const response = await axiosInstance.post('/login/code', { phone_number: phoneNumber });
  65.         console.log('sendPhoneNumberRequest', response);
  66.         return response;
  67.     } catch (e) {
  68.         console.log('sendPhoneNumberRequest 2');
  69.         Log(LOG_TYPES.REQUEST_ERROR, e);
  70.     }
  71. };
  72.  
  73. export const sendPinNumberRequest = async (
  74.     pinNumber: number,
  75.     phoneNumber: string
  76. ): Promise<AxiosResponse<LoginResponseDto>> => {
  77.     return await axiosInstance.post('/login/enter', { code: pinNumber, phone_number: phoneNumber });
  78. };
  79.  
  80. export const deleteActivityRequest = async (id) => {
  81.     try {
  82.         const response: AxiosResponse = await axiosInstance.delete(`/activity-requests/${id}`);
  83.     } catch (e) {
  84.         Log(LOG_TYPES.REQUEST_ERROR, e);
  85.         throw e;
  86.     }
  87. };
  88.  
  89. export const deleteFarmerActivityById = async (id) => {
  90.     try {
  91.         return await axiosInstance.delete(`/activities/${id}`);
  92.     } catch (e) {
  93.         Log(LOG_TYPES.REQUEST_ERROR, e);
  94.         throw e;
  95.     }
  96. };
  97.  
  98. export const getFarmerById = async (farmerId): Promise<AxiosResponse<ProfileRequestDto>> => {
  99.     try {
  100.         const response: AxiosResponse = await axiosInstance.get(`/user/farmer/${farmerId}`);
  101.         return response;
  102.     } catch (e) {
  103.         Log(LOG_TYPES.REQUEST_ERROR, e);
  104.         throw e;
  105.     }
  106. };
  107.  
  108. export const getVolunteerProfile = async (id): Promise<AxiosResponse<ProfileRequestDto>> => {
  109.     try {
  110.         const res: AxiosResponse = await axiosInstance.get(`/user/volunteer/${id}`);
  111.         return res;
  112.     } catch (e) {
  113.         Log(LOG_TYPES.REQUEST_ERROR, e);
  114.         throw e;
  115.     }
  116. };
  117.  
  118. export const getStoryById = async (storyId: number): Promise<AxiosResponse> => {
  119.     return await axiosInstance.get(`/story/${storyId}`);
  120. };
  121.  
  122. export const getAllNotifications = async () => {
  123.     try {
  124.         return await axiosInstance.get('/notifications');
  125.     } catch (e) {
  126.         Log(LOG_TYPES.REQUEST_ERROR);
  127.     }
  128. };
  129.  
  130. export const markNotificationsAsRead = async (untill_date: string) => {
  131.     try {
  132.         return await axiosInstance.post('/notifications/mark-as-read', untill_date);
  133.     } catch (e) {
  134.         Log(LOG_TYPES.REQUEST_ERROR);
  135.     }
  136. };
  137.  
  138. export const deleteNotification = async (notificationId) => {
  139.     try {
  140.         return await axiosInstance.delete(`/notifications/${notificationId}`);
  141.     } catch (e) {
  142.         Log(LOG_TYPES.REQUEST_ERROR);
  143.     }
  144. };
  145.  
  146. export const performEventSearch = async (
  147.     country_id,
  148.     area_id,
  149.     start_date?,
  150.     end_date?,
  151.     difficulty_id?
  152. ): Promise<AxiosResponse<SearchActivityDto>> => {
  153.     try {
  154.         return await axiosInstance.post('/activities/search', {
  155.             country_id,
  156.             area_id,
  157.             start_date,
  158.             end_date,
  159.             difficulty_id,
  160.         });
  161.     } catch (e) {
  162.         Log(LOG_TYPES.REQUEST_ERROR, e);
  163.         console.log(e.response.data);
  164.     }
  165. };
  166.  
  167. export const perfrormActivityRequestsSearch = async (
  168.     country_id,
  169.     area_id,
  170.     start_date?,
  171.     end_date?,
  172.     difficulty_id?
  173. ): Promise<AxiosResponse<SearchActivityDto>> => {
  174.     try {
  175.         return await axiosInstance.post('/activity-requests/search', {
  176.             country_id,
  177.             area_id,
  178.             start_date,
  179.             end_date,
  180.             difficulty_id,
  181.         });
  182.     } catch (e) {
  183.         Log(LOG_TYPES.REQUEST_ERROR, e);
  184.         console.log(e.response.data);
  185.     }
  186. };
  187.  
  188. export const createStory = async (story: CreateStoryRequestDto): Promise<AxiosResponse<any>> => {
  189.     try {
  190.         const response = await axiosInstance.post('/story/create', story);
  191.         return response;
  192.     } catch (e) {
  193.         Log(LOG_TYPES.REQUEST_ERROR, e);
  194.         console.log(e.response);
  195.     }
  196. };
  197.  
  198. export const deleteStoryById = async (storyId: number): Promise<AxiosResponse> => {
  199.     try {
  200.         const response = await axiosInstance.delete(`/story/${storyId}`);
  201.         return response;
  202.     } catch (e) {
  203.         Log(LOG_TYPES.REQUEST_ERROR, e);
  204.     }
  205. };
  206.  
  207. export const getEventsByFarmerId = async (farmerId): Promise<AxiosResponse<EventsForFarmerDto>> => {
  208.     try {
  209.         const response: AxiosResponse = await axiosInstance.get(`/activities/my`);
  210.         return response;
  211.     } catch (e) {
  212.         Log(LOG_TYPES.REQUEST_ERROR, e);
  213.         console.log(e.response.data);
  214.     }
  215. };
  216.  
  217. export const createActivity = async (activity: CreateActivityRequestDto): Promise<AxiosResponse<any>> => {
  218.     try {
  219.         const response = await axiosInstance.post('/activity-requests/create', activity);
  220.         return response;
  221.     } catch (e) {
  222.         Log(LOG_TYPES.REQUEST_ERROR, e);
  223.     }
  224. };
  225.  
  226. export const createFarmerActivity = async (activity: CreateActivitiyPrivateDto): Promise<AxiosResponse<any>> => {
  227.     try {
  228.         const response = await axiosInstance.post('/activities/create/', activity);
  229.         return response;
  230.     } catch (e) {
  231.         Log(LOG_TYPES.REQUEST_ERROR, e);
  232.         console.log(e.response.data);
  233.     }
  234. };
  235.  
  236. export const updateFarmerProfile = async (farmerProfile: FarmerEditRequest) => {
  237.     return await axiosInstance.post('/user/farmer/edit', farmerProfile);
  238. };
  239.  
  240. export const updateVolunteerProfile = async (volunteerDetails: EditVolunteerRequest) => {
  241.     return await axiosInstance.post('/user/volunteer/edit', volunteerDetails);
  242. };
  243.  
  244. export const getFarmerProfileEdit = async (): Promise<AxiosResponse<FarmerEditRequest>> => {
  245.     try {
  246.         return await axiosInstance.get('/user/farmer/edit');
  247.     } catch (e) {
  248.         Log(LOG_TYPES.REQUEST_ERROR, e);
  249.     }
  250. };
  251.  
  252. export const getVolunteerProfileEdit = async (): Promise<AxiosResponse<VolunteerEditRequest>> => {
  253.     try {
  254.         const res = await axiosInstance.get('/user/volunteer/edit');
  255.         console.log(res);
  256.         return res;
  257.     } catch (e) {
  258.         Log(LOG_TYPES.REQUEST_ERROR, e);
  259.     }
  260. };
  261.  
  262. export const registerVolunteerRequest = async (
  263.     registrationState: RegisterVolunteerRequestDto
  264. ): Promise<AxiosResponse<any>> => {
  265.     try {
  266.         console.log('registerVolunteerRequest:', registrationState);
  267.         const res = await axiosInstance.post('/register/volunteer', registrationState);
  268.         return res;
  269.     } catch (e) {
  270.         Log(LOG_TYPES.REQUEST_ERROR, e);
  271.         console.log(e.response.data);
  272.         throw e;
  273.     }
  274. };
  275.  
  276. export const registerFarmerRequest = async (fullNameInput: string, emailInput: string) => {
  277.     try {
  278.         const res = await axiosInstance.post('/register/farmer', { email: emailInput, full_name: fullNameInput });
  279.         return res;
  280.     } catch (e) {
  281.         Log(LOG_TYPES.REQUEST_ERROR, e, e.response);
  282.         throw e;
  283.     }
  284. };
  285.  
  286. export const getCountriesList = async (): Promise<AxiosResponse> => {
  287.     try {
  288.         return await axiosInstance.get('/countries');
  289.     } catch (e) {
  290.         Log(LOG_TYPES.REQUEST_ERROR, e);
  291.     }
  292. };
  293.  
  294. export const getFacilityList = async () => {
  295.     try {
  296.         return await axiosInstance.get('/facilities');
  297.     } catch (e) {
  298.         Log(LOG_TYPES.REQUEST_ERROR, e);
  299.     }
  300. };
  301.  
  302. export const getEventById = async (eventId: number): Promise<AxiosResponse<EventRequestDto>> => {
  303.     return await axiosInstance.get(`/activity-requests/${eventId}`);
  304. };
  305.  
  306. export const joinActivitybyId = async (id) => {
  307.     try {
  308.         return await axiosInstance.get(`/activities/join/${id}`);
  309.     } catch (e) {
  310.         throw e;
  311.     }
  312. };
  313.  
  314. export const getFarmerEventById = async (eventId: number): Promise<AxiosResponse<EventRequestDto>> => {
  315.     return await axiosInstance.get(`/activities/${eventId}`);
  316. };
  317.  
  318. export const getSkillsList = async (): Promise<AxiosResponse> => {
  319.     try {
  320.         return await axiosInstance.get('/skills');
  321.     } catch (e) {
  322.         Log(LOG_TYPES.REQUEST_ERROR, e);
  323.     }
  324. };
  325.  
  326. export const getDifficultiesList = async (): Promise<AxiosResponse> => {
  327.     try {
  328.         return await axiosInstance.get('/difficulties');
  329.     } catch (e) {
  330.         Log(LOG_TYPES.REQUEST_ERROR, e);
  331.     }
  332. };
  333.  
  334. export const sendNewVolunteerActivity = async (activity: CreateActivityRequestDto) => {
  335.     try {
  336.         const res = await axiosInstance.post('/activity-requests/create/', activity);
  337.         return res;
  338.     } catch (e) {
  339.         Log(LOG_TYPES.REQUEST_ERROR, e, e.response);
  340.         throw e;
  341.     }
  342. };
  343.  
  344. export const getFeedRequest = async () => {
  345.     try {
  346.         const response = await axiosInstance.get('/feed');
  347.         return response;
  348.     } catch (error) {
  349.         Log(LOG_TYPES.REQUEST_ERROR, error);
  350.     }
  351. };
  352.  
  353. export const checkIfUserIsActive = async () => {
  354.     try {
  355.         return await axiosInstance.get('');
  356.     } catch (e) {
  357.         Log(LOG_TYPES.REQUEST_ERROR, e);
  358.         throw e;
  359.     }
  360. };
  361.  
  362. export const getActivitiesRelevantToActivity = async (acitivityId: number) => {
  363.     try {
  364.         return await axiosInstance.get(`/activity-requests/${acitivityId}/relevant-activities`);
  365.     } catch (error) {
  366.         Log(LOG_TYPES.REQUEST_ERROR, error);
  367.     }
  368. };
  369.  
  370. export const inviteToActivity = async (activityId: number, volunteerId: number) => {
  371.     try {
  372.         return await axiosInstance.get(`/activities/invite/${activityId}/${volunteerId}`);
  373.     } catch (error) {
  374.         Log(LOG_TYPES.REQUEST_ERROR, error);
  375.     }
  376. };
  377.  
  378. export const deleteNotificationById = async (notificationId: number) => {
  379.         return await axiosInstance.delete(`/notifications/${notificationId}`);
  380. };
  381.  
  382. export const getUserStatus = async () => {
  383.     try {
  384.         return await axiosInstance.get(`/user/get-user-status`);
  385.     } catch (error) {
  386.         Log(LOG_TYPES.REQUEST_ERROR, error);
  387.     }
  388. };
  389.  
  390. export const acceptInvitation = async (activityId: number) => {
  391.     return await axiosInstance.get(`/activities/join/${activityId}`);
  392. };
  393.  
  394. export const declineInvitation = async (activityId: number) => {
  395.     return await axiosInstance.get(`/activities/invite/decline/${activityId}`);
  396. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top