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
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { PanelBody, ToggleControl, Notice, Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState, useEffect } from '@wordpress/element';
import './editor.scss';
 
export default function Edit({ attributes, setAttributes }) {
    const { sectionOrder, showSectionTitles, collapseByDefault } = attributes;
    const [sections, setSections] = useState([]);
 
    // Get sections from localized script
    const allSections = window.jvbFaq?.sections || [];
 
    // Initialize sections with proper ordering
    useEffect(() => {
        if (!allSections.length) return;
 
        if (sectionOrder.length === 0) {
            // First time - use default order
            const orderedSections = allSections.map(section => ({
                id: section.id,
                name: section.name,
            }));
            setSections(orderedSections);
            setAttributes({ sectionOrder: orderedSections.map(s => s.id) });
        } else {
            // Use saved order, add any new sections at the end
            const orderedSections = [];
            const existingIds = new Set(sectionOrder);
 
            // Add sections in saved order
            sectionOrder.forEach(id => {
                const section = allSections.find(s => s.id === id);
                if (section) {
                    orderedSections.push({ id: section.id, name: section.name });
                }
            });
 
            // Add any new sections that weren't in the saved order
            allSections.forEach(section => {
                if (!existingIds.has(section.id)) {
                    orderedSections.push({ id: section.id, name: section.name });
                }
            });
 
            setSections(orderedSections);
        }
    }, [allSections, sectionOrder]);
 
    const moveSection = (index, direction) => {
        const newSections = [...sections];
        const newIndex = direction === 'up' ? index - 1 : index + 1;
 
        if (newIndex < 0 || newIndex >= newSections.length) return;
 
        // Swap sections
        [newSections[index], newSections[newIndex]] = [newSections[newIndex], newSections[index]];
 
        setSections(newSections);
        setAttributes({ sectionOrder: newSections.map(s => s.id) });
    };
 
    const blockProps = useBlockProps({
        className: 'faq-block-editor',
    });
 
    return (
        <>
            <InspectorControls>
                <PanelBody title={__('FAQ Settings', 'jvb')} initialOpen={true}>
                    <ToggleControl
                        label={__('Show Section Titles', 'jvb')}
                        checked={showSectionTitles}
                        onChange={(value) => setAttributes({ showSectionTitles: value })}
                        help={__('Display section names as headings', 'jvb')}
                    />
                    <ToggleControl
                        label={__('Collapse by Default', 'jvb')}
                        checked={collapseByDefault}
                        onChange={(value) => setAttributes({ collapseByDefault: value })}
                        help={__('Questions start collapsed and expand on click', 'jvb')}
                    />
                </PanelBody>
 
                <PanelBody title={__('Section Order', 'jvb')} initialOpen={false}>
                    <p className="components-base-control__help">
                        {__('Use the arrow buttons to reorder sections', 'jvb')}
                    </p>
                    {sections.length > 0 ? (
                        <div className="faq-section-list">
                            {sections.map((section, index) => (
                                <div key={section.id} className="faq-section-item">
                                    <div className="faq-section-controls">
                                        <Button
                                            icon="arrow-up-alt2"
                                            label={__('Move up', 'jvb')}
                                            disabled={index === 0}
                                            onClick={() => moveSection(index, 'up')}
                                            className="faq-section-button"
                                        />
                                        <Button
                                            icon="arrow-down-alt2"
                                            label={__('Move down', 'jvb')}
                                            disabled={index === sections.length - 1}
                                            onClick={() => moveSection(index, 'down')}
                                            className="faq-section-button"
                                        />
                                    </div>
                                    <span className="faq-section-name">{section.name}</span>
                                </div>
                            ))}
                        </div>
                    ) : (
                        <Notice status="info" isDismissible={false}>
                            {__('No sections found. Create sections in the FAQ taxonomy.', 'jvb')}
                        </Notice>
                    )}
                </PanelBody>
            </InspectorControls>
 
            <div {...blockProps}>
                <div className="faq-block-preview">
                    <h3>{__('FAQ Block', 'jvb')}</h3>
                    <p>
                        {__('This block will display FAQs organized by sections.', 'jvb')}
                    </p>
                    {sections.length > 0 ? (
                        <div className="faq-sections-preview">
                            <strong>{__('Section Order:', 'jvb')}</strong>
                            <ol>
                                {sections.map((section) => (
                                    <li key={section.id}>{section.name}</li>
                                ))}
                            </ol>
                        </div>
                    ) : (
                        <Notice status="warning" isDismissible={false}>
                            {__('No sections available. Create sections in the FAQ taxonomy.', 'jvb')}
                        </Notice>
                    )}
                </div>
            </div>
        </>
    );
}