Untitled

 avatar
unknown
plain_text
12 days ago
1.0 kB
1
Indexable
const handleAddClient = async () => {
  try {
    const token = await AsyncStorage.getItem('token');
    if (!token) {
      Alert.alert('Error', 'No authentication token found.');
      return;
    }

    console.log('API URL:', process.env.EXPO_PUBLIC_API_URL);

    const response = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/clients`, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(newClient),
    });

    const responseJson = await response.json();

    if (!response.ok) {
      console.error('Server Error:', responseJson);
      Alert.alert('Error', 'Please fill in all required fields.');
      return;
    }

    setClients([...clients, responseJson]);
    console.log('Client added:', responseJson);
  } catch (error) {
    console.error('Error during client addition:', error);
    Alert.alert('Error', 'Failed to add a client.');
  } finally {
    closeAddClientModal();
  }
};
Leave a Comment