import { useEffect, useState } from '@wordpress/element'; import { Button, Flex, Modal, TextareaControl } from '@wordpress/components'; import PropTypes from 'prop-types'; import { Table } from '../../../components/table/Table'; import { apiURL } from '../../../data/apiURL'; import { vfAPI } from '../../../shared/vfAPI'; import { DeleteButton } from '../../../components/DeleteButton'; interface Notes { data: { id: number; content: string; created_at: string; admin: { id: number; email: string; name: string; }; }[]; from: number; last_page: number; to: number; total: number; } export const AccountNotes = (props) => { const { customerId } = props; const DEFAULT_PER_PAGE = 30; const MAX_PER_PAGE = 500; const [content, setContent] = useState(''); const [perPage, setPerPage] = useState(DEFAULT_PER_PAGE); const [isBusy, setBusy] = useState(); const [notes, setNotes] = useState(); const destroy = (id: number) => { setBusy(true); const url = `${apiURL.CUSTOMERS}/${customerId}/notes/${id}`; vfAPI.delete(url).then((response) => { if (!response.data.errors.length) { index(); setBusy(false); } }); }; const handleDelete = (id: number) => { destroy(id); }; const handleMore = () => { setPerPage(MAX_PER_PAGE); }; const handleSubmit = (e) => { e.preventDefault(); store(); }; const index = () => { setBusy(true); const url = `${apiURL.CUSTOMERS}/${customerId}/notes`; const config = { params: { perPage, }, }; vfAPI.get(url, config).then((response) => { if (!response.data.errors.length) { setNotes(response.data.notes); setBusy(false); } }); }; const store = () => { setBusy(true); const url = `${apiURL.CUSTOMERS}/${customerId}/notes`; const data = { content }; vfAPI .post(url, data) .then((response) => { if (!response.data.errors.length) { index(); setContent(''); } }) .finally(() => { setBusy(false); }); }; useEffect(() => { // Set the initial notes passed down from parent component. if (props.notes) { setNotes(props.notes); } }, [props.notes]); useEffect(() => { // Only reindex if the Load more button triggers setting perPage. if (perPage === MAX_PER_PAGE) { index(); } }, [perPage]); return ( <>
{notes && notes.total > 0 && ( {notes && notes.data.map((note) => ( ))}
Customer notes
Created Content Author
{new Intl.DateTimeFormat('en-US', { dateStyle: 'short', timeStyle: 'short', }).format(new Date(note.created_at))} {note.content} {note.admin.name} handleDelete(note.id) } />
)} {notes && notes?.last_page > 1 && ( )} ); }; AccountNotes.propTypes = { customerId: PropTypes.number.isRequired, notes: PropTypes.object.isRequired, };