@@ -2,6 +2,8 @@ import { Toolbar, VanillaRenderer } from '@wpengine/hwp-toolbar';
22import '@wpengine/hwp-toolbar/styles' ;
33import './style.css' ;
44
5+ const WP_URL = 'http://localhost:8888' ;
6+
57// Create toolbar instance
68const toolbar = new Toolbar ( {
79 onPreviewChange : ( enabled ) => {
@@ -18,20 +20,35 @@ toolbar.subscribe((nodes, state) => {
1820 document . getElementById ( 'state' ) . textContent = JSON . stringify ( state , null , 2 ) ;
1921} ) ;
2022
21- // Control functions
22- window . login = ( ) => {
23- toolbar . setState ( {
24- user : {
25- id : 1 ,
26- name : 'Admin' ,
27- email : 'admin@example.com'
28- } ,
29- site : {
30- adminUrl : 'http://localhost:8888/wp-admin' ,
31- url : 'http://localhost:8888'
23+ // Fetch real WordPress user
24+ window . login = async ( ) => {
25+ try {
26+ // For demo: just set mock authenticated user
27+ // In production, you'd authenticate first via WordPress login
28+ const response = await fetch ( `${ WP_URL } /?rest_route=/wp/v2/users/1` ) ;
29+
30+ if ( response . ok ) {
31+ const user = await response . json ( ) ;
32+ toolbar . setState ( {
33+ user : {
34+ id : user . id ,
35+ name : user . name ,
36+ email : user . email || 'admin@example.com'
37+ } ,
38+ site : {
39+ adminUrl : `${ WP_URL } /wp-admin` ,
40+ url : WP_URL
41+ }
42+ } ) ;
43+ console . log ( 'User logged in:' , user . name ) ;
44+ } else {
45+ console . error ( 'WordPress not available' ) ;
46+ alert ( 'WordPress is not running at ' + WP_URL ) ;
3247 }
33- } ) ;
34- console . log ( 'User logged in' ) ;
48+ } catch ( error ) {
49+ console . error ( 'Error connecting to WordPress:' , error ) ;
50+ alert ( 'Error: Make sure WordPress is running (npm run wp:start)' ) ;
51+ }
3552} ;
3653
3754window . logout = ( ) => {
@@ -45,6 +62,66 @@ window.logout = () => {
4562 console . log ( 'User logged out' ) ;
4663} ;
4764
65+ // Fetch real posts from WordPress GraphQL
66+ window . fetchPosts = async ( ) => {
67+ try {
68+ const response = await fetch ( `${ WP_URL } /?graphql` , {
69+ method : 'POST' ,
70+ headers : {
71+ 'Content-Type' : 'application/json' ,
72+ } ,
73+ body : JSON . stringify ( {
74+ query : `
75+ query GetPosts {
76+ posts(first: 5) {
77+ nodes {
78+ databaseId
79+ title
80+ slug
81+ status
82+ }
83+ }
84+ }
85+ `
86+ } )
87+ } ) ;
88+
89+ const { data } = await response . json ( ) ;
90+
91+ if ( data ?. posts ?. nodes ?. length > 0 ) {
92+ const posts = data . posts . nodes ;
93+ console . log ( 'Fetched posts:' , posts ) ;
94+
95+ // Display posts in UI
96+ const postsDiv = document . getElementById ( 'wordpress-posts' ) ;
97+ postsDiv . innerHTML = '<h3>WordPress Posts:</h3>' ;
98+ posts . forEach ( post => {
99+ const btn = document . createElement ( 'button' ) ;
100+ btn . textContent = post . title ;
101+ btn . onclick = ( ) => loadPost ( post ) ;
102+ postsDiv . appendChild ( btn ) ;
103+ } ) ;
104+ }
105+ } catch ( error ) {
106+ console . error ( 'Error fetching posts:' , error ) ;
107+ alert ( 'Error fetching posts. Make sure WPGraphQL is installed.' ) ;
108+ }
109+ } ;
110+
111+ function loadPost ( post ) {
112+ toolbar . setState ( {
113+ post : {
114+ id : post . databaseId ,
115+ title : post . title ,
116+ type : 'post' ,
117+ status : post . status ,
118+ slug : post . slug
119+ }
120+ } ) ;
121+ console . log ( 'Post loaded:' , post . title ) ;
122+ }
123+
124+ // Mock data functions (for when WordPress isn't running)
48125window . addPost = ( ) => {
49126 toolbar . setState ( {
50127 post : {
@@ -55,7 +132,7 @@ window.addPost = () => {
55132 slug : 'hello-world'
56133 }
57134 } ) ;
58- console . log ( 'Post context added' ) ;
135+ console . log ( 'Mock post added' ) ;
59136} ;
60137
61138window . addPage = ( ) => {
@@ -68,7 +145,7 @@ window.addPage = () => {
68145 slug : 'about'
69146 }
70147 } ) ;
71- console . log ( 'Page context added' ) ;
148+ console . log ( 'Mock page added' ) ;
72149} ;
73150
74151window . clearPost = ( ) => {
@@ -104,14 +181,15 @@ window.clearAll = () => {
104181// Initialize with home button
105182toolbar . register ( 'home' , 'Home' , ( ) => {
106183 console . log ( 'Navigate to home' ) ;
107- alert ( 'Navigate to Homepage' ) ;
184+ window . location . href = '/' ;
108185} ) ;
109186
110187// Console greeting
111188console . log ( '%cHeadless WordPress Toolbar' , 'font-size: 18px; font-weight: bold; color: #0073aa;' ) ;
112- console . log ( 'For headless WordPress implementations' ) ;
189+ console . log ( 'WordPress at:' , WP_URL ) ;
113190console . log ( '' ) ;
114191console . log ( 'Try this flow:' ) ;
115- console . log ( '1. Click "Login" to authenticate' ) ;
116- console . log ( '2. Click "Add Post" to load content' ) ;
117- console . log ( '3. See WordPress controls appear in toolbar' ) ;
192+ console . log ( '1. Click "Login" to fetch real WP user' ) ;
193+ console . log ( '2. Click "Fetch Posts" to load real posts' ) ;
194+ console . log ( '3. Click a post to add it to toolbar' ) ;
195+ console . log ( '4. See WordPress controls appear in toolbar' ) ;
0 commit comments