diff --git a/assets/src/api.ts b/assets/src/api.ts index 9255612f1..922d35bb8 100644 --- a/assets/src/api.ts +++ b/assets/src/api.ts @@ -6,6 +6,7 @@ import { BrowserSession, Conversation, Customer, + CustomerNote, Tag, User, WidgetSettings, @@ -1087,16 +1088,20 @@ export const enableAccountUser = async ( .then((res) => res.body.data); }; +export type CustomerNotesListResponse = { + data: Array; +}; + export const fetchCustomerNotes = async ( customerId: string, token = getAccessToken() -) => { +): Promise => { if (!token) { throw new Error('Invalid token!'); } return request - .get(`/api/notes`) + .get('/api/notes') .query({customer_id: customerId}) .set('Authorization', token) .then((res) => res.body.data); diff --git a/assets/src/components/customers/CustomerDetailsMainSection.tsx b/assets/src/components/customers/CustomerDetailsMainSection.tsx index e85d979ae..7eed86738 100644 --- a/assets/src/components/customers/CustomerDetailsMainSection.tsx +++ b/assets/src/components/customers/CustomerDetailsMainSection.tsx @@ -5,6 +5,7 @@ import {Box} from 'theme-ui'; import {Tabs} from '../common'; import CustomerDetailsCard from './CustomerDetailsCard'; import CustomerDetailsConversations from './CustomerDetailsConversations'; +import CustomerDetailsNotes from './CustomerDetailsNotes'; const {TabPane} = Tabs; @@ -31,7 +32,7 @@ const CustomerDetailsMainSection = ({customerId, history}: Props) => { /> - Notes + diff --git a/assets/src/components/customers/CustomerDetailsNewNoteInput.tsx b/assets/src/components/customers/CustomerDetailsNewNoteInput.tsx new file mode 100644 index 000000000..92e5d5b81 --- /dev/null +++ b/assets/src/components/customers/CustomerDetailsNewNoteInput.tsx @@ -0,0 +1,70 @@ +import React, {useState} from 'react'; +import * as API from '../../api'; +import {Box} from 'theme-ui'; +import logger from '../../logger'; +import {CustomerNote} from '../../types'; +import {TextArea, Text, Button} from '../common'; +import {formatServerError} from '../../utils'; + +const CustomerDetailNewNoteInput = ({ + customerId, + onCreateNote, +}: { + customerId: string; + onCreateNote: (note: CustomerNote) => void; +}) => { + const [isSaving, setIsSaving] = useState(false); + const [errorMessage, setErrorMessage] = useState(''); + const [note, setNote] = useState(''); + + const handleNoteChange = (e: React.ChangeEvent) => { + setNote(e.target.value); + }; + + const handleSaveNote = async () => { + if (isSaving || note.length < 1) { + return; + } + + setIsSaving(true); + + try { + const newNote = await API.createCustomerNote(customerId, note); + onCreateNote(newNote); + setNote(''); + } catch (error) { + logger.error('Error creating customer note:', error); + const errorMessage = formatServerError(error); + setErrorMessage(errorMessage); + } + + setIsSaving(false); + }; + + return ( + +