Skip to content

Commit

Permalink
Merge branch 'develop' into r2-2603-exports-family-number
Browse files Browse the repository at this point in the history
  • Loading branch information
dhernandez-quoin committed Oct 10, 2023
2 parents 6c52ed6 + a8e073a commit ea0e548
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v2/children_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def traces
alias select_updated_fields_super select_updated_fields
def select_updated_fields
changes = @record.saved_changes_to_record.keys
@updated_field_names = select_updated_fields_super + @record.current_care_arrangements_changes(changes)
@updated_field_names << 'family_details_section' if @record.family&.family_members_changed?
@updated_field_names = select_updated_fields_super + @record.current_care_arrangements_changes(changes) +
@record.family_changes(changes)
end

def create_family
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ import PageContainer from "../../../page";
import LoadingIndicator from "../../../loading-indicator";
import { clearSelectedRecord, fetchRecord, saveRecord, setSelectedRecord } from "../../../records";
import { RECORD_TYPES, RECORD_TYPES_PLURAL, REFERRAL } from "../../../../config";
import { getIsProcessingSomeAttachment, getLoadingRecordState, getSelectedRecord } from "../../../records/selectors";
import { getIsProcessingSomeAttachment, getLoadingRecordState } from "../../../records/selectors";
import { clearRecordAttachments, fetchRecordsAlerts } from "../../../records/action-creators";
import useIncidentFromCase from "../../../records/use-incident-form-case";
import SaveAndRedirectDialog from "../../../save-and-redirect-dialog";
import { fetchReferralUsers } from "../../../record-actions/transitions/action-creators";
import { SERVICES_SUBFORM } from "../../../record-actions/add-service/constants";
import { getLoadingState, getErrors, getSelectedForm } from "../../selectors";
import {
clearDataProtectionInitialValues,
clearValidationErrors,
setPreviousRecord,
setSelectedForm
} from "../../action-creators";
import { clearDataProtectionInitialValues, clearValidationErrors, setPreviousRecord } from "../../action-creators";
import Nav from "../../nav";
import { RecordForm, RecordFormToolbar } from "../../form";
import css from "../../styles.css";
Expand Down Expand Up @@ -60,7 +55,6 @@ const Component = ({
}) => {
let submitForm = null;
const mobileDisplay = useMediaQuery(theme => theme.breakpoints.down("sm"));
const [selectedRecordChanged, setSelectedRecordChanged] = useState(false);

const { state: locationState } = useLocation();
const history = useHistory();
Expand Down Expand Up @@ -91,7 +85,6 @@ const Component = ({
const loadingRecord = useMemoizedSelector(state => getLoadingRecordState(state, params.recordType));
const errors = useMemoizedSelector(state => getErrors(state));
const selectedForm = useMemoizedSelector(state => getSelectedForm(state));
const selectedRecord = useMemoizedSelector(state => getSelectedRecord(state, params.recordType));
const isProcessingSomeAttachment = useMemoizedSelector(state =>
getIsProcessingSomeAttachment(state, params.recordType)
);
Expand Down Expand Up @@ -245,19 +238,6 @@ const Component = ({
}
}, [selectedForm]);

useEffect(() => {
if (params.id && selectedRecord && selectedRecord !== params.id && containerMode.isShow) {
setSelectedRecordChanged(true);
}
}, [selectedRecord, containerMode.isShow, params.id]);

useEffect(() => {
if (selectedRecordChanged && containerMode.isShow && firstTab) {
dispatch(setSelectedForm(firstTab.unique_id));
setSelectedRecordChanged(false);
}
}, [selectedRecordChanged, containerMode.isShow, firstTab]);

const transitionProps = {
fetchable: isNotANewCase,
isReferral: REFERRAL === selectedForm,
Expand Down Expand Up @@ -332,13 +312,15 @@ const Component = ({
formNav={formNav}
handleToggleNav={handleToggleNav}
isNew={containerMode.isNew}
isShow={containerMode.isShow}
mobileDisplay={mobileDisplay}
recordType={params.recordType}
selectedForm={selectedForm}
selectedRecord={navSelectedRecords}
toggleNav={toggleNav}
primeroModule={selectedModule.primeroModule}
hasForms={hasForms}
recordId={params.id}
formikValuesForNav={formikValuesForNav}
/>
</div>
Expand Down
6 changes: 5 additions & 1 deletion app/javascript/components/record-form/form/record-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ const RecordForm = ({
const redirectToIncident = RECORD_TYPES.cases === recordType ? { redirectToIncident: false } : {};

if (record) {
const recordFormValues = { ...initialValues, ...record.toJS(), ...redirectToIncident };
const recordFormValues = {
...(mode.isNew ? constructInitialValues(forms.values()) : {}),
...record.toJS(),
...redirectToIncident
};

const subformValues = sortSubformValues(recordFormValues, forms.values());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TextField as MuiTextField } from "formik-material-ui";

import { NUMERIC_FIELD } from "../constants";
import { getRecordForms } from "../selectors";
import { RECORD_TYPES } from "../../../config";
import { RECORD_TYPES, CASES } from "../../../config";
import { setupMountedComponent, stub } from "../../../test";
import { FieldRecord, FormSectionRecord } from "../records";

Expand Down Expand Up @@ -306,6 +306,86 @@ describe("<RecordForm />", () => {
});
});

describe("when an record exists", () => {
const initialStateRecordExists = Map({
forms: Map({
formSections: Map({
1: FormSectionRecord({
id: 1,
name: {
en: "Form Section 1"
},
unique_id: "form_section_1",
module_ids: ["some_module"],
visible: true,
is_nested: false,
parent_form: RECORD_TYPES.cases,
fields: [1, 2, 3]
})
}),
fields: Map({
1: FieldRecord({
id: 1,
name: "field_1",
display_name: {
en: "Field 1"
},
type: TEXT_FIELD_NAME,
required: true,
visible: true
}),
2: FieldRecord({
id: 2,
name: "field_2",
display_name: {
en: "Field 2"
},
type: TEXT_FIELD_NAME,
visible: true,
selected_value: "field_2_value"
}),
3: FieldRecord({
id: 3,
name: "field_age",
display_name: {
en: "Field Age"
},
type: NUMERIC_FIELD,
visible: true
})
})
})
});
const queryRecordExists = {
recordType: RECORD_TYPES.cases,
primeroModule: "some_module"
};
const formsRecordsEdit = getRecordForms(initialStateRecordExists, queryRecordExists);

const recordValues = { age: "4", name_first: "test" };

it("should set the values for the case selected", () => {
const { component: fromCaseComponent } = setupMountedComponent(RecordForm, {
bindSubmitForm: () => {},
forms: formsRecordsEdit,
handleToggleNav: () => {},
mobileDisplay: false,
mode: { isNew: false, isEdit: true, isShow: false },
onSubmit: () => {},
record: fromJS(recordValues),
recordType: CASES,
selectedForm: "form_section_1",
incidentFromCase: {},
externalComponents: () => {},
setFormikValuesForNav: () => {}
});

expect(fromCaseComponent.find(FormikForm).props().values).to.deep.equal({
...recordValues
});
});
});

it("renders component with valid props", () => {
const incidentsProps = { ...component.find(RecordForm).props() };

Expand Down
27 changes: 27 additions & 0 deletions app/javascript/components/record-form/nav/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useI18n } from "../../i18n";
import { INCIDENT_FROM_CASE, RECORD_INFORMATION_GROUP, RECORD_TYPES, RECORD_OWNER } from "../../../config";
import {
getIncidentFromCaseForm,
getPreviousRecordType,
getRecordFormsByUniqueId,
getRecordInformationFormIds,
getValidationErrors
Expand All @@ -33,8 +34,10 @@ const Component = ({
hasForms,
handleToggleNav,
isNew,
isShow,
mobileDisplay,
recordType,
recordId,
selectedRecord,
toggleNav,
primeroModule,
Expand All @@ -47,6 +50,7 @@ const Component = ({

const [open, setOpen] = useState("");
const [previousGroup, setPreviousGroup] = useState("");
const [selectedRecordChanged, setSelectedRecordChanged] = useState(false);

const incidentFromCaseForm = useMemoizedSelector(state =>
getIncidentFromCaseForm(state, { recordType, i18n, primeroModule })
Expand All @@ -66,6 +70,8 @@ const Component = ({
const recordInformationFormIds = useMemoizedSelector(state =>
getRecordInformationFormIds(state, { recordType: RECORD_TYPES[recordType], primeroModule })
);
const previousRecordType = useMemoizedSelector(state => getPreviousRecordType(state));
const selectedRecordId = useMemoizedSelector(state => getSelectedRecord(state, recordType));

const formGroupLookup = useOptions({
source: buildFormGroupUniqueId(primeroModule, RECORD_TYPES[recordType].replace("_", "-"))
Expand Down Expand Up @@ -161,6 +167,25 @@ const Component = ({
}
}, [history.action, firstSelectedForm?.form_group_id]);

useEffect(() => {
if (recordId && selectedRecordId && selectedRecordId !== recordId && isShow) {
setSelectedRecordChanged(true);
}
}, [selectedRecord, isShow, recordId]);

useEffect(() => {
if (selectedRecordChanged && isShow && firstTab) {
dispatch(setSelectedForm(firstTab.unique_id));
setSelectedRecordChanged(false);
}
}, [selectedRecordChanged, isShow, firstTab]);

useEffect(() => {
if (firstTab && recordType && previousRecordType && recordType !== previousRecordType) {
dispatch(setSelectedForm(firstTab.unique_id));
}
}, [recordType, previousRecordType, firstTab]);

const drawerClasses = { paper: css.drawerPaper };

if (!formNav) return null;
Expand Down Expand Up @@ -217,8 +242,10 @@ Component.propTypes = {
handleToggleNav: PropTypes.func.isRequired,
hasForms: PropTypes.bool,
isNew: PropTypes.bool,
isShow: PropTypes.bool,
mobileDisplay: PropTypes.bool.isRequired,
primeroModule: PropTypes.string,
recordId: PropTypes.string,
recordType: PropTypes.string,
selectedForm: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
selectedRecord: PropTypes.string,
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/components/record-form/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ export const getShouldFetchRecord = (state, { id, recordType }) => {
return !state.getIn([NAMESPACE, "previousRecord"], fromJS({})).equals(fromJS({ id, recordType }));
};

export const getPreviousRecordType = state => {
return state.getIn([NAMESPACE, "previousRecord", "recordType"]);
};

export const getWritableFields = createCachedSelector(
(state, query) => getRecordForms(state, { ...query, writable: true, checkPermittedForms: true }),
formSections => formSections.flatMap(formSection => formSection.fields)
Expand Down
10 changes: 10 additions & 0 deletions app/javascript/components/record-form/selectors.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1319,4 +1319,14 @@ describe("<RecordForm /> - Selectors", () => {
).to.deep.equals(fromJS(["name_first"]));
});
});

describe("getPreviousRecordType", () => {
it("returns the previousRecordType", () => {
const stateWithPreviousRecord = fromJS({
forms: { previousRecord: { id: "001", recordType: "cases" } }
});

expect(selectors.getPreviousRecordType(stateWithPreviousRecord)).to.equals("cases");
});
});
});
7 changes: 7 additions & 0 deletions app/models/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def update_properties(user, data)
self.family_id = data.delete('family_id') if data.key?('family_id')
self.registry_record_id = data.delete('registry_record_id') if data.key?('registry_record_id')
self.mark_for_reopen = @incidents_to_save.present?
update_family_data(data)
super_update_properties(user, data)
end

Expand Down Expand Up @@ -284,6 +285,12 @@ def display_id
case_id_display
end

def family_number
return super unless family.present?

family.family_number
end

def day_of_birth
return nil unless date_of_birth.is_a? Date

Expand Down
27 changes: 22 additions & 5 deletions app/models/concerns/family_linkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ module FamilyLinkable
before_save :associate_to_family
before_save :update_family_members
after_save :associate_family_member
after_save :update_family
after_save :save_family
end

def stamp_family_fields
return unless changes_to_save.key?('family_id')

self.family_id_display = family&.family_id_display
self.family_number = family&.family_number
end

def associate_to_family
Expand All @@ -39,11 +38,15 @@ def associate_family_member
end
end

def update_family
def update_family_data(child_data)
return unless family.present?

family.family_number = family_number if saved_changes_to_record.keys.include?('family_number')
return unless family.has_changes_to_save?
changed_family_fields = FamilyLinkageService::GLOBAL_FAMILY_FIELDS & child_data.keys
changed_family_fields.each { |field| family.data[field] = child_data.delete(field) }
end

def save_family
return unless family.present? && family.has_changes_to_save?

family.save!
end
Expand Down Expand Up @@ -84,4 +87,18 @@ def find_family_detail(family_detail_id)
def family_members
(family&.family_members || []).reject { |member| member['unique_id'] == family_member_id }
end

def family_changes(changes)
return [] unless family.present?

changes ||= saved_changes_to_record.keys
if changes.include?('family_id_display')
return FamilyLinkageService::GLOBAL_FAMILY_FIELDS + ['family_details_section']
end

field_names = []
field_names << 'family_details_section' if family.family_members_changed?
field_names += FamilyLinkageService::GLOBAL_FAMILY_FIELDS & family.saved_changes_to_record.keys
field_names
end
end
1 change: 0 additions & 1 deletion app/services/record_data_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def embed_family_info(data, record, selected_field_names)

data['family_id'] = record.family_id if selected_field_names.include?('family_id')
data['family_member_id'] = record.family_member_id if selected_field_names.include?('family_member_id')
data['family_number'] = record.family_number if selected_field_names.include?('family_number')
data = embed_family_details(data, record, selected_field_names)
embed_family_details_section(data, record, selected_field_names)
end
Expand Down
2 changes: 1 addition & 1 deletion config/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class Primero::Application
VERSION = '2.7.0'
VERSION = '2.8.0-rc1'
end
Loading

0 comments on commit ea0e548

Please sign in to comment.