tsk-6: Send Message (#22)
Closes #6 Reviewed-on: phoenix/textsender#22 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
import { API_BASE_URL } from '../constants/api';
|
||||
import {
|
||||
DATA_KEY_ACCESS_TOKEN,
|
||||
DATA_KEY_ADDED_CONTACT_ID,
|
||||
DATA_KEY_USER_ID,
|
||||
} from '../constants/app';
|
||||
import { contactApi, CreateContactRequest } from '../api/contactApi';
|
||||
import { tokenService } from '../services/TokenService';
|
||||
import { DATA_KEY_ADDED_CONTACT_ID } from '../constants/app';
|
||||
|
||||
import './AddContact.css';
|
||||
|
||||
@@ -66,33 +63,16 @@ const AddContact = ({ isOpen, onClose, onSuccess }) => {
|
||||
setSubmitError('');
|
||||
|
||||
try {
|
||||
const userId = localStorage.getItem(DATA_KEY_USER_ID);
|
||||
const accessToken = localStorage.getItem(DATA_KEY_ACCESS_TOKEN);
|
||||
const reqBod = {
|
||||
phone_number: formData.phoneNumber,
|
||||
first_name: formData.firstName,
|
||||
last_name: formData.lastName,
|
||||
user_id: userId,
|
||||
};
|
||||
// Send the API request directly from the Contact component
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/contact/new`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
body: JSON.stringify(reqBod),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(
|
||||
errorData.message ||
|
||||
`Failed to create contact. Status: ${response.status}`
|
||||
);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
const userId = tokenService.getUserId();
|
||||
let contact = new CreateContactRequest();
|
||||
contact.phone_number = formData.phoneNumber;
|
||||
contact.first_name = formData.firstName;
|
||||
contact.last_name = formData.lastName;
|
||||
contact.user_id = userId;
|
||||
const result = await contactApi.createContact(
|
||||
contact,
|
||||
tokenService.bearerToken()
|
||||
);
|
||||
if (result.data.length > 0) {
|
||||
console.log('Contact added');
|
||||
const contactId = result.data[0].id;
|
||||
|
||||
Reference in New Issue
Block a user