/** * WordPress dependencies */ import { Button, Modal, TextControl } from '@safe-wordpress/components'; import { useSelect, useDispatch } from '@safe-wordpress/data'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { store as NC_CALENDAR } from '@nelio-content/calendar'; import { store as NC_DATA } from '@nelio-content/data'; /** * Internal dependencies */ import './style.scss'; export const ExportDialog = (): JSX.Element | null => { const [ isExportDialogOpen, openExportDialog ] = useExportDialog(); const isIcsEnabled = useSelect( ( select ) => select( NC_CALENDAR ).isIcsEnabled(), [] ); const allPostsIcsLink = useIcsLink( 'all' ); const userIcsLink = useIcsLink( 'user' ); const downloadCsv = useCsvDownloader(); if ( ! isExportDialogOpen ) { return null; } return ( openExportDialog( false ) } title={ _x( 'Export Calendar', 'text', 'nelio-content' ) } >
{ _x( 'Export your editorial calendar in CSV format:', 'user', 'nelio-content' ) }
{ isIcsEnabled && (
{ _x( 'Export your editorial calendar to Google Calendar or any tool that supports iCalendar (.ics) format.', 'user', 'nelio-content' ) }
void null } readOnly />
void null } readOnly />
) }
); }; // ===== // HOOKS // ===== const useExportDialog = () => { const isOpen = useSelect( ( select ) => select( NC_CALENDAR ).isExportDialogOpen(), [] ); const { openExportDialog } = useDispatch( NC_CALENDAR ); return [ isOpen, openExportDialog ] as const; }; const useIcsLink = ( type: 'all' | 'user' ) => useSelect( ( select ) => select( NC_CALENDAR ).getIcsLink( type ), [ type ] ); const useCsvDownloader = () => { const firstDayOfWeek = useSelect( ( select ) => select( NC_DATA ).getFirstDayOfWeek(), [] ); const { firstDay, lastDay } = useSelect( ( select ) => { const cal = select( NC_CALENDAR ); return { firstDay: cal.getFirstDay( firstDayOfWeek ), lastDay: cal.getLastDay( firstDayOfWeek ), }; }, [ firstDayOfWeek ] ); const csv = useSelect( ( select ) => select( NC_DATA ).getCSV( firstDay, lastDay ), [ firstDay, lastDay ] ); return () => { const url = window.URL || window.webkitURL; const blob = new window.Blob( [ csv ], { type: 'text/csv;charset=UTF-8', } ); const element = document.createElement( 'a' ); element.setAttribute( 'href', url.createObjectURL( blob ) ); element.setAttribute( 'download', 'export.csv' ); document.body.appendChild( element ); element.click(); document.body.removeChild( element ); }; };