Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Axios, { AxiosInstance, AxiosResponse } from 'axios';
- import { AsyncStorage } from 'react-native';
- import { store } from '../components/router/router';
- import { ActionTypes } from '../entities/enums/action-types';
- import { CreateActivitiyPrivateDto } from '../entities/interfaces/create-activitiy-private.dto';
- import { CreateActivityRequestDto } from '../entities/interfaces/create-activity-request.dto';
- import { CreateStoryRequestDto } from '../entities/interfaces/create-story-request.dto';
- import { EditVolunteerRequest } from '../entities/interfaces/edit-volunteer-request';
- import { EventRequestDto } from '../entities/interfaces/event-request.dto';
- import { EventsForFarmerDto } from '../entities/interfaces/events-for-farmer.dto';
- import { FarmerEditRequest } from '../entities/interfaces/farmer-edit-request';
- import { LoginResponseDto } from '../entities/interfaces/login-response.dto';
- import { ProfileRequestDto } from '../entities/interfaces/profile-request.dto';
- import { RegisterVolunteerRequestDto } from '../entities/interfaces/register-volunteer-request.dto';
- import { SearchActivityDto } from '../entities/interfaces/search-activity.dto';
- import { VolunteerEditRequest } from '../entities/interfaces/volunteer-edit-request';
- import { removeToken } from '../state-managment/actions/token-actions';
- import { Log, LOG_TYPES } from '../utils/log';
- const baseUrl = __DEV__ ? 'http://sundo.compie.tech/api' : 'https://services.sundoapp.com/api';
- const axiosInstance: AxiosInstance = Axios.create({
- baseURL: baseUrl,
- timeout: 10000,
- headers: {
- 'Content-Type': 'application/json',
- // authorization: 'JSON token',
- },
- });
- axiosInstance.interceptors.request.use(async (config) => {
- try {
- config.headers['Authorization'] = 'Bearer ' + (await AsyncStorage.getItem('@Sundo:appToken'));
- return config;
- } catch (err) {
- console.log('interceptor error', err);
- throw err;
- }
- });
- axiosInstance.interceptors.response.use(
- (response) => {
- // Do something with response data
- // TODO if response status is 401 dispatch action that will delete the token key from state, asyncstorage & moves user to login page
- // console.log('---------');
- // console.log(response);
- return response;
- },
- (error) => {
- if (error.response === 408 || error.code === 'ECONNABORTED') {
- store.dispatch({ type: ActionTypes.REQUEST_TIMED_OUT });
- } else if (error.response.data.data === 'TOKEN_NOT_PROVIDED') {
- console.log('token removed');
- store.dispatch(removeToken());
- }
- // Do something with response error
- return Promise.reject(error);
- }
- );
- export const sendPhoneNumberRequest = async (phoneNumber: string): Promise<AxiosResponse> => {
- try {
- console.log('sendPhoneNumberRequest 1', phoneNumber);
- const response = await axiosInstance.post('/login/code', { phone_number: phoneNumber });
- console.log('sendPhoneNumberRequest', response);
- return response;
- } catch (e) {
- console.log('sendPhoneNumberRequest 2');
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const sendPinNumberRequest = async (
- pinNumber: number,
- phoneNumber: string
- ): Promise<AxiosResponse<LoginResponseDto>> => {
- return await axiosInstance.post('/login/enter', { code: pinNumber, phone_number: phoneNumber });
- };
- export const deleteActivityRequest = async (id) => {
- try {
- const response: AxiosResponse = await axiosInstance.delete(`/activity-requests/${id}`);
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- throw e;
- }
- };
- export const deleteFarmerActivityById = async (id) => {
- try {
- return await axiosInstance.delete(`/activities/${id}`);
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- throw e;
- }
- };
- export const getFarmerById = async (farmerId): Promise<AxiosResponse<ProfileRequestDto>> => {
- try {
- const response: AxiosResponse = await axiosInstance.get(`/user/farmer/${farmerId}`);
- return response;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- throw e;
- }
- };
- export const getVolunteerProfile = async (id): Promise<AxiosResponse<ProfileRequestDto>> => {
- try {
- const res: AxiosResponse = await axiosInstance.get(`/user/volunteer/${id}`);
- return res;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- throw e;
- }
- };
- export const getStoryById = async (storyId: number): Promise<AxiosResponse> => {
- return await axiosInstance.get(`/story/${storyId}`);
- };
- export const getAllNotifications = async () => {
- try {
- return await axiosInstance.get('/notifications');
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR);
- }
- };
- export const markNotificationsAsRead = async (untill_date: string) => {
- try {
- return await axiosInstance.post('/notifications/mark-as-read', untill_date);
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR);
- }
- };
- export const deleteNotification = async (notificationId) => {
- try {
- return await axiosInstance.delete(`/notifications/${notificationId}`);
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR);
- }
- };
- export const performEventSearch = async (
- country_id,
- area_id,
- start_date?,
- end_date?,
- difficulty_id?
- ): Promise<AxiosResponse<SearchActivityDto>> => {
- try {
- return await axiosInstance.post('/activities/search', {
- country_id,
- area_id,
- start_date,
- end_date,
- difficulty_id,
- });
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- console.log(e.response.data);
- }
- };
- export const perfrormActivityRequestsSearch = async (
- country_id,
- area_id,
- start_date?,
- end_date?,
- difficulty_id?
- ): Promise<AxiosResponse<SearchActivityDto>> => {
- try {
- return await axiosInstance.post('/activity-requests/search', {
- country_id,
- area_id,
- start_date,
- end_date,
- difficulty_id,
- });
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- console.log(e.response.data);
- }
- };
- export const createStory = async (story: CreateStoryRequestDto): Promise<AxiosResponse<any>> => {
- try {
- const response = await axiosInstance.post('/story/create', story);
- return response;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- console.log(e.response);
- }
- };
- export const deleteStoryById = async (storyId: number): Promise<AxiosResponse> => {
- try {
- const response = await axiosInstance.delete(`/story/${storyId}`);
- return response;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const getEventsByFarmerId = async (farmerId): Promise<AxiosResponse<EventsForFarmerDto>> => {
- try {
- const response: AxiosResponse = await axiosInstance.get(`/activities/my`);
- return response;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- console.log(e.response.data);
- }
- };
- export const createActivity = async (activity: CreateActivityRequestDto): Promise<AxiosResponse<any>> => {
- try {
- const response = await axiosInstance.post('/activity-requests/create', activity);
- return response;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const createFarmerActivity = async (activity: CreateActivitiyPrivateDto): Promise<AxiosResponse<any>> => {
- try {
- const response = await axiosInstance.post('/activities/create/', activity);
- return response;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- console.log(e.response.data);
- }
- };
- export const updateFarmerProfile = async (farmerProfile: FarmerEditRequest) => {
- return await axiosInstance.post('/user/farmer/edit', farmerProfile);
- };
- export const updateVolunteerProfile = async (volunteerDetails: EditVolunteerRequest) => {
- return await axiosInstance.post('/user/volunteer/edit', volunteerDetails);
- };
- export const getFarmerProfileEdit = async (): Promise<AxiosResponse<FarmerEditRequest>> => {
- try {
- return await axiosInstance.get('/user/farmer/edit');
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const getVolunteerProfileEdit = async (): Promise<AxiosResponse<VolunteerEditRequest>> => {
- try {
- const res = await axiosInstance.get('/user/volunteer/edit');
- console.log(res);
- return res;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const registerVolunteerRequest = async (
- registrationState: RegisterVolunteerRequestDto
- ): Promise<AxiosResponse<any>> => {
- try {
- console.log('registerVolunteerRequest:', registrationState);
- const res = await axiosInstance.post('/register/volunteer', registrationState);
- return res;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- console.log(e.response.data);
- throw e;
- }
- };
- export const registerFarmerRequest = async (fullNameInput: string, emailInput: string) => {
- try {
- const res = await axiosInstance.post('/register/farmer', { email: emailInput, full_name: fullNameInput });
- return res;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e, e.response);
- throw e;
- }
- };
- export const getCountriesList = async (): Promise<AxiosResponse> => {
- try {
- return await axiosInstance.get('/countries');
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const getFacilityList = async () => {
- try {
- return await axiosInstance.get('/facilities');
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const getEventById = async (eventId: number): Promise<AxiosResponse<EventRequestDto>> => {
- return await axiosInstance.get(`/activity-requests/${eventId}`);
- };
- export const joinActivitybyId = async (id) => {
- try {
- return await axiosInstance.get(`/activities/join/${id}`);
- } catch (e) {
- throw e;
- }
- };
- export const getFarmerEventById = async (eventId: number): Promise<AxiosResponse<EventRequestDto>> => {
- return await axiosInstance.get(`/activities/${eventId}`);
- };
- export const getSkillsList = async (): Promise<AxiosResponse> => {
- try {
- return await axiosInstance.get('/skills');
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const getDifficultiesList = async (): Promise<AxiosResponse> => {
- try {
- return await axiosInstance.get('/difficulties');
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- }
- };
- export const sendNewVolunteerActivity = async (activity: CreateActivityRequestDto) => {
- try {
- const res = await axiosInstance.post('/activity-requests/create/', activity);
- return res;
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e, e.response);
- throw e;
- }
- };
- export const getFeedRequest = async () => {
- try {
- const response = await axiosInstance.get('/feed');
- return response;
- } catch (error) {
- Log(LOG_TYPES.REQUEST_ERROR, error);
- }
- };
- export const checkIfUserIsActive = async () => {
- try {
- return await axiosInstance.get('');
- } catch (e) {
- Log(LOG_TYPES.REQUEST_ERROR, e);
- throw e;
- }
- };
- export const getActivitiesRelevantToActivity = async (acitivityId: number) => {
- try {
- return await axiosInstance.get(`/activity-requests/${acitivityId}/relevant-activities`);
- } catch (error) {
- Log(LOG_TYPES.REQUEST_ERROR, error);
- }
- };
- export const inviteToActivity = async (activityId: number, volunteerId: number) => {
- try {
- return await axiosInstance.get(`/activities/invite/${activityId}/${volunteerId}`);
- } catch (error) {
- Log(LOG_TYPES.REQUEST_ERROR, error);
- }
- };
- export const deleteNotificationById = async (notificationId: number) => {
- return await axiosInstance.delete(`/notifications/${notificationId}`);
- };
- export const getUserStatus = async () => {
- try {
- return await axiosInstance.get(`/user/get-user-status`);
- } catch (error) {
- Log(LOG_TYPES.REQUEST_ERROR, error);
- }
- };
- export const acceptInvitation = async (activityId: number) => {
- return await axiosInstance.get(`/activities/join/${activityId}`);
- };
- export const declineInvitation = async (activityId: number) => {
- return await axiosInstance.get(`/activities/invite/decline/${activityId}`);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement