-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
129 lines (121 loc) · 3.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* WordPress dependencies
*/
import {
Button,
Modal,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
TextControl,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { decodeEntities } from '@wordpress/html-entities';
import { serialize, synchronizeBlocksWithTemplate } from '@wordpress/blocks';
export default function AddNewPostModal( { postType, onSave, onClose } ) {
const labels = useSelect(
( select ) => select( coreStore ).getPostType( postType )?.labels,
[ postType ]
);
const [ isCreatingPost, setIsCreatingPost ] = useState( false );
const [ title, setTitle ] = useState( '' );
const { saveEntityRecord } = useDispatch( coreStore );
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );
const { resolveSelect } = useRegistry();
async function createPost( event ) {
event.preventDefault();
if ( isCreatingPost ) {
return;
}
setIsCreatingPost( true );
try {
const postTypeObject =
await resolveSelect( coreStore ).getPostType( postType );
const newPage = await saveEntityRecord(
'postType',
postType,
{
status: 'draft',
title,
slug: title || __( 'No title' ),
content:
!! postTypeObject.template &&
postTypeObject.template.length
? serialize(
synchronizeBlocksWithTemplate(
[],
postTypeObject.template
)
)
: undefined,
},
{ throwOnError: true }
);
onSave( newPage );
createSuccessNotice(
sprintf(
// translators: %s: Title of the created post or template, e.g: "Hello world".
__( '"%s" successfully created.' ),
decodeEntities( newPage.title?.rendered || title )
),
{ type: 'snackbar' }
);
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the item.' );
createErrorNotice( errorMessage, {
type: 'snackbar',
} );
} finally {
setIsCreatingPost( false );
}
}
return (
<Modal
title={
// translators: %s: post type singular_name label e.g: "Page".
sprintf( __( 'Draft new: %s' ), labels?.singular_name )
}
onRequestClose={ onClose }
focusOnMount="firstContentElement"
size="small"
>
<form onSubmit={ createPost }>
<VStack spacing={ 4 }>
<TextControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Title' ) }
onChange={ setTitle }
placeholder={ __( 'No title' ) }
value={ title }
/>
<HStack spacing={ 2 } justify="end">
<Button
__next40pxDefaultSize
variant="tertiary"
onClick={ onClose }
>
{ __( 'Cancel' ) }
</Button>
<Button
__next40pxDefaultSize
variant="primary"
type="submit"
isBusy={ isCreatingPost }
aria-disabled={ isCreatingPost }
>
{ __( 'Create draft' ) }
</Button>
</HStack>
</VStack>
</form>
</Modal>
);
}