Jake Vanderwerf
2025-09-30 2cb91676044ecd0abd9c45b4835abb8b0d042312
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
import { __ } from '@wordpress/i18n';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { PanelBody, TextControl, ToggleControl, SelectControl, CheckboxControl } from '@wordpress/components';
import { useEffect, useState } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import './editor.scss';
 
export default function Edit({ attributes, setAttributes }) {
    const blockProps = useBlockProps();
    const [availableTypes, setAvailableTypes] = useState({});
 
    useEffect(() => {
        apiFetch({ path: '/jvb/v1/types' }).then(types => {
            setAvailableTypes(JSON.parse(types));
        });
    }, []);
 
    return (
        <>
            <InspectorControls>
                <PanelBody title={__('Feed Settings', 'jvb')}>
                    <TextControl
                        label={__('Title', 'jvb')}
                        value={attributes.title}
                        onChange={(title) => setAttributes({ title })}
                    />
 
                    <ToggleControl
                        label={__('Inherit Current Query', 'jvb')}
                        help={__('Inherit filters from the current archive or taxonomy query', 'jvb')}
                        checked={attributes.inheritQuery}
                        onChange={(inheritQuery) => setAttributes({ inheritQuery })}
                    />
 
                    {!attributes.inheritQuery && (
                        <div className="feed-content-types">
                            <p className="components-base-control__label">
                                {__('Content Types', 'jvb')}
                            </p>
                            <div className="checkbox-list">
                                {Object.entries(availableTypes).map(([id, label]) => (
                                    <CheckboxControl
                                        key={id}
                                        label={label}
                                        checked={attributes.contentTypes.includes(id)}
                                        onChange={(isChecked) => {
                                            const newTypes = isChecked
                                                ? [...attributes.contentTypes, id]
                                                : attributes.contentTypes.filter(t => t !== id);
                                            setAttributes({contentTypes: newTypes});
                                        }}
                                    />
                                ))}
                            </div>
                            <div className="select-all-wrapper">
                                <CheckboxControl
                                    label={__('Select All', 'jvb')}
                                    checked={attributes.contentTypes.length === Object.keys(availableTypes).length}
                                    onChange={(isChecked) => {
                                        setAttributes({
                                            contentTypes: isChecked ? Object.keys(availableTypes) : []
                                        });
                                    }}
                                />
                            </div>
                        </div>
                    )}
 
                    <SelectControl
                        label={__('Items Per Page', 'jvb')}
                        value={attributes.itemsPerPage}
                        options={[
                            {label: '12', value: 12},
                            {label: '24', value: 24},
                            {label: '36', value: 36}
                        ]}
                        onChange={(itemsPerPage) => setAttributes({itemsPerPage: parseInt(itemsPerPage)})}
                    />
 
                    <SelectControl
                        label={__('Default Order', 'jvb')}
                        value={attributes.defaultOrder}
                        options={[
                            {label: __('Newest First', 'jvb'), value: 'date_desc' },
                            { label: __('Oldest First', 'jvb'), value: 'date_asc' },
                            { label: __('Random', 'jvb'), value: 'random' }
                        ]}
                        onChange={(defaultOrder) => setAttributes({ defaultOrder })}
                    />
                </PanelBody>
            </InspectorControls>
 
            <div {...blockProps}>
                <div className="feed-block-preview">
                    <h2>{attributes.title}</h2>
                    <div className="feed-filters">
                        <div className="filter-preview">
                            {attributes.contentTypes.map(type => (
                                <span key={type} className="content-type-badge">
                                    {availableTypes[type]}
                                </span>
                            ))}
                        </div>
                    </div>
                    <div className="feed-grid-placeholder">
                        {[...Array(6)].map((_, i) => (
                            <div key={i} className="grid-item-placeholder" />
                        ))}
                    </div>
                </div>
            </div>
        </>
    );
}