From 1b2bac1644cc11513cb760815a4e79a17677c217 Mon Sep 17 00:00:00 2001 From: Diana Nanyanzi Date: Sun, 29 Sep 2024 04:10:21 +0300 Subject: [PATCH] refactor: replace multiple view components with one view + css modifications --- components/header-bar/src/apps.js | 691 +++++++++++++----------------- 1 file changed, 307 insertions(+), 384 deletions(-) diff --git a/components/header-bar/src/apps.js b/components/header-bar/src/apps.js index 95fd4ab99..72db8e9f3 100755 --- a/components/header-bar/src/apps.js +++ b/components/header-bar/src/apps.js @@ -8,12 +8,11 @@ import { IconArrowLeft16, } from '@dhis2/ui-icons' import { Button } from '@dhis2-ui/button' -import { Card } from '@dhis2-ui/card' import { InputField } from '@dhis2-ui/input' import { MenuItem } from '@dhis2-ui/menu' import { Modal, ModalContent } from '@dhis2-ui/modal' import PropTypes from 'prop-types' -import React, { useState, useCallback, useRef } from 'react' +import React, { useState, useCallback, useRef, useEffect } from 'react' import { joinPath } from './join-path.js' import i18n from './locales/index.js' @@ -35,18 +34,16 @@ function Search({ value, onChange }) { placeholder={i18n.t('Search apps, shortcuts, commands')} onChange={onChange} initialFocus + // style={{ + // width: '100%', + // border: 'none', + // }} /> @@ -58,7 +55,7 @@ Search.propTypes = { onChange: PropTypes.func.isRequired, } -function Item({ name, path, img }) { +function AppItem({ name, path, img }) { return ( app logo @@ -67,14 +64,11 @@ function Item({ name, path, img }) { ) } -AppItem.propTypes = { +ListItem.propTypes = { description: PropTypes.string, image: PropTypes.string, name: PropTypes.string, path: PropTypes.string, } -function BackToHomeButton({ setView }) { - const handleClick = () => { - setView('home') - } - return ( - - - {show ? ( - - ) : null} +
+ {filteredItems.map( + ( + { displayName, name, defaultAction, icon, description }, + idx + ) => ( + + ) + )}
) } - -CommandPalette.propTypes = { - apps: PropTypes.array, - commands: PropTypes.array, +List.propTypes = { + filteredItems: PropTypes.array, } -export const MenuModal = ({ show, apps, commands, filter, onFilterChange }) => { - console.log(apps, 'apps') - const [currentView, setCurrentView] = useState('home') - const showActions = filter.length <= 0 && currentView === 'home' - - return ( -
- {show && ( - - -
- - - - {showActions ? ( - - ) : null} - -
-
-
- )} -
- ) -} +function Actions({ setView }) { + const actions = [ + { + icon: IconApps16, + type: 'apps', + action: 'Browse apps', + }, + { + icon: IconTerminalWindow16, + type: 'commands', + action: 'Browse commands', + }, + ] -MenuModal.propTypes = { - apps: PropTypes.array, - commands: PropTypes.array, - filter: PropTypes.string, - show: PropTypes.bool, - onFilterChange: PropTypes.func, -} + const { baseUrl } = useConfig() -function ViewSwitcher({ apps, commands, filter, view, setView }) { - switch (view) { - case 'apps': - return - case 'commands': - return ( - +
{i18n.t('Actions')}
+ {actions.map((action, index) => ( + { + console.log(payload.value, event.target) + setView(action.type) + }} + label={i18n.t(`${action.action}`)} + value={action.action} + icon={} /> - ) - case 'home': - default: - return - } + ))} + {/* got from profile-menu: https://github.com/dhis2/ui/blob/4902126ef0a6163961286a29f8df44b8fd3a0604/components/header-bar/src/profile-menu/profile-menu.js#L88 */} + { + // setLoading(true) + await clearSensitiveCaches() + // setLoading(false) + window.location.assign( + joinPath( + baseUrl, + 'dhis-web-commons-security/logout.action' + ) + ) + }} + label={i18n.t('Logout')} + value="logout" + icon={} + /> + + ) } -ViewSwitcher.propTypes = { - apps: PropTypes.array, - commands: PropTypes.array, - filter: PropTypes.string, +Actions.propTypes = { setView: PropTypes.func, - view: PropTypes.string, } -function AllAppsView({ apps, filter, setView }) { - const filteredApps = apps.filter(({ displayName, name }) => { - const appName = displayName || name - const formattedAppName = appName.toLowerCase() - const formattedFilter = escapeRegExpCharacters(filter).toLowerCase() - - return filter.length > 0 - ? formattedAppName.match(formattedFilter) - : true - }) - +function SearchResults({ filter, filteredItems, heading }) { return ( - <> - -
- {filter ? ( - filteredApps.length > 0 ? ( -

- Results for {filter} -

- ) : ( -

- Nothing found for {filter} -

- ) +
+ {filter ? ( + filteredItems.length > 0 ? ( +

+ Results for {filter} +

) : ( -
- {i18n.t('All Apps')} -
- )} -
- - +

+ Nothing found for {filter} +

+ ) + ) : ( +
+ {i18n.t(`${heading}`)} +
+ )} +
) } -AllAppsView.propTypes = { - apps: PropTypes.array, +SearchResults.propTypes = { filter: PropTypes.string, - setView: PropTypes.func, + filteredItems: PropTypes.array, + heading: PropTypes.string, } -function CommandsView({ commands, filter, setView }) { - const filteredCommands = commands.filter(({ displayName, name }) => { - const commandName = displayName || name - const formattedAppName = commandName.toLowerCase() +function ActionsView({ heading, itemsArray, filter, setView }) { + const filteredItems = itemsArray.filter(({ displayName, name }) => { + const itemName = displayName || name + const formattedItemName = itemName.toLowerCase() const formattedFilter = escapeRegExpCharacters(filter).toLowerCase() return filter.length > 0 - ? formattedAppName.match(formattedFilter) + ? formattedItemName.match(formattedFilter) : true }) + const handleClick = () => { + setView('home') + } return ( <> - -
- {filter ? ( - filteredCommands.length > 0 ? ( - Results for {filter} - ) : ( - Nothing found for {filter} - ) - ) : ( -
- {i18n.t('All Commands')} -
- )} -
- {filteredCommands.map( - ( - { displayName, name, defaultAction, icon, description }, - idx - ) => ( - - ) - )} + + + {show ? ( + + +
+ + + {showActions ? ( + + ) : null} +
+
+
+ ) : null} + + + ) } -Actions.propTypes = { - setView: PropTypes.func, +CommandPalette.propTypes = { + apps: PropTypes.array, + commands: PropTypes.array, } export default CommandPalette