From 92eadc3ca3ba552c4093e5cbe27a1e184f00343b Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Sat, 26 Oct 2024 19:22:34 +0300 Subject: [PATCH] dev: source setting screens from global --- packages/admin/components/fields/index.tsx | 4 +- .../components/layout/header/menu/index.tsx | 70 +++++++++++-------- packages/admin/components/screen/context.tsx | 13 ++-- packages/admin/components/screen/screen.tsx | 58 ++++----------- .../components/screen/setting-screen.tsx | 10 +-- packages/admin/components/screen/utils.ts | 47 +++++++++++++ packages/admin/contexts/settings-context.tsx | 3 +- packages/admin/types.d.ts | 17 +++-- src/Admin/Settings/AbstractSettings.php | 18 ++++- src/Admin/Settings/AccessControlSettings.php | 7 ++ src/Admin/Settings/PluginSettings.php | 7 ++ 11 files changed, 154 insertions(+), 100 deletions(-) create mode 100644 packages/admin/components/screen/utils.ts diff --git a/packages/admin/components/fields/index.tsx b/packages/admin/components/fields/index.tsx index 60ccb6ca..8a8da827 100644 --- a/packages/admin/components/fields/index.tsx +++ b/packages/admin/components/fields/index.tsx @@ -1,4 +1,4 @@ -import type { AllowedSettingKeys, SettingSchema } from '@/admin/types'; +import type { FieldSchema } from '@/admin/types'; import { Field } from './field'; export const Fields = ( { @@ -9,7 +9,7 @@ export const Fields = ( { }: { excludedProperties?: string[]; values: Record< string, unknown > | undefined; - fields: SettingSchema[ AllowedSettingKeys ]; + fields: Record< string, FieldSchema >; setValue: ( values: Record< string, unknown > ) => void; } ) => { if ( ! values ) { diff --git a/packages/admin/components/layout/header/menu/index.tsx b/packages/admin/components/layout/header/menu/index.tsx index d72a85d8..dfd02277 100644 --- a/packages/admin/components/layout/header/menu/index.tsx +++ b/packages/admin/components/layout/header/menu/index.tsx @@ -2,7 +2,7 @@ import { Button, Icon, NavigableMenu } from '@wordpress/components'; import { link as LinkSVG } from '@wordpress/icons'; import { __ } from '@wordpress/i18n'; import { useCurrentScreen } from '@/admin/components/screen/context'; -import type { AllowedScreens } from '@/admin/components/screen/screen'; +import { getScreenForSetting } from '@/admin/components/screen/utils'; import styles from './styles.module.scss'; @@ -13,43 +13,57 @@ const LinkIcon = () => { return ; }; -const SCREEN_LABELS: Record< AllowedScreens, string > = { - providers: __( 'Login Providers', 'wp-graphql-headless-login' ), - 'access-control': __( 'Access Control', 'wp-graphql-headless-login' ), - 'plugin-settings': __( 'Misc', 'wp-graphql-headless-login' ), +/** + * Builds and returns the menu object from the wpGraphQLLogin.settings global. + */ +export const getMenuObject = (): Record< string, string > => { + const settings = wpGraphQLLogin.settings; + + const menu: Record< string, string > = { + providers: '', // We want this as the first key. + }; + + for ( const key in settings ) { + // @todo get providers from global after the refactor. + const menuTitle = + settings[ key ].label || + __( 'Providers', 'wp-graphql-headless-login' ); + const screen = getScreenForSetting( key ); + + menu[ screen ] = menuTitle; + } + + return menu; }; export const Menu = () => { const { currentScreen, setCurrentScreen } = useCurrentScreen(); + // Build the menu object of screens and labels from the wpGraphQLLogin?.settings. + const menuItems = getMenuObject(); + return (
    { // Loop through the screen titles and create a button for each one. - Object.entries( SCREEN_LABELS ).map( - ( [ screen, title ] ) => ( -
  • - -
  • - ) - ) + Object.entries( menuItems ).map( ( [ screen, title ] ) => ( +
  • + +
  • + ) ) }