diff --git a/src/components/panel/modals/CreateModal.tsx b/src/components/panel/modals/CreateModal.tsx new file mode 100644 index 0000000..f908fb2 --- /dev/null +++ b/src/components/panel/modals/CreateModal.tsx @@ -0,0 +1,79 @@ +import { + Modal, + ModalTitle, + ModalContent, + ModalActions, + Button, + ButtonStrip, + InputField, +} from '@dhis2/ui' +import React from 'react' +import i18n from '../../../locales' +import { CreateFieldValues } from '../sidepanel/SidePanel' + +type CreateModalProps = { + createFn: (values) => void, + values: CreateFieldValues, + setValues: (values) => void, + closeModal: () => void, + title: string, + type: string, + buttonLabel: string +} + +const CreateModal = ({ + createFn, + values, + setValues, + closeModal, + title, + type, + buttonLabel +}: CreateModalProps) => { + return ( + + + {title} + + + {type === 'namespace' && ( + { + setValues({ + ...values, + ['namespace']: value, + }) + }} + /> + )} + { + setValues({ + ...values, + ['key']: value, + }) + }} + /> + + + + + + + + + ) +} +export default CreateModal diff --git a/src/components/panel/sidepanel/CreateButton.tsx b/src/components/panel/sidepanel/CreateButton.tsx index 03ffbbb..67cf2e9 100644 --- a/src/components/panel/sidepanel/CreateButton.tsx +++ b/src/components/panel/sidepanel/CreateButton.tsx @@ -4,19 +4,17 @@ import React from 'react' import i18n from '../../../locales' import classes from '../Panel.module.css' -const CreateButton = () => { +const CreateButton = ({ label, handleClick }) => { return (
) diff --git a/src/components/panel/sidepanel/SidePanel.tsx b/src/components/panel/sidepanel/SidePanel.tsx index f499d7c..f41221d 100644 --- a/src/components/panel/sidepanel/SidePanel.tsx +++ b/src/components/panel/sidepanel/SidePanel.tsx @@ -1,9 +1,13 @@ -import React from 'react' +import React, { useState } from 'react' import { ErrorResponse } from '../error/ErrorComponent' import classes from '../Panel.module.css' import CreateButton from './CreateButton' import PanelLinksList from './PanelLinksList' import PanelSearchField from './SearchField' +import i18n from '../../../locales' +import CreateModal from '../modals/CreateModal' +import { useDataEngine } from '@dhis2/app-service-data' +import { useNavigate, useParams } from 'react-router' type SidePanelProps = { data: { results: [] } @@ -13,6 +17,11 @@ type SidePanelProps = { type: string } +export type CreateFieldValues = { + namespace?: string, + key?: string +} + const SidePanel = ({ data, error, @@ -20,11 +29,66 @@ const SidePanel = ({ refetchList, type, }: SidePanelProps) => { - console.log(data, type, 'sidebar panel') + const engine = useDataEngine() + const navigate = useNavigate() + const { store, namespace: currentNamespace } = useParams() + const [openModal, setOpenModal] = useState(false) + const [values, setValues] = useState({}) + + // const { showSuccess, showError } = useCustomAlert() + + const handleCreate = async (values: CreateFieldValues) => { + let resource = '' + if (type === 'namespace') { + resource = `${store}/${values?.namespace}/${values?.key}` + } else { + resource = `${store}/${currentNamespace}/${values?.key}` + } + + await engine.mutate( + { + type: 'create', + resource, + data: () => ({}), + }, + { + onComplete: () => { + let url = '' + if (type === 'namespace') { + url = `${store}/edit/${values?.namespace}/${values?.key}` + } else { + url = `${values?.key}` + } + const message = i18n.t('Key created successfully') + // showSuccess(message) + navigate(url) + setValues({}) + }, + onError: () => { + const message = i18n.t( + 'There was an error creating the key' + ) + // showError(message) + }, + } + ) + setOpenModal(false) + } + + const derivedModalProps = { + title: type === "namespace" ? i18n.t("Add New Namespace") : i18n.t("Add New Key"), + buttonLabel: type === "namespace" ? i18n.t("Add Namespace") : i18n.t("Add Key"), + } + const createButtonLabel = type === "namespace" ? i18n.t("New namespace") : i18n.t("New key"); + return ( + <>
- + setOpenModal(true)} + />
+ {openModal && ( + setOpenModal(false)} + type={type} + {...derivedModalProps} + /> + )} + ) }