Jake Vanderwerf
7 days ago 46d681c6b825d21b3f698d793c4e630c687d90ad
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * Feed Block - Edit Component
 * Fetches available feed types from /jvb/v1/feed/types
 * Allows configuration of content types and inherit query setting
 */
 
import { useEffect, useState } from '@wordpress/element';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import {
    PanelBody,
    CheckboxControl,
    ToggleControl,
    Spinner,
    Notice
} from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
 
export default function Edit({ attributes, setAttributes }) {
    const [feedTypes, setFeedTypes] = useState(null);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);
 
    const blockProps = useBlockProps({
        className: 'feed-block-editor'
    });
 
    /**
     * Fetch available feed types on component mount
     */
    useEffect(() => {
        apiFetch({
            path: '/jvb/v1/feed/types',
            headers: {
                'If-Modified-Since': localStorage.getItem('feed_types_modified'),
            }
        })
            .then(types => {
                setFeedTypes(types);
                setLoading(false);
 
                // Store Last-Modified for future requests
                // (apiFetch doesn't expose response headers easily,
                // but the server will handle 304s)
 
                // Initialize contentTypes if not set and not inheriting
                if (!attributes.contentTypes && !attributes.inheritQuery) {
                    const firstType = Object.keys(types)[0];
                    if (firstType) {
                        setAttributes({ contentTypes: [firstType] });
                    }
                }
            })
            .catch(err => {
                console.error('Error loading feed types:', err);
                setError(err.message);
                setLoading(false);
            });
    }, [attributes.inheritQuery]);
 
    /**
     * Toggle a content type in the selection
     */
    const toggleContentType = (slug, checked) => {
        const currentTypes = attributes.contentTypes || [];
 
        const newTypes = checked
            ? [...currentTypes, slug]
            : currentTypes.filter(t => t !== slug);
 
        setAttributes({ contentTypes: newTypes });
    };
 
    /**
     * Get friendly label for content type
     */
    const getTypeLabel = (slug, config) => {
        return `${config.plural} (${config.type})`;
    };
 
    /**
     * Group types by category for better UX
     */
    const groupedTypes = feedTypes ? {
        content: Object.entries(feedTypes)
            .filter(([_, config]) => config.type === 'content'),
        taxonomy: Object.entries(feedTypes)
            .filter(([_, config]) => config.type === 'taxonomy')
    } : { content: [], taxonomy: [] };
 
    return (
        <div {...blockProps}>
            <InspectorControls>
                <PanelBody
                    title={__('Feed Settings', 'jvb')}
                    initialOpen={true}
                >
                    <ToggleControl
                        label={__('Inherit from Page Context', 'jvb')}
                        help={
                            attributes.inheritQuery
                                ? __('Feed will adapt to the current page (profile, taxonomy, etc.)', 'jvb')
                                : __('Manually select content types to display', 'jvb')
                        }
                        checked={attributes.inheritQuery}
                        onChange={(value) => setAttributes({ inheritQuery: value })}
                    />
 
                    {!attributes.inheritQuery && (
                        <>
                            {loading && (
                                <div style={{ textAlign: 'center', padding: '20px' }}>
                                    <Spinner />
                                    <p>{__('Loading feed types...', 'jvb')}</p>
                                </div>
                            )}
 
                            {error && (
                                <Notice status="error" isDismissible={false}>
                                    {__('Error loading feed types: ', 'jvb')} {error}
                                </Notice>
                            )}
 
                            {!loading && !error && feedTypes && (
                                <>
                                    {groupedTypes.content.length > 0 && (
                                        <>
                                            <h4>{__('Content Types', 'jvb')}</h4>
                                            {groupedTypes.content.map(([slug, config]) => (
                                                <CheckboxControl
                                                    key={slug}
                                                    label={getTypeLabel(slug, config)}
                                                    checked={
                                                        attributes.contentTypes?.includes(slug) || false
                                                    }
                                                    onChange={(checked) =>
                                                        toggleContentType(slug, checked)
                                                    }
                                                    help={
                                                        config.taxonomies?.length > 0
                                                            ? `Filters: ${config.taxonomies.join(', ')}`
                                                            : null
                                                    }
                                                />
                                            ))}
                                        </>
                                    )}
 
                                    {groupedTypes.taxonomy.length > 0 && (
                                        <>
                                            <h4 style={{ marginTop: '20px' }}>
                                                {__('Content Taxonomies', 'jvb')}
                                            </h4>
                                            <p style={{ fontSize: '12px', color: '#757575' }}>
                                                {__('These are collections that group other content', 'jvb')}
                                            </p>
                                            {groupedTypes.taxonomy.map(([slug, config]) => (
                                                <CheckboxControl
                                                    key={slug}
                                                    label={getTypeLabel(slug, config)}
                                                    checked={
                                                        attributes.contentTypes?.includes(slug) || false
                                                    }
                                                    onChange={(checked) =>
                                                        toggleContentType(slug, checked)
                                                    }
                                                    help={
                                                        config.for_content?.length > 0
                                                            ? `Contains: ${config.for_content.join(', ')}`
                                                            : null
                                                    }
                                                />
                                            ))}
                                        </>
                                    )}
 
                                    {!attributes.contentTypes?.length && (
                                        <Notice status="warning" isDismissible={false}>
                                            {__('Please select at least one content type', 'jvb')}
                                        </Notice>
                                    )}
                                </>
                            )}
                        </>
                    )}
                </PanelBody>
 
                <PanelBody
                    title={__('Display Settings', 'jvb')}
                    initialOpen={false}
                >
                    <ToggleControl
                        label={__('Show Gallery View', 'jvb')}
                        help={__('Enable lightbox for images', 'jvb')}
                        checked={attributes.enableGallery || false}
                        onChange={(value) =>
                            setAttributes({ enableGallery: value })
                        }
                    />
                </PanelBody>
            </InspectorControls>
 
            <div className="feed-block-placeholder">
                <div className="feed-block-icon">
                    <svg width="48" height="48" viewBox="0 0 24 24" fill="none">
                        <rect x="3" y="3" width="7" height="7" fill="currentColor" opacity="0.3" />
                        <rect x="13" y="3" width="7" height="7" fill="currentColor" opacity="0.3" />
                        <rect x="3" y="13" width="7" height="7" fill="currentColor" opacity="0.3" />
                        <rect x="13" y="13" width="7" height="7" fill="currentColor" opacity="0.3" />
                    </svg>
                </div>
 
                <h3>{__('Feed Block', 'jvb')}</h3>
 
                {attributes.inheritQuery ? (
                    <p className="feed-block-description">
                        {__('📍 Inheriting from page context', 'jvb')}
                    </p>
                ) : (
                    <div className="feed-block-description">
                        {attributes.contentTypes?.length > 0 ? (
                            <>
                                <p><strong>{__('Showing:', 'jvb')}</strong></p>
                                <ul style={{
                                    listStyle: 'none',
                                    padding: '0',
                                    margin: '8px 0'
                                }}>
                                    {attributes.contentTypes.map(type => {
                                        const config = feedTypes?.[type];
                                        return (
                                            <li key={type} style={{
                                                padding: '4px 0',
                                                color: '#2271b1'
                                            }}>
                                                ✓ {config?.plural || type}
                                            </li>
                                        );
                                    })}
                                </ul>
                            </>
                        ) : (
                            <p style={{ color: '#d63638' }}>
                                {__('⚠️  No content types selected', 'jvb')}
                            </p>
                        )}
                    </div>
                )}
 
                <p className="feed-block-note">
                    {__('Feed will be displayed on the frontend', 'jvb')}
                </p>
            </div>
        </div>
    );
}