1- import axios from 'axios' ;
2- import AsyncStorage from '@react-native-async-storage/async-storage' ;
3- import { FARM_API_URL } from '@env' ;
1+ import api from './api' ;
42
5- const fetchFarms = async showPopup => {
3+ export const fetchFarms = async ( showPopup ) => {
64 try {
7- const token = await AsyncStorage . getItem ( 'accessToken' ) ;
8- const response = await axios . get ( FARM_API_URL , {
9- headers : {
10- Authorization : `Bearer ${ token } ` ,
11- } ,
12- } ) ;
13- return response . data . data . docs ;
5+ const res = await api . get ( '/' ) ;
6+ return res . data . data . docs ;
147 } catch ( error ) {
15- console . error ( 'Failed to fetch farms:' , error ) ;
16- showPopup ( { success : false , msg : 'failed to fetch Farms' } ) ;
8+ showPopup ( { success : false , msg : 'Failed to fetch farms' } ) ;
179 return [ ] ;
1810 }
1911} ;
2012
21- const addFarm = async ( form , showPopup ) => {
13+ export const addFarm = async ( form , showPopup ) => {
2214 try {
23- const token = await AsyncStorage . getItem ( 'accessToken' ) ;
24- const response = await axios . post ( FARM_API_URL , form , {
25- headers : {
26- Authorization : `Bearer ${ token } ` ,
27- } ,
28- } ) ;
29- showPopup ( { success : true , msg : 'farm added successfully' } ) ;
30- return response . data . data ;
15+ const res = await api . post ( '/' , form ) ;
16+ showPopup ( { success : true , msg : 'Farm added successfully' } ) ;
17+ return res . data . data ;
3118 } catch ( error ) {
32- showPopup ( { success : false , msg : 'Failed to add Farm ' } ) ;
19+ showPopup ( { success : false , msg : 'Failed to add farm ' } ) ;
3320 throw error ;
3421 }
3522} ;
3623
37- const deleteFarm = async ( farmId , showPopup ) => {
24+ export const deleteFarm = async ( farmId , showPopup ) => {
3825 try {
39- const token = await AsyncStorage . getItem ( 'accessToken' ) ;
40- const response = await axios . delete ( `${ FARM_API_URL } /${ farmId } ` , {
41- headers : {
42- Authorization : `Bearer ${ token } ` ,
43- } ,
44- } ) ;
26+ const res = await api . delete ( `/${ farmId } ` ) ;
4527 showPopup ( { success : true , msg : 'Farm deleted' } ) ;
46- return response . data . data ;
28+ return res . data . data ;
4729 } catch ( err ) {
4830 showPopup ( { success : false , msg : 'Failed to delete' } ) ;
4931 throw err ;
5032 }
51- } ;
52-
53- export { fetchFarms , addFarm , deleteFarm } ;
33+ } ;
0 commit comments