From 37c6f5ee7fb111f018ce252d55ec69eb478606c8 Mon Sep 17 00:00:00 2001 From: Dennis Hernandez Date: Wed, 13 Sep 2023 17:29:03 -0600 Subject: [PATCH] R2-2608 - Users can create multiple cases for a family details entry * Refactor api response to return the updated version of the family details entries and not the whole record. * Use dialog hook to close the create dialog until the response is completed --- app/controllers/api/v2/children_controller.rb | 2 -- app/controllers/api/v2/families_controller.rb | 2 +- .../record-form/form/subforms/constants.js | 1 + .../subforms/subform-dialog/component.jsx | 20 +++++++------ app/javascript/components/records/reducer.js | 30 ++----------------- .../components/records/reducer.unit.test.js | 18 +++++++---- .../api/v2/children/family.json.jbuilder | 11 +++++-- .../api/v2/families/create_case.json.jbuilder | 1 + .../api/v2/children_controller_spec.rb | 24 +++++++++++---- 9 files changed, 55 insertions(+), 54 deletions(-) diff --git a/app/controllers/api/v2/children_controller.rb b/app/controllers/api/v2/children_controller.rb index 6dc80ec7a4..20b353ea9b 100644 --- a/app/controllers/api/v2/children_controller.rb +++ b/app/controllers/api/v2/children_controller.rb @@ -28,8 +28,6 @@ def family ) @current_record.save! if @current_record.has_changes_to_save? @record.save! - - select_updated_fields select_fields_for_show end diff --git a/app/controllers/api/v2/families_controller.rb b/app/controllers/api/v2/families_controller.rb index 64025bfc44..50c8594fb7 100644 --- a/app/controllers/api/v2/families_controller.rb +++ b/app/controllers/api/v2/families_controller.rb @@ -14,7 +14,7 @@ def create_case @current_record = Family.find(create_case_params[:family_id]) @record = @current_record.new_child_from_family_member(current_user, create_case_params['family_member_id']) @record.save! - select_updated_fields + select_fields_for_show end def create_case_params diff --git a/app/javascript/components/record-form/form/subforms/constants.js b/app/javascript/components/record-form/form/subforms/constants.js index b47a58c8e1..36bcbad2b2 100644 --- a/app/javascript/components/record-form/form/subforms/constants.js +++ b/app/javascript/components/record-form/form/subforms/constants.js @@ -8,3 +8,4 @@ export const SUBFORM_FIELDS = "SubformFields"; export const SUBFORM_FIELD_ARRAY = "SubformFieldArray"; export const SUBFORM_DIALOG = "SubformDialog"; export const SUBFORM_FIELD_SUBFORM = "SubformFieldSubform"; +export const SUBFORM_CREATE_CASE_DIALOG = "SubformCreateCaseDialog"; diff --git a/app/javascript/components/record-form/form/subforms/subform-dialog/component.jsx b/app/javascript/components/record-form/form/subforms/subform-dialog/component.jsx index 427f9b39ad..397ea8d4e3 100644 --- a/app/javascript/components/record-form/form/subforms/subform-dialog/component.jsx +++ b/app/javascript/components/record-form/form/subforms/subform-dialog/component.jsx @@ -8,11 +8,11 @@ import isEmpty from "lodash/isEmpty"; import { useDispatch } from "react-redux"; import { fieldValidations } from "../../validations"; -import { SUBFORM_DIALOG } from "../constants"; +import { SUBFORM_CREATE_CASE_DIALOG, SUBFORM_DIALOG } from "../constants"; import ServicesSubform from "../services-subform"; import SubformMenu from "../subform-menu"; import { getSubformValues, serviceHasReferFields, updateSubformEntries, addSubformEntries } from "../../utils"; -import ActionDialog from "../../../../action-dialog"; +import ActionDialog, { useDialog } from "../../../../action-dialog"; import SubformDrawer from "../subform-drawer"; import { compactValues, constructInitialValues } from "../../../utils"; import SubformErrors from "../subform-errors"; @@ -61,9 +61,9 @@ const Component = ({ const params = useParams(); const dispatch = useDispatch(); const [initialValues, setInitialValues] = useState({}); + const { dialogOpen, setDialog } = useDialog(SUBFORM_CREATE_CASE_DIALOG); const [openConfirmationModal, setOpenConfirmationModal] = useState(false); - const [openCreateCaseConfirmationModal, setOpenCreateCaseConfirmationModal] = useState(false); - const caseFromFamilyMemberLoading = useMemoizedSelector(state => getCaseFormFamilyMemberLoading(state)); + const caseFromFamilyMemberLoading = useMemoizedSelector(state => getCaseFormFamilyMemberLoading(state, recordType)); const childFormikRef = useRef(); const isValidIndex = index === 0 || index > 0; const asDrawer = isViolation || isViolationAssociation || isFamilyMember || isFamilyDetail; @@ -186,15 +186,17 @@ const Component = ({ }; const createCaseConfirmationProps = { - open: openCreateCaseConfirmationModal, + open: dialogOpen, maxSize: "xs", confirmButtonLabel: isFamilyMember ? i18n.t("family.family_member.create") : i18n.t("case.create"), + omitCloseAfterSuccess: true, dialogTitle: title, dialogText: isFamilyMember ? i18n.t("family.messages.confirm_create_case") : i18n.t("case.messages.confirm_create_case"), - disableBackdropClick: true, - cancelHandler: () => setOpenCreateCaseConfirmationModal(false), + cancelHandler: () => { + setDialog({ dialog: SUBFORM_CREATE_CASE_DIALOG, open: false }); + }, successHandler: () => { if (isFamilyMember) { dispatch(createCaseFromFamilyMember({ familyId: params.id, familyMemberId: subformValues.unique_id })); @@ -272,7 +274,7 @@ const Component = ({ pending={caseFromFamilyMemberLoading} handleCreateLabel={familyCreateLabel} handleCreate={() => { - setOpenCreateCaseConfirmationModal(true); + setDialog({ dialog: SUBFORM_CREATE_CASE_DIALOG, open: true }); }} /> ) : null @@ -304,7 +306,7 @@ const Component = ({ }} - {isFamilySubform && openCreateCaseConfirmationModal && } + {isFamilySubform && } ); diff --git a/app/javascript/components/records/reducer.js b/app/javascript/components/records/reducer.js index 334836b6d0..9a70f4a8b1 100644 --- a/app/javascript/components/records/reducer.js +++ b/app/javascript/components/records/reducer.js @@ -360,16 +360,8 @@ export default namespace => } case `${namespace}/${CREATE_CASE_FROM_FAMILY_MEMBER_SUCCESS}`: { const recordIndex = state.get("data").findIndex(record => record.get("id") === payload.data.id); - const subformIndex = state - .getIn(["data", recordIndex, "family_members"]) - .findIndex(member => member.get("unique_id") === payload.data.record.family_member_id); - return state.updateIn(["data", recordIndex, "family_members", subformIndex], existing => - mergeRecord( - existing, - fromJS({ case_id: payload.data.record.id, case_id_display: payload.data.record.case_id_display }) - ) - ); + return state.setIn(["data", recordIndex, "family_members"], fromJS(payload.data.family_members)); } case `${namespace}/${CREATE_CASE_FROM_FAMILY_MEMBER_FAILURE}`: { return state.setIn(["case_from_family", "errors"], true); @@ -383,24 +375,8 @@ export default namespace => case `${namespace}/${CREATE_CASE_FROM_FAMILY_DETAIL_SUCCESS}`: { const recordIndex = state.get("data").findIndex(record => record.get("id") === payload.data.id); - const result = payload.data.record.family_details_section.reduce((acc, familyDetail) => { - const subformIndex = acc - .getIn(["data", recordIndex, "family_details_section"]) - .findIndex(member => member.get("unique_id") === familyDetail.unique_id); - - if (subformIndex > -1) { - return acc.updateIn(["data", recordIndex, "family_details_section", subformIndex], existing => - mergeRecord(existing, familyDetail) - ); - } - - return acc.setIn( - ["data", recordIndex, "family_details_section"], - acc.getIn(["data", recordIndex, "family_details_section"]).push(fromJS(familyDetail)) - ); - }, state); - - return result + return state + .setIn(["data", recordIndex, "family_details_section"], fromJS(payload.data.family_details_section)) .setIn(["data", recordIndex, "family_number"], payload.data.family_number) .setIn(["data", recordIndex, "family_member_id"], payload.data.family_member_id) .setIn(["data", recordIndex, "family_id"], payload.data.family_id); diff --git a/app/javascript/components/records/reducer.unit.test.js b/app/javascript/components/records/reducer.unit.test.js index 640b95e3e5..992caf7047 100644 --- a/app/javascript/components/records/reducer.unit.test.js +++ b/app/javascript/components/records/reducer.unit.test.js @@ -733,7 +733,13 @@ describe(" - Reducers", () => { }); const action = { type: "TestRecordType/CREATE_CASE_FROM_FAMILY_MEMBER_SUCCESS", - payload: { data: { id: "f001", record: { id: "c001", case_id_display: "001", family_member_id: "m002" } } } + payload: { + data: { + id: "f001", + family_members: [{ unique_id: "m002", case_id: "c001", case_id_display: "001" }], + record: { id: "c001", case_id_display: "001", family_member_id: "m002" } + } + } }; const newState = nsReducer(initialState, action); @@ -787,14 +793,14 @@ describe(" - Reducers", () => { family_id: "f001", family_number: "fn001", family_member_id: "fd001", + family_details_section: [ + { unique_id: "fd001", relation_sex: "female", case_id: "c002", case_id_display: "002" }, + { unique_id: "fd002", relation_sex: "male", case_id: "c001", case_id_display: "001" } + ], record: { id: "c002", case_id_display: "002", - family_member_id: "fd002", - family_details_section: [ - { unique_id: "fd001", relation_sex: "female", case_id: "c002", case_id_display: "002" }, - { unique_id: "fd002", relation_sex: "male", case_id: "c001", case_id_display: "001" } - ] + family_member_id: "fd002" } } } diff --git a/app/views/api/v2/children/family.json.jbuilder b/app/views/api/v2/children/family.json.jbuilder index 4b83143e03..90fb8daf18 100644 --- a/app/views/api/v2/children/family.json.jbuilder +++ b/app/views/api/v2/children/family.json.jbuilder @@ -5,9 +5,14 @@ json.data do json.family_id @current_record.family_id if @selected_field_names.include?('family_id') json.family_number @current_record.family_number if @selected_field_names.include?('family_number') json.family_member_id @current_record.family_member_id if @selected_field_names.include?('family_member_id') + + if @selected_field_names.include?('family_details_section') + json.family_details_section @current_record.family_members_details + end + json.record do - json.partial! 'api/v2/records/record', - record: @record, - selected_field_names: @updated_field_names + json.id @record.id + json.case_id_display @record.case_id_display + json.family_member_id @record.family_member_id if @selected_field_names.include?('family_member_id') end end diff --git a/app/views/api/v2/families/create_case.json.jbuilder b/app/views/api/v2/families/create_case.json.jbuilder index df66e741ee..c6020d68c0 100644 --- a/app/views/api/v2/families/create_case.json.jbuilder +++ b/app/views/api/v2/families/create_case.json.jbuilder @@ -2,6 +2,7 @@ json.data do json.id @current_record.id + json.family_members @current_record.family_members if @selected_field_names.include?('family_members') json.record do json.id @record.id json.case_id_display @record.case_id_display diff --git a/spec/requests/api/v2/children_controller_spec.rb b/spec/requests/api/v2/children_controller_spec.rb index c970046b6b..53226f468c 100644 --- a/spec/requests/api/v2/children_controller_spec.rb +++ b/spec/requests/api/v2/children_controller_spec.rb @@ -1121,8 +1121,14 @@ expect(json['data']).not_to have_key('family_id') expect(json['data']['family_number']).to be_nil expect(json['data']).not_to have_key('family_member_id') - expect(json['data']['record']['sex']).to eq('male') - expect(json['data']['record']['age']).to eq(5) + expect(json['data']['family_details_section'][0]['unique_id']).to eq(@member_unique_id3) + expect(json['data']['family_details_section'][0]['relation_sex']).to eq('male') + expect(json['data']['family_details_section'][0]['relation_age']).to eq(5) + expect(json['data']['family_details_section'][0]['case_id']).not_to be_nil + expect(json['data']['family_details_section'][0]['case_id_display']).not_to be_nil + expect(json['data']['record']['id']).not_to be_nil + expect(json['data']['record']['case_id_display']).not_to be_nil + expect(json['data']['record']).not_to have_key('family_member_id') end it 'creates a new child linked to a family when there is a family record' do @@ -1137,9 +1143,16 @@ expect(json['data']).not_to have_key('family_id') expect(json['data']['family_number']).to eq(@family2.family_number) expect(json['data']).not_to have_key('family_member_id') + expect(json['data']['family_details_section'].size).to eq(2) + expect(json['data']['family_details_section'][1]['unique_id']).to eq(@member_unique_id5) + expect(json['data']['family_details_section'][1]['relation_name']).to eq('Test8 - Member3') + expect(json['data']['family_details_section'][1]['relation_sex']).to eq('male') + expect(json['data']['family_details_section'][1]['relation_age']).to eq(4) + expect(json['data']['family_details_section'][1]['case_id']).not_to be_nil + expect(json['data']['family_details_section'][1]['case_id_display']).not_to be_nil expect(json['data']['record']['id']).not_to be_nil - expect(json['data']['record']['sex']).to eq('male') - expect(json['data']['record']['age']).to eq(4) + expect(json['data']['record']['case_id_display']).not_to be_nil + expect(json['data']['record']).not_to have_key('family_member_id') end it 'creates a new child and returns the family data if a user has the view_family_record permission' do @@ -1162,8 +1175,7 @@ expect(json['data']['family_number']).to eq(@family2.family_number) expect(json['data']['family_member_id']).to eq(@member_unique_id2) expect(json['data']['record']['id']).not_to be_nil - expect(json['data']['record']['sex']).to eq('male') - expect(json['data']['record']['age']).to eq(10) + expect(json['data']['record']['case_id_display']).not_to be_nil expect(json['data']['record']['family_member_id']).to eq(@member_unique_id4) end end