Skip to content

Commit bf0c9d5

Browse files
authored
Blocks: Add state & context docs (#1768)
1 parent 5fb3859 commit bf0c9d5

File tree

11 files changed

+97
-46
lines changed

11 files changed

+97
-46
lines changed

build/follow-me/render.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
'copy' => __( 'Copy', 'activitypub' ),
4343
'copied' => __( 'Copied!', 'activitypub' ),
4444
'emptyProfileError' => __( 'Please enter a profile URL or handle.', 'activitypub' ),
45-
'invalidProfileError' => __( 'Please enter a valid profile URL or handle.', 'activitypub' ),
4645
'genericError' => __( 'An error occurred. Please try again.', 'activitypub' ),
46+
'invalidProfileError' => __( 'Please enter a valid profile URL or handle.', 'activitypub' ),
4747
),
4848
)
4949
);
@@ -61,19 +61,17 @@
6161

6262
$wrapper_context = wp_interactivity_data_wp_context(
6363
array(
64+
'backgroundColor' => $background_color,
6465
'blockId' => $block_id,
65-
'remoteProfile' => '',
66-
'isLoading' => false,
67-
'isError' => false,
68-
'errorMessage' => '',
66+
'buttonStyle' => $button_style,
6967
'copyButtonText' => __( 'Copy', 'activitypub' ),
68+
'errorMessage' => '',
69+
'isError' => false,
70+
'isLoading' => false,
71+
'modal' => array( 'isOpen' => false ),
72+
'remoteProfile' => '',
7073
'userId' => $user_id,
71-
'buttonStyle' => $button_style,
72-
'backgroundColor' => $background_color,
7374
'webfinger' => '@' . $actor->get_webfinger(),
74-
'modal' => array(
75-
'isOpen' => false,
76-
),
7775
)
7876
);
7977

build/followers/render.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ function ( $follower ) {
7474

7575
// Set initial context data.
7676
$context = array(
77-
'userId' => $user_id,
77+
'followers' => $followers,
78+
'isLoading' => false,
79+
'order' => $attributes['order'],
7880
'page' => 1,
81+
'pages' => ceil( $follower_data['total'] / $_per_page ),
7982
'per_page' => $_per_page,
80-
'order' => $attributes['order'],
81-
'followers' => $followers,
8283
'total' => $follower_data['total'],
83-
'pages' => ceil( $follower_data['total'] / $_per_page ),
84-
'isLoading' => false,
84+
'userId' => $user_id,
8585
);
8686

8787
// Get block wrapper attributes with the data-wp-interactive attribute.

build/reactions/render.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ function ( $reaction ) use ( $attributes ) {
115115
// Initialize the context for the block.
116116
$context = array(
117117
'blockId' => $block_id,
118-
'reactions' => $reactions,
119-
'postId' => $_post_id,
120118
'modal' => array(
121119
'isCompact' => true,
122120
'isOpen' => false,
123121
'items' => array(),
124122
),
123+
'postId' => $_post_id,
124+
'reactions' => $reactions,
125125
);
126126

127127
// Add the block wrapper attributes.

build/remote-reply/render.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
'copied' => __( 'Copied!', 'activitypub' ),
2727
'copy' => __( 'Copy', 'activitypub' ),
2828
'emptyProfileError' => __( 'Please enter a profile URL or handle.', 'activitypub' ),
29-
'invalidProfileError' => __( 'Please enter a valid URL or handle.', 'activitypub' ),
3029
'genericError' => __( 'An error occurred. Please try again.', 'activitypub' ),
30+
'invalidProfileError' => __( 'Please enter a valid URL or handle.', 'activitypub' ),
3131
),
3232
)
3333
);
@@ -52,13 +52,11 @@
5252
'hasRemoteUser' => false,
5353
'isError' => false,
5454
'isLoading' => false,
55+
'modal' => array( 'isOpen' => false ),
5556
'profileURL' => '',
5657
'remoteProfile' => '',
5758
'shouldSaveProfile' => true,
5859
'template' => '',
59-
'modal' => array(
60-
'isOpen' => false,
61-
),
6260
)
6361
);
6462

src/follow-me/render.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
'copy' => __( 'Copy', 'activitypub' ),
4343
'copied' => __( 'Copied!', 'activitypub' ),
4444
'emptyProfileError' => __( 'Please enter a profile URL or handle.', 'activitypub' ),
45-
'invalidProfileError' => __( 'Please enter a valid profile URL or handle.', 'activitypub' ),
4645
'genericError' => __( 'An error occurred. Please try again.', 'activitypub' ),
46+
'invalidProfileError' => __( 'Please enter a valid profile URL or handle.', 'activitypub' ),
4747
),
4848
)
4949
);
@@ -61,19 +61,17 @@
6161

6262
$wrapper_context = wp_interactivity_data_wp_context(
6363
array(
64+
'backgroundColor' => $background_color,
6465
'blockId' => $block_id,
65-
'remoteProfile' => '',
66-
'isLoading' => false,
67-
'isError' => false,
68-
'errorMessage' => '',
66+
'buttonStyle' => $button_style,
6967
'copyButtonText' => __( 'Copy', 'activitypub' ),
68+
'errorMessage' => '',
69+
'isError' => false,
70+
'isLoading' => false,
71+
'modal' => array( 'isOpen' => false ),
72+
'remoteProfile' => '',
7073
'userId' => $user_id,
71-
'buttonStyle' => $button_style,
72-
'backgroundColor' => $background_color,
7374
'webfinger' => '@' . $actor->get_webfinger(),
74-
'modal' => array(
75-
'isOpen' => false,
76-
),
7775
)
7876
);
7977

