@@ -16,7 +16,7 @@ import { useFocusEffect } from '@react-navigation/native';
1616import { COLORS , SPACING , FONT_SIZE , BORDER_RADIUS , SHADOWS } from '../theme/tokens' ;
1717import { useAuth } from '../context/AuthContext' ;
1818import { PLATFORMS } from '@devcard/shared' ;
19- import { API_BASE_URL } from '../config ' ;
19+ import { get , post , del , put } from '../services/api ' ;
2020import { EmptyState } from '../components/EmptyState' ;
2121import { Skeleton } from '../components/Skeleton' ;
2222
@@ -46,19 +46,12 @@ export default function CardsScreen() {
4646 const fetchData = useCallback ( async ( showLoading = true ) => {
4747 if ( showLoading ) setLoading ( true ) ;
4848 try {
49- const [ cardsRes , profileRes ] = await Promise . all ( [
50- fetch ( `${ API_BASE_URL } /api/cards` , {
51- headers : { Authorization : `Bearer ${ token } ` } ,
52- } ) ,
53- fetch ( `${ API_BASE_URL } /api/profiles/me` , {
54- headers : { Authorization : `Bearer ${ token } ` } ,
55- } ) ,
49+ const [ cardsData , profileData ] = await Promise . all ( [
50+ get < Card [ ] > ( '/api/cards' , token ) . catch ( ( ) => [ ] ) ,
51+ get < any > ( '/api/profiles/me' , token ) . catch ( ( ) => null ) ,
5652 ] ) ;
57- if ( cardsRes . ok ) setCards ( await cardsRes . json ( ) ) ;
58- if ( profileRes . ok ) {
59- const data = await profileRes . json ( ) ;
60- setAllLinks ( data . platformLinks || [ ] ) ;
61- }
53+ setCards ( cardsData || [ ] ) ;
54+ setAllLinks ( profileData ?. platformLinks || [ ] ) ;
6255 } catch ( error ) {
6356 console . error ( 'Failed to fetch:' , error ) ;
6457 } finally {
@@ -84,20 +77,11 @@ export default function CardsScreen() {
8477 return ;
8578 }
8679 try {
87- const res = await fetch ( `${ API_BASE_URL } /api/cards` , {
88- method : 'POST' ,
89- headers : {
90- 'Content-Type' : 'application/json' ,
91- Authorization : `Bearer ${ token } ` ,
92- } ,
93- body : JSON . stringify ( { title : newTitle . trim ( ) , linkIds : selectedLinkIds } ) ,
94- } ) ;
95- if ( res . ok ) {
96- setShowCreate ( false ) ;
97- setNewTitle ( '' ) ;
98- setSelectedLinkIds ( [ ] ) ;
99- fetchData ( ) ;
100- }
80+ await post ( '/api/cards' , { title : newTitle . trim ( ) , linkIds : selectedLinkIds } , token ) ;
81+ setShowCreate ( false ) ;
82+ setNewTitle ( '' ) ;
83+ setSelectedLinkIds ( [ ] ) ;
84+ fetchData ( ) ;
10185 } catch {
10286 Alert . alert ( 'Error' , 'Failed to create card' ) ;
10387 }
@@ -110,21 +94,23 @@ export default function CardsScreen() {
11094 text : 'Delete' ,
11195 style : 'destructive' ,
11296 onPress : async ( ) => {
113- await fetch ( `${ API_BASE_URL } /api/cards/${ id } ` , {
114- method : 'DELETE' ,
115- headers : { Authorization : `Bearer ${ token } ` } ,
116- } ) ;
97+ try {
98+ await del ( `/api/cards/${ id } ` , undefined , token ) ;
99+ } catch {
100+ // ignore
101+ }
117102 fetchData ( ) ;
118103 } ,
119104 } ,
120105 ] ) ;
121106 } ;
122107
123108 const setDefault = async ( id : string ) => {
124- await fetch ( `${ API_BASE_URL } /api/cards/${ id } /default` , {
125- method : 'PUT' ,
126- headers : { Authorization : `Bearer ${ token } ` } ,
127- } ) ;
109+ try {
110+ await put ( `/api/cards/${ id } /default` , undefined , token ) ;
111+ } catch {
112+ // ignore
113+ }
128114 fetchData ( ) ;
129115 } ;
130116
0 commit comments