From 0be9b0b208d47b31971f6038087a830f88c44ed6 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Wed, 20 Nov 2024 11:44:09 +0530 Subject: [PATCH] update UI as per figma --- .../components/classifai-settings/index.js | 25 ++-- .../components/service-settings/index.js | 129 ++++++++++++++---- src/scss/settings.scss | 13 ++ 3 files changed, 132 insertions(+), 35 deletions(-) diff --git a/src/js/settings/components/classifai-settings/index.js b/src/js/settings/components/classifai-settings/index.js index 8cb0f1df7..24456fe18 100644 --- a/src/js/settings/components/classifai-settings/index.js +++ b/src/js/settings/components/classifai-settings/index.js @@ -8,13 +8,14 @@ import { HashRouter, useParams, NavLink, + useNavigate, } from 'react-router-dom'; /** * WordPress dependencies */ import { useDispatch } from '@wordpress/data'; -import { SlotFillProvider } from '@wordpress/components'; +import { SlotFillProvider, Button, Icon } from '@wordpress/components'; import { __, sprintf } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; import apiFetch from '@wordpress/api-fetch'; @@ -62,6 +63,7 @@ const DefaultFeatureSettings = () => { const FeatureSettingsWrapper = () => { const { service, feature } = useParams(); const serviceFeatures = Object.keys( features[ service ] || {} ); + const navigate = useNavigate(); if ( ! serviceFeatures.includes( feature ) ) { return ; @@ -69,6 +71,12 @@ const FeatureSettingsWrapper = () => { return ( + ); @@ -199,16 +207,11 @@ export const ClassifAISettings = () => { } - > - } - /> - } - /> - + /> + } + /> } diff --git a/src/js/settings/components/service-settings/index.js b/src/js/settings/components/service-settings/index.js index 1a321592d..a352e67d8 100644 --- a/src/js/settings/components/service-settings/index.js +++ b/src/js/settings/components/service-settings/index.js @@ -1,14 +1,24 @@ /** * External dependencies */ -import { NavLink, Outlet, useParams } from 'react-router-dom'; +import { NavLink, useParams } from 'react-router-dom'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useDispatch } from '@wordpress/data'; -import { useEffect } from '@wordpress/element'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { + Flex, + FlexItem, + FlexBlock, + ToggleControl, + Button, + Panel, + PanelBody, +} from '@wordpress/components'; +import { useEffect, useRef } from '@wordpress/element'; +import apiFetch from '@wordpress/api-fetch'; /** * Internal dependencies @@ -22,34 +32,105 @@ const { features } = window.classifAISettings; * @return {Object} The ServiceSettings component. */ export const ServiceSettings = () => { - const { setCurrentService } = useDispatch( STORE_NAME ); + const { + setCurrentService, + setIsSaving, + setSettings + } = useDispatch( STORE_NAME ); + const { service } = useParams(); + const isInitialPageLoad = useRef( true ); + + const { settings, getFeatureSettings } = useSelect( ( select ) => { + const store = select( STORE_NAME ); + + return { + settings: store.getSettings(), + getFeatureSettings: ( key, featureName ) => store.getFeatureSettings( key, featureName ), + } + } ); + useEffect( () => { setCurrentService( service ); }, [ service, setCurrentService ] ); const serviceFeatures = features[ service ] || {}; + + const saveSettings = () => { + setIsSaving( true ); + apiFetch( { + path: '/classifai/v1/settings/', + method: 'POST', + data: { + settings, + is_setup: true, + step: 'enable_features', + }, + } ).then( ( res ) => { + if ( res.errors && res.errors.length ) { + res.errors.forEach( ( error ) => { + createErrorNotice( error.message, { + id: `error-${ featureName }`, + } ); + } ); + setSettings( res.settings ); + setIsSaving( false ); + return; + } + + setSettings( res.settings ); + setIsSaving( false ); + } ) + }; + + const statuses = Object.keys( settings ).map( ( key ) => settings[ key ].status ).join( '' ); + + useEffect( () => { + if ( isInitialPageLoad.current ) { + isInitialPageLoad.current = false; + return; + } + saveSettings() + }, [ statuses ] ); + return ( -
-
- { Object.keys( serviceFeatures ).map( ( feature ) => ( - - isActive - ? 'active-tab classifai-tabs-item' - : 'classifai-tabs-item' - } - > - { serviceFeatures[ feature ]?.label || - __( 'Feature', 'classifai' ) } - - ) ) } -
-
- -
+
+ { Object.keys( serviceFeatures ).map( ( feature ) => ( + + + + + + wp.data.dispatch( STORE_NAME ).setFeatureSettings( { status: value ? '1' : '0' }, feature ) } + /> + + + + { serviceFeatures[ feature ]?.label } +
+ + + + + + + +

+ + + ) ) }
); }; diff --git a/src/scss/settings.scss b/src/scss/settings.scss index 6cba5f189..d46ca82f2 100644 --- a/src/scss/settings.scss +++ b/src/scss/settings.scss @@ -572,3 +572,16 @@ margin: 0 auto; top: 1rem; } + +.classifai-settings-dashboard { + margin-top: 36px; + max-width: 700px; +} + +.classifai-feature-status { + margin-bottom: 0 !important; + + .components-flex { + gap: 0 !important; + } +} \ No newline at end of file