Skip to content

Commit

Permalink
feat: delete key functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rita committed Dec 19, 2024
1 parent 9556893 commit 684c713
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
19 changes: 14 additions & 5 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-12-19T16:36:13.574Z\n"
"PO-Revision-Date: 2024-12-19T16:36:13.575Z\n"
"POT-Creation-Date: 2024-12-19T16:57:20.491Z\n"
"PO-Revision-Date: 2024-12-19T16:57:20.492Z\n"

msgid "An error has occurred"
msgstr "An error has occurred"
Expand All @@ -26,6 +26,9 @@ msgstr "Cancel"
msgid "Add"
msgstr "Add"

msgid "Delete"
msgstr "Delete"

msgid "Key"
msgstr "Key"

Expand Down Expand Up @@ -68,6 +71,9 @@ msgstr "Search keys"
msgid "Add New Key"
msgstr "Add New Key"

msgid "Delete Key"
msgstr "Delete Key"

msgid "Search namespaces"
msgstr "Search namespaces"

Expand All @@ -77,12 +83,15 @@ msgstr "New Namespace"
msgid "Add New Namespace"
msgstr "Add New Namespace"

msgid "Delete Namespace"
msgstr "Delete Namespace"

msgid "This will delete all the keys in this namespace"
msgstr "This will delete all the keys in this namespace"

msgid "Search"
msgstr "Search"

msgid "Delete"
msgstr "Delete"

msgid "Actions"
msgstr "Actions"

Expand Down
71 changes: 69 additions & 2 deletions src/components/sections/KeysDataSection.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useDataEngine, useDataQuery } from '@dhis2/app-runtime'
import { IconAdd16, colors } from '@dhis2/ui'
import React, { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import { useNavigate, 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 DeleteModal from '../modals/DeleteModal'
import { KeysField } from '../modals/Fields'
import ItemsTable from '../table/ItemsTable'
import CreateButton from './CreateButton'
Expand All @@ -19,9 +20,12 @@ interface QueryResults {

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

const [openModal, setOpenModal] = useState(false)
const [openDeleteModal, setOpenDeleteModal] = useState(false)
const [selectedKey, setSelectedKey] = useState(null)

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

const numberOfKeysInNamespace = data?.results?.length

const handleCreate = async ({ key }) => {
await engine.mutate(
{
Expand All @@ -46,6 +52,42 @@ const KeysDataSection = ({ query }) => {
refetch({ id: currentNamespace })
}

const handleDelete = async () => {
let resource = `${store}`

if (numberOfKeysInNamespace > 1) {
resource = `${resource}/${currentNamespace}`
await engine.mutate(
{
type: 'delete' as const,
resource: resource,
id: selectedKey,
},
{
onComplete: () => {
setOpenDeleteModal(false)
refetch({ id: currentNamespace })
navigate(`/${store}/edit/${currentNamespace}`)
},
}
)
} else {
await engine.mutate(
{
type: 'delete' as const,
resource: resource,
id: currentNamespace,
},
{
onComplete: () => {
setOpenDeleteModal(false)
navigate(`/${store}`)
},
}
)
}
}

useEffect(() => {
refetch({ id: currentNamespace })
}, [currentNamespace])
Expand Down Expand Up @@ -74,7 +116,14 @@ const KeysDataSection = ({ query }) => {
<SearchField placeholder={i18n.t('Search keys')} />
</div>
<div>
{data && <ItemsTable data={data} label={i18n.t('Key')} />}
{data && (
<ItemsTable
data={data}
label={i18n.t('Key')}
setOpenDeleteModal={setOpenDeleteModal}
setSelectedItem={setSelectedKey}
/>
)}
</div>
{openModal && (
<CreateModal
Expand All @@ -85,6 +134,24 @@ const KeysDataSection = ({ query }) => {
<KeysField initialFocus />
</CreateModal>
)}
{openDeleteModal && (
<DeleteModal
closeModal={() => setOpenDeleteModal(false)}
handleDelete={handleDelete}
title={i18n.t('Delete Key')}
>
{i18n.t(
`Are you sure you want to delete '${selectedKey}' in ${currentNamespace}?`
)}
{numberOfKeysInNamespace < 2 && (
<p>
{i18n.t(
`This will also delete the namespace '${currentNamespace}'`
)}
</p>
)}
</DeleteModal>
)}
</>
)
}
Expand Down
1 change: 1 addition & 0 deletions src/components/table/ItemsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const ItemsTable = ({
const handleDeleteBtnClick = (item) => {
setOpenDeleteModal(true)
setSelectedItem(item)
setActiveRow(item)
}

return (
Expand Down

0 comments on commit 684c713

Please sign in to comment.