Skip to content

Commit

Permalink
feat: create new namespace functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rita committed Dec 19, 2024
1 parent b0d7646 commit 35dd3b0
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/components/sections/NamespaceDataSection.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useDataQuery } from '@dhis2/app-runtime'
import { useDataEngine, useDataQuery } from '@dhis2/app-runtime'
import { IconAdd24, colors } from '@dhis2/ui'
import React from 'react'
import React, { useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import classes from '../../App.module.css'
import i18n from '../../locales'
import ErrorNotice from '../error/ErrorNotice'
import CenteredLoader from '../loader/Loader'
import CreateModal from '../modals/CreateModal'
import { KeysField, NamespaceField } from '../modals/Fields'
import ItemsTable from '../table/ItemsTable'
import CreateButton from './CreateButton'
import SearchField from './SearchField'
Expand All @@ -14,7 +17,22 @@ interface QueryResults {
}

const NamespaceDataSection = ({ query }) => {
const { error, loading, data } = useDataQuery<QueryResults>(query)
const engine = useDataEngine()
const navigate = useNavigate()
const { store } = useParams()
const [openModal, setOpenModal] = useState(false)

const { error, loading, data, refetch } = useDataQuery<QueryResults>(query)

const handleCreate = async (values) => {
await engine.mutate({
type: 'create',
resource: `${store}/${values?.namespace}/${values?.key}`,
data: () => ({}),
})
refetch()
navigate(`edit/${values?.namespace}`)
}

if (error) {
return <ErrorNotice />
Expand All @@ -30,13 +48,23 @@ const NamespaceDataSection = ({ query }) => {
<SearchField placeholder={i18n.t('Search namespaces')} />
<CreateButton
label={i18n.t('New Namespace')}
handleClick={() => console.log('create new namespace')}
handleClick={() => setOpenModal(true)}
icon={<IconAdd24 color={colors.grey600} />}
/>
</div>
<div>
{data && <ItemsTable data={data} label={i18n.t('Namespace')} />}
</div>
{openModal && (
<CreateModal
title={i18n.t('Add New Namespace')}
closeModal={() => setOpenModal(false)}
handleCreate={handleCreate}
>
<NamespaceField initialFocus />
<KeysField />
</CreateModal>
)}
</>
)
}
Expand Down

0 comments on commit 35dd3b0

Please sign in to comment.