Skip to content

Commit

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

const KeysDataSection = ({ query }) => {
const { namespace: currentNamespace } = useParams()
const engine = useDataEngine()
const { store, namespace: currentNamespace } = useParams()

const [openModal, setOpenModal] = useState(false)

const { error, loading, data, refetch } = useDataQuery<QueryResults>(
query,
Expand All @@ -27,6 +32,20 @@ const KeysDataSection = ({ query }) => {
}
)

const handleCreate = async ({ key }) => {
await engine.mutate(
{
type: 'create',
resource: `${store}/${currentNamespace}/${key}`,
data: () => ({}),
},
{
onComplete: () => setOpenModal(false),
}
)
refetch({ id: currentNamespace })
}

useEffect(() => {
refetch({ id: currentNamespace })
}, [currentNamespace])
Expand All @@ -47,7 +66,7 @@ const KeysDataSection = ({ query }) => {
</span>
<CreateButton
label={i18n.t('New Key')}
handleClick={() => console.log('create new key')}
handleClick={() => setOpenModal(true)}
icon={<IconAdd16 color={colors.grey600} />}
/>
</PanelHeader>
Expand All @@ -57,6 +76,15 @@ const KeysDataSection = ({ query }) => {
<div>
{data && <ItemsTable data={data} label={i18n.t('Key')} />}
</div>
{openModal && (
<CreateModal
title={i18n.t('Add New Key')}
closeModal={() => setOpenModal(false)}
handleCreate={handleCreate}
>
<KeysField initialFocus />
</CreateModal>
)}
</>
)
}
Expand Down

0 comments on commit 4577d52

Please sign in to comment.