Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update key values #141

Open
wants to merge 4 commits into
base: DHIS2-18662/delete-namespaces-and-keys
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 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-19T22:29:04.401Z\n"
"PO-Revision-Date: 2024-12-19T22:29:04.402Z\n"
"POT-Creation-Date: 2024-12-19T22:30:05.381Z\n"
"PO-Revision-Date: 2024-12-19T22:30:05.382Z\n"

msgid "An error has occurred"
msgstr "An error has occurred"
Expand Down Expand Up @@ -41,15 +41,6 @@ msgstr "Back to all namespaces"
msgid "Configure Namespaces"
msgstr "Configure Namespaces"

msgid "Close"
msgstr "Close"

msgid "Save"
msgstr "Save"

msgid "Save changes"
msgstr "Save changes"

msgid "Choose a key to start editing"
msgstr "Choose a key to start editing"

Expand All @@ -59,6 +50,33 @@ msgstr "DataStore"
msgid "UserDataStore"
msgstr "UserDataStore"

msgid "There was a problem fetching this data - {{error}}"
msgstr "There was a problem fetching this data - {{error}}"

msgid "Do not save these changes?"
msgstr "Do not save these changes?"

msgid "Confirm"
msgstr "Confirm"

msgid "Key '{{key}}' updated successfully"
msgstr "Key '{{key}}' updated successfully"

msgid "There was a problem updating the key - {{error}}"
msgstr "There was a problem updating the key - {{error}}"

msgid "There was a problem - {{error}}"
msgstr "There was a problem - {{error}}"

msgid "Close"
msgstr "Close"

msgid "Save"
msgstr "Save"

msgid "Save changes"
msgstr "Save changes"

msgid "Key '{{key}}' added successfully"
msgstr "Key '{{key}}' added successfully"

Expand Down
42 changes: 5 additions & 37 deletions src/components/panels/EditorPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Button } from '@dhis2/ui'
import React from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import classes from '../../App.module.css'
import i18n from '../../locales'
import PanelHeader from '../header/PanelHeader'
import EditorSection from '../sections/EditorSection'
import { useParams } from 'react-router-dom'
import EditSection from '../sections/EditSection'

