api.js 935 B

12345678910111213141516171819202122232425262728293031
  1. import AsyncStorage from '@react-native-async-storage/async-storage';
  2. export const fetchInvoices = async (setInvoices) => {
  3. try {
  4. const token = await AsyncStorage.getItem('token');
  5. const response = await fetch('http://localhost:3000/invoices', {
  6. headers: { 'Authorization': token },
  7. });
  8. if (response.ok) {
  9. const data = await response.json();
  10. setInvoices(data);
  11. } else {
  12. throw new Error('Failed to fetch invoices');
  13. }
  14. } catch (error) {
  15. Alert.alert('Error', 'Failed to fetch invoices');
  16. }
  17. };
  18. export const markAsPaid = async (invoiceId, setInvoices) => {
  19. try {
  20. const token = await AsyncStorage.getItem('token');
  21. await fetch(`http://localhost:3000/invoice/${invoiceId}/pay`, {
  22. method: 'POST',
  23. headers: { 'Authorization': token },
  24. });
  25. fetchInvoices(setInvoices);
  26. } catch (error) {
  27. Alert.alert('Error', 'Failed to mark invoice as paid');
  28. }
  29. };