Skip to content

Commit

Permalink
Merged in R2-3132-case-incident-error (pull request #7001)
Browse files Browse the repository at this point in the history
R2-3132: Fixing issue where incorrect violence type field is selected for case to incident form
  • Loading branch information
jtoliver-quoin authored and pnabutovsky committed Dec 4, 2024
2 parents 2cb1af1 + 8c423ea commit c676294
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import IncidentDetail from "../detail";
import useMemoizedSelector from "../../../../libs/use-memoized-selector";
import { getIncidentAvailable } from "../../../records";
import { getFieldByName } from "../../../record-form/selectors";
import includeCPByDefault from "../../../../libs/include-cp-by-default";

import { CP_VIOLENCE_TYPE, GBV_VIOLENCE_TYPE } from "./constants";
import { getViolenceType } from "../../selectors";

function Component({
incident,
Expand All @@ -34,8 +32,7 @@ function Component({
const handleExpanded = () => {
setExpanded(!expanded);
};

const violationType = includeCPByDefault(incident.get("module_id", false)) ? CP_VIOLENCE_TYPE : GBV_VIOLENCE_TYPE;
const violationType = useMemoizedSelector(state => getViolenceType(state, incident.get("module_id")));
const incidentTypeData = incident.get(violationType, false) || undefined;
const incidentUniqueID = incident.get("unique_id", false);
const incidentDateData = incident.get("incident_date", false);
Expand All @@ -46,7 +43,6 @@ function Component({

const isIncidentAvailable = useMemoizedSelector(state => getIncidentAvailable(state, incidentUniqueID));
const violenceTypeField = useMemoizedSelector(state => getFieldByName(state, violationType));

const lookupViolenceType = violenceTypeField.option_strings_source?.replace(/lookup /, "");

const incidentType = lookupViolenceType && (
Expand Down
16 changes: 16 additions & 0 deletions app/javascript/components/incidents-from-case/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.

/* eslint-disable import/prefer-default-export */
import { MODULES } from "../../config";
import { selectModule } from "../application";

import { CP_VIOLENCE_TYPE, GBV_VIOLENCE_TYPE } from "./components/panel/constants";

function getViolenceType(state, moduleID) {
const defaultModule = moduleID === MODULES.GBV ? GBV_VIOLENCE_TYPE : CP_VIOLENCE_TYPE;
const selectedModule = selectModule(state, moduleID);

return selectedModule.getIn(["options", "violation_type_field"], defaultModule);
}

export { getViolenceType };
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.

import { fromJS } from "immutable";

import { PrimeroModuleRecord } from "../application/records";
import { MODULES } from "../../config";

import { getViolenceType } from "./selectors";
import { CP_VIOLENCE_TYPE, GBV_VIOLENCE_TYPE } from "./components/panel/constants";

const state = fromJS({
user: {
modules: ["test1"]
},
application: {
modules: [
PrimeroModuleRecord({ unique_id: "test1", options: { violation_type_field: "test1_violence_type_field_id" } })
]
}
});

describe("<IncidentFromCase /> - Selectors", () => {
describe("getViolenceType", () => {
it("should return violence type field id from store", () => {
const violenceType = getViolenceType(state, "test1");

expect(violenceType).to.deep.equal("test1_violence_type_field_id");
});

it("should return violence type field id from store", () => {
const violenceType = getViolenceType(state, MODULES.CP);

expect(violenceType).to.deep.equal(CP_VIOLENCE_TYPE);
});

it("should return violence type field id from store", () => {
const violenceType = getViolenceType(state, MODULES.GBV);

expect(violenceType).to.deep.equal(GBV_VIOLENCE_TYPE);
});
});
});

0 comments on commit c676294

Please sign in to comment.