Skip to content

Commit

Permalink
UDCSL #28 - Updating ReferenceCodeFormDropdown to allow lookup of exi…
Browse files Browse the repository at this point in the history
…sting reference tables (by key) and creation of new reference tables
  • Loading branch information
dleadbetter committed Jul 27, 2022
1 parent f5e4438 commit 5ad3f47
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
38 changes: 23 additions & 15 deletions packages/semantic-ui/src/components/ReferenceCodeFormDropdown.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,17 +16,30 @@ type Props = {
};

const ReferenceCodeFormDropdown: ComponentType<any> = (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 (
<>
<Form.Input
Expand All @@ -35,33 +48,28 @@ const ReferenceCodeFormDropdown: ComponentType<any> = (props: Props) => {
<ReferenceCodeFormLabel
label={label}
onClick={() => setModal(true)}
referenceTable={referenceTable}
referenceTable={referenceTable.key}
/>
)}
required={required}
>
<ReferenceCodeDropdown
{...rest}
id={referenceTable}
referenceTable={referenceTable}
key={key}
referenceTable={referenceTable.key}
key={dropdownKey}
/>
</Form.Input>
{ modal && (
<EditModal
component={ReferenceTableModal}
item={{ id: referenceTable }}
item={referenceTable}
onClose={() => 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))
)}
/>
Expand Down
11 changes: 11 additions & 0 deletions packages/shared/src/services/ReferenceTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<AxiosResponse<any>>}
*/
fetchByKey(key) {
return this.getAxios().get(`${this.getBaseUrl()}/find_by_key`, { params: { key } });
}

/**
* Returns the reference tables base URL.
*
Expand Down

0 comments on commit 5ad3f47

Please sign in to comment.