import { h } from 'preact'; import PropTypes from 'prop-types'; import { Dropdown, ButtonNew as Button } from '@crayons'; import CogIcon from '@images/cog.svg'; /** * Component comprising a trigger button and dropdown with additional post options. * * @param {Object} props * @param {Object} props.passedData The current post options data * @param {Function} props.onSaveDraft Callback for when the post draft is saved * @param {Function} props.onConfigChange Callback for when the config options have changed */ function toISOStringLocal(datetime) { const month = (datetime.getMonth() + 1).toString().padStart(2, '0'); const day = datetime.getDate().toString().padStart(2, '0'); return [ datetime.getFullYear(), '-', month, '-', day, 'T', datetime.toLocaleTimeString(), ].join(''); } export const Options = ({ passedData: { published = false, publishedAt = '', timezone = Intl.DateTimeFormat().resolvedOptions().timeZone, allSeries = [], canonicalUrl = '', series = '', }, schedulingEnabled, onSaveDraft, onConfigChange, previewLoading, }) => { let publishedField = ''; let existingSeries = ''; let publishedAtField = ''; const publishedDate = new Date(publishedAt); const currentDate = new Date(); const localPublishedAt = toISOStringLocal(publishedDate); const minPublishedAt = toISOStringLocal(currentDate); const readonlyPublishedAt = published && publishedDate < currentDate; if (allSeries.length > 0) { const seriesNames = allSeries.map((name, index) => { return ( ); }); existingSeries = (
Existing series: {` `}
); } if (published) { publishedField = (
Danger Zone
); } if (schedulingEnabled && !readonlyPublishedAt) { publishedAtField = (
); } return (
); }; Options.propTypes = { passedData: PropTypes.shape({ published: PropTypes.bool.isRequired, publishedAt: PropTypes.string.isRequired, timezone: PropTypes.string.isRequired, allSeries: PropTypes.array.isRequired, canonicalUrl: PropTypes.string.isRequired, series: PropTypes.string.isRequired, }).isRequired, schedulingEnabled: PropTypes.bool.isRequired, onSaveDraft: PropTypes.func.isRequired, onConfigChange: PropTypes.func.isRequired, previewLoading: PropTypes.bool.isRequired, }; Options.displayName = 'Options';