src/follow-me/view.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ const { apiFetch } = window.wp;
77

88
createModalStore( 'activitypub/follow-me' );
99

10+
/**
11+
* @typedef {Object} state
12+
* @property {String} namespace ActivityPub REST Namespace.
13+
* @property {Object} i18n Internationalization strings.
14+
* @property {String} i18n.copy "Copy" button text.
15+
* @property {String} i18n.copied "Copied" button text.
16+
* @property {String} i18n.emptyProfileError Error message for empty remote profile.
17+
* @property {String} i18n.genericError Generic error message.
18+
* @property {String} i18n.invalidProfileError Error message for invalid remote profile.
19+
*/
20+
21+
/**
22+
* @typedef {Object} context
23+
* @property {String} backgroundColor The background color for the button.
24+
* @property {String} blockId The block ID.
25+
* @property {String} buttonStyle The button style.
26+
* @property {String} copyButtonText The copy button text.
27+
* @property {String} errorMessage The error message.
28+
* @property {boolean} isError Whether the remote profile input has an error.
29+
* @property {boolean} isLoading Whether the remote profile is being submitted.
30+
* @property {Object} modal The modal state.
31+
* @property {boolean} modal.isOpen Whether the modal is open.
32+
* @property {String} template The template for the remote reply URL.
33+
* @property {String} userId The user ID.
34+
* @property {String} webfinger The webfinger of the user.
35+
*/
36+
1037
const { actions, callbacks, state } = store( 'activitypub/follow-me', {
1138
actions: {
1239
/**

src/followers/render.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ function ( $follower ) {
7474

7575
// Set initial context data.
7676
$context = array(
77-
'userId' => $user_id,
77+
'followers' => $followers,
78+
'isLoading' => false,
79+
'order' => $attributes['order'],
7880
'page' => 1,
81+
'pages' => ceil( $follower_data['total'] / $_per_page ),
7982
'per_page' => $_per_page,
80-
'order' => $attributes['order'],
81-
'followers' => $followers,
8283
'total' => $follower_data['total'],
83-
'pages' => ceil( $follower_data['total'] / $_per_page ),
84-
'isLoading' => false,
84+
'userId' => $user_id,
8585
);
8686

8787
// Get block wrapper attributes with the data-wp-interactive attribute.

src/followers/view.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,27 @@ import { store, getContext } from '@wordpress/interactivity';
66
*/
77
const { apiFetch, url } = window.wp;
88

9-
// Register the store for the followers block.
9+
/**
10+
* @typedef {Object} context
11+
* @property {Array} followers The list of followers.
12+
* @property {boolean} isLoading Whether the followers are currently being fetched.
13+
* @property {String} order The order in which to fetch followers (e.g., 'asc', 'desc').
14+
* @property {Number} page The current page of followers.
15+
* @property {Number} pages The total number of pages of followers.
16+
* @property {Number} per_page The number of followers per page.
17+
* @property {Number} total The total number of followers.
18+
* @property {String} userId The user ID for which to fetch followers.
19+
*/
20+
1021
const { actions, state } = store( 'activitypub/followers', {
22+
/**
23+
* @typedef {Object} state
24+
* @property {String} defaultAvatarUrl Default avatar URL.
25+
* @property {String} namespace ActivityPub REST Namespace.
26+
* @property {Function} paginationText Get the pagination text.
27+
* @property {Function} disablePreviousLink Whether the previous link should be disabled.
28+
* @property {Function} disableNextLink Whether the next link should be disabled.
29+
*/
1130
state: {
1231
/**
1332
* Get the pagination text.

src/reactions/render.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ function ( $reaction ) use ( $attributes ) {
115115
// Initialize the context for the block.
116116
$context = array(
117117
'blockId' => $block_id,
118-
'reactions' => $reactions,
119-
'postId' => $_post_id,
120118
'modal' => array(
121119
'isCompact' => true,
122120
'isOpen' => false,
123121
'items' => array(),
124122
),
123+
'postId' => $_post_id,
124+
'reactions' => $reactions,
125125
);
126126

127127
// Add the block wrapper attributes.

src/reactions/view.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@ const { apiFetch } = window.wp;
77
createModalStore( 'activitypub/reactions' );
88

99
/**
10-
* @var {Object} state
11-
* @var {Object} state.reactions Reactions data.
12-
* @var {String} state.defaultAvatarUrl Default avatar URL.
10+
* @typedef {Object} state
11+
* @property {Object} reactions Reactions data, keyed by post ID.
12+
* @property {String} defaultAvatarUrl Default avatar URL.
13+
* @property {String} namespace API namespace for ActivityPub.
1314
*/
15+
16+
/**
17+
* @typedef {Object} context
18+
* @property {String} blockId The block ID.
19+
* @property {Object} modal The modal state.
20+
* @property {boolean} modal.isCompact Whether the modal is compact.
21+
* @property {boolean} modal.isOpen Whether the modal is open.
22+
* @property {Object} modal.items The items to display in the modal.
23+
* @property {String} postId The post ID.
24+
* @property {Object} reactions Reactions data, keyed by reaction type.
25+
*/
26+
1427
const { callbacks, state } = store( 'activitypub/reactions', {
1528
actions: {
1629
/**

0 commit comments

Comments
 (0)