Skip to content

Commit 446c8eb

Browse files
committed
make button disabled and then enabled when appropriate!
1 parent 174ce43 commit 446c8eb

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

src/index.js

+59-21
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@
22
* WordPress dependencies
33
*/
44
import { __ } from "@wordpress/i18n";
5-
import { useSelect, withSelect } from "@wordpress/data";
5+
import { useSelect } from "@wordpress/data";
66
import { store as coreStore } from "@wordpress/core-data";
7-
import { renderToString } from "@wordpress/element";
7+
import { renderToString, useState } from "@wordpress/element";
88
import { addQueryArgs } from "@wordpress/url";
99
import { registerPlugin } from "@wordpress/plugins";
1010

11-
const AddNewPostButton = ({ postType, newPost }) => {
11+
const AddNewPostButton = () => {
12+
const { postType } = useSelect((select) => {
13+
return {
14+
postType: select("core/editor").getCurrentPostType(),
15+
};
16+
});
17+
1218
if (!postType) {
1319
return null;
1420
}
15-
if (newPost) {
16-
return null;
17-
}
21+
const { newState, setNewState } = useState(true);
22+
const { newPost } = useSelect((select) => {
23+
const newPost = select("core/editor").isCleanNewPost();
1824

25+
return {
26+
newPost: newPost,
27+
};
28+
});
1929
const { singleLabel, addNewLabel } = useSelect((select) => {
2030
const { getPostTypes } = select(coreStore);
2131
const includedPostType = [postType];
@@ -34,7 +44,9 @@ const AddNewPostButton = ({ postType, newPost }) => {
3444
singleLabel: undefined,
3545
};
3646
});
47+
// console.log("post is " + newPost);
3748
if (undefined !== addNewLabel) {
49+
console.log("post is " + newPost);
3850
const AddButton = (
3951
<a
4052
class="components-button is-secondary"
@@ -57,29 +69,55 @@ const AddNewPostButton = ({ postType, newPost }) => {
5769
</span>
5870
</a>
5971
);
60-
requestAnimationFrame(() => {
72+
const DisabledButton = (
73+
<button
74+
class="components-button is-secondary"
75+
id="createwithrani-add-new-button"
76+
style={{
77+
textTransform: "capitalize",
78+
margin: "0 1em",
79+
}}
80+
aria-disabled={true}
81+
>
82+
<span>
83+
{sprintf(
84+
/* translators: %1$s: the phrase "Add New", %2$s: Name of current post type. */
85+
__("%1$s %2$s", "createwithrani-add-new-post"),
86+
addNewLabel,
87+
singleLabel
88+
)}
89+
</span>
90+
</button>
91+
);
92+
const paintbutton = () => {
6193
if (!document.querySelector(".edit-post-header-toolbar__left")) {
6294
return;
6395
}
64-
// Redundant extra check added because of a bug where the above check wasn't working, credit: Extendify plugin
6596
if (document.getElementById("createwithrani-add-new-button")) {
66-
return;
97+
var existingButton = document.getElementById(
98+
"createwithrani-add-new-button"
99+
);
100+
existingButton.remove();
67101
}
68-
document
69-
.querySelector(".edit-post-header-toolbar__left")
70-
.insertAdjacentHTML("beforeend", renderToString(AddButton));
71-
});
72-
}
102+
if (newPost) {
103+
document
104+
.querySelector(".edit-post-header-toolbar__left")
105+
.insertAdjacentHTML(
106+
"beforeend",
107+
renderToString(DisabledButton)
108+
);
109+
} else {
110+
document
111+
.querySelector(".edit-post-header-toolbar__left")
112+
.insertAdjacentHTML("beforeend", renderToString(AddButton));
113+
}
114+
};
73115

116+
requestAnimationFrame(paintbutton);
117+
}
74118
return null;
75119
};
76-
const AddNewPostButtonWrapped = withSelect((select) => {
77-
return {
78-
postType: select("core/editor").getCurrentPostType(),
79-
newPost: select("core/editor").isCleanNewPost(),
80-
};
81-
})(AddNewPostButton);
82120

83121
registerPlugin("createwithrani-add-new-post", {
84-
render: AddNewPostButtonWrapped,
122+
render: AddNewPostButton,
85123
});

0 commit comments

Comments
 (0)