/** * WordPress dependencies */ import apiFetch from '@safe-wordpress/api-fetch'; import { select, dispatch } from '@safe-wordpress/data'; /** * External dependencies */ import { store as NC_DATA } from '@nelio-content/data'; import { isDefined, showErrorNotice } from '@nelio-content/utils'; import type { PostStatus } from '@nelio-content/types'; /** * Internal dependencies */ import { store as NC_STATUSES } from '../store'; export async function saveStatuses(): Promise< void > { await dispatch( NC_STATUSES ).markAsSaving( true ); const { getStatus, getStatuses } = select( NC_STATUSES ); const statuses = getStatuses().map( getStatus ).filter( isDefined ); try { const result = await apiFetch< ReadonlyArray< PostStatus > >( { path: '/nelio-content/v1/statuses', method: 'POST', data: { statuses: statuses.map( ( s ) => ( { ...s, available: true, } ) ), }, } ); await dispatch( NC_DATA ).resetStatuses( result ); await dispatch( NC_STATUSES ).resetStatuses( result ); } catch ( e ) { await showErrorNotice( e ); } await dispatch( NC_STATUSES ).markAsSaving( false ); }