From 5ad3f47d3618a489713beafa34b082374b9258c3 Mon Sep 17 00:00:00 2001 From: Derek Leadbetter Date: Wed, 27 Jul 2022 09:57:33 -0400 Subject: [PATCH] UDCSL #28 - Updating ReferenceCodeFormDropdown to allow lookup of existing reference tables (by key) and creation of new reference tables --- .../components/ReferenceCodeFormDropdown.js | 38 +++++++++++-------- .../shared/src/services/ReferenceTables.js | 11 ++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/packages/semantic-ui/src/components/ReferenceCodeFormDropdown.js b/packages/semantic-ui/src/components/ReferenceCodeFormDropdown.js index 32e2de71..aff4cc79 100644 --- a/packages/semantic-ui/src/components/ReferenceCodeFormDropdown.js +++ b/packages/semantic-ui/src/components/ReferenceCodeFormDropdown.js @@ -1,7 +1,7 @@ // @flow import { ReferenceTablesService } from '@performant-software/shared-components'; -import React, { type ComponentType, useState } from 'react'; +import React, { type ComponentType, useEffect, useState } from 'react'; import { Form } from 'semantic-ui-react'; import EditModal from './EditModal'; import ReferenceCodeDropdown from './ReferenceCodeDropdown'; @@ -16,17 +16,30 @@ type Props = { }; const ReferenceCodeFormDropdown: ComponentType = (props: Props) => { - const [modal, setModal] = useState(false); - const [key, setKey] = useState(0); - const { error, label, required, - referenceTable, + referenceTable: key, ...rest } = props; + const [modal, setModal] = useState(false); + const [dropdownKey, setDropdownKey] = useState(0); + const [referenceTable, setReferenceTable] = useState({ key }); + + /** + * Looks up the existing reference table base on the passed key. + */ + useEffect(() => ( + ReferenceTablesService + .fetchByKey(key) + .then(({ data }) => setReferenceTable((prevTable) => ({ + ...prevTable, + ...data.reference_table + }))) + ), [key]); + return ( <> = (props: Props) => { setModal(true)} - referenceTable={referenceTable} + referenceTable={referenceTable.key} /> )} required={required} @@ -43,25 +56,20 @@ const ReferenceCodeFormDropdown: ComponentType = (props: Props) => { { modal && ( setModal(false)} - onInitialize={(id) => ( - ReferenceTablesService - .fetchOne(id) - .then(({ data }) => data.reference_table) - )} onSave={(record) => ( ReferenceTablesService .save(record) .then(({ data }) => data.reference_table) - .then(() => setKey((prevKey) => prevKey + 1)) + .then(() => setDropdownKey((prevKey) => prevKey + 1)) .finally(() => setModal(false)) )} /> diff --git a/packages/shared/src/services/ReferenceTables.js b/packages/shared/src/services/ReferenceTables.js index 232bc9b2..567f1a42 100644 --- a/packages/shared/src/services/ReferenceTables.js +++ b/packages/shared/src/services/ReferenceTables.js @@ -7,6 +7,17 @@ import ReferenceTable from '../transforms/ReferenceTable'; * Class responsible for handling all reference table API requests. */ class ReferenceTables extends BaseService { + /** + * Calls the find_by_key API end point for reference tables. + * + * @param key + * + * @returns {Promise>} + */ + fetchByKey(key) { + return this.getAxios().get(`${this.getBaseUrl()}/find_by_key`, { params: { key } }); + } + /** * Returns the reference tables base URL. *