import {__, _x} from '@wordpress/i18n'; import {useState} from 'react'; import apiFetch from '@wordpress/api-fetch'; import {Button, Card, Modal, Notice, Pagination as ListTablePagination, Spinner, Table} from '@givewp/components'; import {useForms} from '../../resources/utils'; import useCampaigns from '../../../Campaigns/Blocks/shared/hooks/useCampaigns'; import {format} from 'date-fns'; import * as locales from 'date-fns/locale'; import styles from './styles.module.scss'; const browserLanguage = navigator.language; const localizedCode = browserLanguage.replace('-', ''); const genericCode = browserLanguage.split('-')[0]; const locale = locales[localizedCode] ?? locales[genericCode] ?? locales.enUS; const dateFormat = _x('MM/dd/yyyy \'at\' h:mmaaa', 'Date format', 'give'); type Props = { ids: number[]; currentPage: number; showModal: boolean; campaign?: number; } const OrphanedFormsListTable = () => { const [state, setState] = useState({ ids: [], currentPage: 1, showModal: false, }); const {forms, hasResolved, totalPages} = useForms({ page: state.currentPage, status: ['orphaned'], }); const {campaigns, hasResolved: campaignsLoaded} = useCampaigns(); const showModal = (showModal: boolean) => { setState(prevState => { return { ...prevState, showModal, }; }); }; const selectForm = (selectedId: number) => { let ids = state.ids; if (ids.includes(selectedId)) { ids = ids.filter(id => id !== selectedId); } else { ids.push(selectedId); } setState(prevState => { return { ...prevState, ids, }; }); }; const selectCampaign = (campaign: number) => { setState(prevState => { return { ...prevState, campaign, }; }); }; const setCurrentPage = (currentPage: number) => { setState((previousState) => { return { ...previousState, currentPage, }; }); }; const getFormName = (id: number) => { //@ts-ignore const form = forms.filter(form => form.id === id); //@ts-ignore return form[0].title; }; const handleSave = () => { apiFetch({ path: '/givewp/v3/associate-forms-with-campaign', method: 'POST', data: { campaignId: state.campaign, formIDs: state.ids, }, }).then(() => { window.location.reload(); }); }; const Pagination = () => (
setCurrentPage(page)} totalPages={totalPages} disabled={!hasResolved} />
); const ConfirmationModal = () => { return ( showModal(false)} > {__('Associate forms with campaign', 'give')} showModal(false)} />

{__('Selected forms', 'give')}

{state.ids.map(id => (

{getFormName(id)}

))}

{__('Select campaign', 'give')}

); }; const columns = [ { key: 'id', label: __('Donation Form', 'give'), }, { key: 'createdAt', label: __('Date created', 'give'), }, ]; const columnFilters = { id: (id: number, form: {title: string}) => (
selectForm(id)} />
{form.title}
), createdAt: (value: {date: string}) => { return format(new Date(value.date), dateFormat, {locale}); }, }; if (!hasResolved) { return (

{__('Loading donation forms', 'give')}

); } return ( <>
{hasResolved && forms && ( )}
{state.showModal && campaignsLoaded && } ); }; export default OrphanedFormsListTable;