const DataStoreKeyValuesQuery = {
results: {
Expand All @@ -23,43 +19,15 @@ const UserDataStoreKeyValuesQuery = {
}

const EditorPanel = () => {
const { key, namespace, store } = useParams()
const navigate = useNavigate()
const { store } = useParams()

return (
<div>
<PanelHeader>
<span className={classes.editorPanelHeader}>{key}</span>
<div className={classes.editButtons}>
<Button
small
aria-label={i18n.t('Close')}
name="close"
onClick={() => {
console.log('deselect key and cancel mutation?')
navigate(`/${store}/edit/${namespace}`)
}}
title={i18n.t('Close')}
>
{i18n.t('Close')}
</Button>
<Button
small
aria-label={i18n.t('Save')}
name="save"
onClick={() => console.log('save changes')}
title={i18n.t('Save')}
primary
>
{i18n.t('Save changes')}
</Button>
</div>
</PanelHeader>
{store === 'dataStore' && (
<EditorSection query={DataStoreKeyValuesQuery} />
<EditSection query={DataStoreKeyValuesQuery} />
)}
{store === 'userDataStore' && (
<EditorSection query={UserDataStoreKeyValuesQuery} />
<EditSection query={UserDataStoreKeyValuesQuery} />
)}
</div>
)
Expand Down
169 changes: 169 additions & 0 deletions src/components/sections/EditSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { useAlert, useDataEngine, useDataQuery } from '@dhis2/app-runtime'
import { Button } from '@dhis2/ui'
import React, { useEffect, useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import classes from '../../App.module.css'
import useCustomAlert from '../../hooks/useCustomAlert'
import i18n from '../../locales'
import PanelHeader from '../header/PanelHeader'
import Editor from './Editor'

const EditSection = ({ query }) => {
const { key, namespace, store } = useParams()
const engine = useDataEngine()
const navigate = useNavigate()

const { showError, showSuccess } = useCustomAlert()

const [editError, setEditError] = useState(null)
const [updateLoading, setUpdateLoading] = useState(false)

const { data, loading, refetch } = useDataQuery(query, {
variables: {
key,
namespace,
},
onError(error) {
showError(
i18n.t('There was a problem fetching this data - {{error}}', {
error: error.message,
interpolation: { escapeValue: false },
})
)
},
})

const [value, setValue] = useState(
JSON.stringify(data?.results, null, 4) || ''
)

const handleEditorChange = (value) => {
setValue(value)
}

const closeEditorAlert = useAlert(i18n.t('Do not save these changes?'), {
warning: true,
actions: [
{
label: i18n.t('Confirm'),
onClick: () => navigate(`/${store}/edit/${namespace}`),
},
{
label: i18n.t('Cancel'),
onClick: () => closeEditorAlert.hide(),
},
],
})

const handleClose = () => {
if (JSON.stringify(data?.results, null, 4) === value) {
navigate(`/${store}/edit/${namespace}`)
} else {
closeEditorAlert.show()
}
}

const handleUpdate = async () => {
let body
const resource = `${store}`
setEditError(null)
setUpdateLoading(true)

try {
body = JSON.parse(value)
await engine.mutate(
{
type: 'update' as const,
resource: resource,
id: `${namespace}/${key}`,
data: body,
},
{
onComplete: () => {
showSuccess(
i18n.t("Key '{{key}}' updated successfully", {
key,
})
)
refetch({
key,
namespace,
})
},
onError(error) {
showError(
i18n.t(
'There was a problem updating the key - {{error}}',
{
error: error.message,
interpolation: { escapeValue: false },
}
)
)
},
}
)
} catch (error) {
setEditError(error.message)
showError(
i18n.t('There was a problem - {{error}}', {
error: error.message,
interpolation: { escapeValue: false },
})
)
}
setUpdateLoading(false)
}

useEffect(() => {
setValue(JSON.stringify(data?.results, null, 4))
}, [data])

useEffect(() => {
refetch({
key,
namespace,
})
}, [store, namespace, key])

return (
<div>
<PanelHeader>
{data && (
<>
<span className={classes.editorPanelHeader}>{key}</span>
<div className={classes.editButtons}>
<Button
small
aria-label={i18n.t('Close')}
name="close"
onClick={() => handleClose()}
title={i18n.t('Close')}
disabled={updateLoading}
>
{i18n.t('Close')}
</Button>
<Button
small
aria-label={i18n.t('Save')}
name="save"
onClick={() => handleUpdate()}
title={i18n.t('Save')}
primary
loading={!editError && updateLoading}
>
{i18n.t('Save changes')}
</Button>
</div>
</>
)}
</PanelHeader>
<Editor
value={loading ? 'Loading...' : value}
handleChange={handleEditorChange}
/>
</div>
)
}

export default EditSection
45 changes: 0 additions & 45 deletions src/components/sections/EditorSection.tsx

This file was deleted.

8 changes: 6 additions & 2 deletions src/components/table/ItemsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TableBody,
TableHead,
} from '@dhis2/ui'
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import classes from '../../App.module.css'
import i18n from '../../locales'
Expand Down Expand Up @@ -39,10 +39,14 @@ const ItemsTable = ({
setActiveRow(item)
}

useEffect(() => {
setActiveRow(key)
}, [key])

return (
<div>
{data && (
<DataTable layout="fixed">
<DataTable layout="fixed" scrollHeight="75vh">
<TableHead>
<DataTableRow>
<DataTableColumnHeader
Expand Down
4 changes: 2 additions & 2 deletions src/routes/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Navigate, createHashRouter } from 'react-router-dom'
import ErrorComponent from '../components/error/ErrorComponent'
import Edit from '../components/pages/Edit'
import EditPage from '../components/pages/Edit'
import NamespacesPage from '../components/pages/Namespaces'
import EditorPanel from '../components/panels/EditorPanel'
import EmptyEditorPanel from '../components/panels/EmptyEditorPanel'
Expand All @@ -20,7 +20,7 @@ export const hashRouter = createHashRouter([
},
{
path: ':store/edit/:namespace',
element: <Edit />,
element: <EditPage />,
children: [
{
index: true,
Expand Down
Loading