Phase 2: Schema-Based Form System
- Add ModuleSettingsController with GET/POST/schema endpoints
- Create SchemaField component supporting 8 field types (text, textarea, email, url, number, toggle, checkbox, select)
- Create SchemaForm component for automatic form generation from schema
- Add ModuleSettings page with dynamic routing (/settings/modules/:moduleId)
- Add useModuleSettings React hook for settings management
- Implement NewsletterSettings as example with 8 configurable fields
- Add has_settings flag to module registry
- Settings stored as woonoow_module_{module_id}_settings
Phase 3: Advanced Features
- Create windowAPI.ts exposing React, hooks, components, icons, utils to addons via window.WooNooW
- Add DynamicComponentLoader for loading external React components
- Create TypeScript definitions (woonoow-addon.d.ts) for addon developers
- Initialize Window API in App.tsx on mount
- Enable custom React components for addon settings pages
Phase 4: Production Polish & Example
- Create complete Biteship addon example demonstrating both approaches:
* Schema-based settings (no build required)
* Custom React component (with build)
- Add comprehensive README with installation and testing guide
- Include package.json with esbuild configuration
- Demonstrate window.WooNooW API usage in custom component
Bug Fixes:
- Fix footer newsletter form visibility (remove redundant module check)
- Fix footer contact_data and social_links not saving (parameter name mismatch: snake_case vs camelCase)
- Fix useModules hook returning undefined (remove .data wrapper, add fallback)
- Add optional chaining to footer settings rendering
- Fix TypeScript errors in woonoow-addon.d.ts (use any for external types)
Files Added (15):
- includes/Api/ModuleSettingsController.php
- includes/Modules/NewsletterSettings.php
- admin-spa/src/components/forms/SchemaField.tsx
- admin-spa/src/components/forms/SchemaForm.tsx
- admin-spa/src/routes/Settings/ModuleSettings.tsx
- admin-spa/src/hooks/useModuleSettings.ts
- admin-spa/src/lib/windowAPI.ts
- admin-spa/src/components/DynamicComponentLoader.tsx
- types/woonoow-addon.d.ts
- examples/biteship-addon/biteship-addon.php
- examples/biteship-addon/src/Settings.jsx
- examples/biteship-addon/package.json
- examples/biteship-addon/README.md
- PHASE_2_3_4_SUMMARY.md
Files Modified (11):
- admin-spa/src/App.tsx
- admin-spa/src/hooks/useModules.ts
- admin-spa/src/routes/Appearance/Footer.tsx
- admin-spa/src/routes/Settings/Modules.tsx
- customer-spa/src/hooks/useModules.ts
- customer-spa/src/layouts/BaseLayout.tsx
- customer-spa/src/components/NewsletterForm.tsx
- includes/Api/Routes.php
- includes/Api/ModulesController.php
- includes/Core/ModuleRegistry.php
- woonoow.php
API Endpoints Added:
- GET /woonoow/v1/modules/{module_id}/settings
- POST /woonoow/v1/modules/{module_id}/settings
- GET /woonoow/v1/modules/{module_id}/schema
For Addon Developers:
- Schema-based: Define settings via woonoow/module_settings_schema filter
- Custom React: Build component using window.WooNooW API, externalize react/react-dom
- Both approaches use same storage and retrieval methods
- TypeScript definitions provided for type safety
- Complete working example (Biteship) included
203 lines
7.6 KiB
JavaScript
203 lines
7.6 KiB
JavaScript
/**
|
|
* Biteship Custom Settings Component
|
|
*
|
|
* This demonstrates how to create a custom React settings page for a WooNooW addon
|
|
* using the exposed window.WooNooW API
|
|
*/
|
|
|
|
// Access WooNooW API from window
|
|
const { React, hooks, components, icons, utils } = window.WooNooW;
|
|
const { useModuleSettings } = hooks;
|
|
const { SettingsLayout, SettingsCard, Input, Button, Switch, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Badge } = components;
|
|
const { Settings: SettingsIcon, Save, AlertCircle, Check } = icons;
|
|
const { toast } = utils;
|
|
|
|
function BiteshipSettings() {
|
|
const { settings, isLoading, updateSettings } = useModuleSettings('biteship-shipping');
|
|
const [formData, setFormData] = React.useState({});
|
|
const [testingConnection, setTestingConnection] = React.useState(false);
|
|
const [connectionStatus, setConnectionStatus] = React.useState(null);
|
|
|
|
// Initialize form data from settings
|
|
React.useEffect(() => {
|
|
if (settings) {
|
|
setFormData(settings);
|
|
}
|
|
}, [settings]);
|
|
|
|
const handleChange = (key, value) => {
|
|
setFormData(prev => ({ ...prev, [key]: value }));
|
|
};
|
|
|
|
const handleSave = () => {
|
|
updateSettings.mutate(formData);
|
|
};
|
|
|
|
const testConnection = async () => {
|
|
if (!formData.api_key) {
|
|
toast.error('Please enter an API key first');
|
|
return;
|
|
}
|
|
|
|
setTestingConnection(true);
|
|
setConnectionStatus(null);
|
|
|
|
// Simulate API test (in real addon, call Biteship API)
|
|
setTimeout(() => {
|
|
const isValid = formData.api_key.startsWith('biteship_');
|
|
setConnectionStatus(isValid ? 'success' : 'error');
|
|
setTestingConnection(false);
|
|
|
|
if (isValid) {
|
|
toast.success('Connection successful!');
|
|
} else {
|
|
toast.error('Invalid API key format');
|
|
}
|
|
}, 1500);
|
|
};
|
|
|
|
if (isLoading) {
|
|
return React.createElement(SettingsLayout, { title: 'Biteship Settings', isLoading: true });
|
|
}
|
|
|
|
return React.createElement(SettingsLayout, {
|
|
title: 'Biteship Shipping Settings',
|
|
description: 'Configure your Biteship integration for Indonesian shipping'
|
|
},
|
|
// API Configuration Card
|
|
React.createElement(SettingsCard, {
|
|
title: 'API Configuration',
|
|
description: 'Connect your Biteship account'
|
|
},
|
|
React.createElement('div', { className: 'space-y-4' },
|
|
// API Key
|
|
React.createElement('div', { className: 'space-y-2' },
|
|
React.createElement('label', { className: 'text-sm font-medium' }, 'API Key'),
|
|
React.createElement('div', { className: 'flex gap-2' },
|
|
React.createElement(Input, {
|
|
type: 'password',
|
|
value: formData.api_key || '',
|
|
onChange: (e) => handleChange('api_key', e.target.value),
|
|
placeholder: 'biteship_xxxxxxxxxxxxx'
|
|
}),
|
|
React.createElement(Button, {
|
|
variant: 'outline',
|
|
onClick: testConnection,
|
|
disabled: testingConnection
|
|
}, testingConnection ? 'Testing...' : 'Test Connection')
|
|
),
|
|
connectionStatus && React.createElement('div', {
|
|
className: `flex items-center gap-2 text-sm ${connectionStatus === 'success' ? 'text-green-600' : 'text-red-600'}`
|
|
},
|
|
React.createElement(connectionStatus === 'success' ? Check : AlertCircle, { className: 'h-4 w-4' }),
|
|
connectionStatus === 'success' ? 'Connection successful' : 'Connection failed'
|
|
)
|
|
),
|
|
|
|
// Environment
|
|
React.createElement('div', { className: 'space-y-2' },
|
|
React.createElement('label', { className: 'text-sm font-medium' }, 'Environment'),
|
|
React.createElement(Select, {
|
|
value: formData.environment || 'test',
|
|
onValueChange: (value) => handleChange('environment', value)
|
|
},
|
|
React.createElement(SelectTrigger, null,
|
|
React.createElement(SelectValue, null)
|
|
),
|
|
React.createElement(SelectContent, null,
|
|
React.createElement(SelectItem, { value: 'test' }, 'Test Mode'),
|
|
React.createElement(SelectItem, { value: 'production' }, 'Production')
|
|
)
|
|
),
|
|
React.createElement('p', { className: 'text-xs text-muted-foreground' },
|
|
'Use test mode for development and testing'
|
|
)
|
|
)
|
|
)
|
|
),
|
|
|
|
// Origin Location Card
|
|
React.createElement(SettingsCard, {
|
|
title: 'Origin Location',
|
|
description: 'Your warehouse or pickup location'
|
|
},
|
|
React.createElement('div', { className: 'grid grid-cols-2 gap-4' },
|
|
React.createElement('div', { className: 'space-y-2' },
|
|
React.createElement('label', { className: 'text-sm font-medium' }, 'Latitude'),
|
|
React.createElement(Input, {
|
|
value: formData.origin_lat || '',
|
|
onChange: (e) => handleChange('origin_lat', e.target.value),
|
|
placeholder: '-6.200000'
|
|
})
|
|
),
|
|
React.createElement('div', { className: 'space-y-2' },
|
|
React.createElement('label', { className: 'text-sm font-medium' }, 'Longitude'),
|
|
React.createElement(Input, {
|
|
value: formData.origin_lng || '',
|
|
onChange: (e) => handleChange('origin_lng', e.target.value),
|
|
placeholder: '106.816666'
|
|
})
|
|
)
|
|
)
|
|
),
|
|
|
|
// Features Card
|
|
React.createElement(SettingsCard, {
|
|
title: 'Features',
|
|
description: 'Enable or disable shipping features'
|
|
},
|
|
React.createElement('div', { className: 'space-y-4' },
|
|
// COD
|
|
React.createElement('div', { className: 'flex items-center justify-between' },
|
|
React.createElement('div', null,
|
|
React.createElement('p', { className: 'font-medium' }, 'Cash on Delivery'),
|
|
React.createElement('p', { className: 'text-sm text-muted-foreground' }, 'Allow customers to pay on delivery')
|
|
),
|
|
React.createElement(Switch, {
|
|
checked: formData.enable_cod || false,
|
|
onCheckedChange: (checked) => handleChange('enable_cod', checked)
|
|
})
|
|
),
|
|
|
|
// Insurance
|
|
React.createElement('div', { className: 'flex items-center justify-between' },
|
|
React.createElement('div', null,
|
|
React.createElement('p', { className: 'font-medium' }, 'Shipping Insurance'),
|
|
React.createElement('p', { className: 'text-sm text-muted-foreground' }, 'Automatically add insurance to shipments')
|
|
),
|
|
React.createElement(Switch, {
|
|
checked: formData.enable_insurance !== false,
|
|
onCheckedChange: (checked) => handleChange('enable_insurance', checked)
|
|
})
|
|
),
|
|
|
|
// Debug Mode
|
|
React.createElement('div', { className: 'flex items-center justify-between' },
|
|
React.createElement('div', null,
|
|
React.createElement('p', { className: 'font-medium' }, 'Debug Mode'),
|
|
React.createElement('p', { className: 'text-sm text-muted-foreground' }, 'Log API requests for troubleshooting')
|
|
),
|
|
React.createElement(Switch, {
|
|
checked: formData.debug_mode || false,
|
|
onCheckedChange: (checked) => handleChange('debug_mode', checked)
|
|
})
|
|
)
|
|
)
|
|
),
|
|
|
|
// Save Button
|
|
React.createElement('div', { className: 'flex justify-end' },
|
|
React.createElement(Button, {
|
|
onClick: handleSave,
|
|
disabled: updateSettings.isPending
|
|
},
|
|
React.createElement(Save, { className: 'mr-2 h-4 w-4' }),
|
|
updateSettings.isPending ? 'Saving...' : 'Save Settings'
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
// Export component to global scope for WooNooW to load
|
|
window.WooNooWAddon_biteship_shipping = BiteshipSettings;
|