Skip to content

Commit

Permalink
[#391] Handle enableEditCase state
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Jan 20, 2025
1 parent 465a593 commit 2a2107b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
51 changes: 51 additions & 0 deletions frontend/src/pages/cases/Case.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PrevCaseState,
CaseVisualState,
} from "./store";
import { UserState } from "../../store";
import {
SetIncomeTarget,
EnterIncomeData,
Expand All @@ -21,6 +22,7 @@ import { EnterIncomeDataVisual } from "./visualizations";
import "./steps/steps.scss";
import { isEmpty, orderBy } from "lodash";
import { customFormula } from "../../lib/formula";
import { adminRole } from "../../store/static";

const commodityOrder = ["focus", "secondary", "tertiary", "diversified"];
const masterCommodityCategories = window.master?.commodity_categories || [];
Expand Down Expand Up @@ -171,6 +173,7 @@ const Case = () => {
const { questionGroups, totalIncomeQuestions } = CaseVisualState.useState(
(s) => s
);
const userState = UserState.useState((s) => s);

const updateStepIncomeTargetState = (key, value) => {
CaseUIState.update((s) => {
Expand All @@ -181,6 +184,54 @@ const Case = () => {
});
};

// check for enableEditCase
useEffect(() => {
const checkEnableEditCase = () => {
if (adminRole.includes(userState?.role)) {
return true;
}
// allow internal user to create new case
if (userState?.internal_user && !caseId) {
return true;
}
// check user access
const userPermission = userState.case_access.find(
(a) => a.case === parseInt(caseId)
)?.permission;
// allow internal user case owner to edit case
if (
userState?.internal_user &&
currentCase?.created_by === userState?.email
) {
return true;
}
if (
(userState?.internal_user && !userPermission) ||
userPermission === "view"
) {
return false;
}
if (userPermission === "edit") {
return true;
}
return false;
};
CaseUIState.update((s) => ({
...s,
general: {
...s.general,
enableEditCase: checkEnableEditCase,
},
}));
}, [
caseId,
userState?.internal_user,
userState?.email,
userState?.case_access,
userState?.role,
currentCase?.created_by,
]);

// Fetch case details
useEffect(() => {
if (caseId && currentCase.id !== parseInt(caseId)) {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/pages/cases/components/CaseSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ const CaseSettings = ({ open = false, handleCancel = () => {} }) => {
onOk={() => form.submit()}
okButtonProps={{
loading: isSaving,
disabled: !enableEditCase,
}}
okText="Save case"
onCancel={handleCancel}
Expand Down
15 changes: 13 additions & 2 deletions frontend/src/pages/cases/components/SegmentForm.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React from "react";
import { Form, Card, Button, Row, Col, Input, InputNumber } from "antd";
import { CloseOutlined, PlusOutlined } from "@ant-design/icons";
import { CaseUIState } from "../store";

const MAX_SEGMENT = 5;

const SegmentForm = () => {
const MAX_SEGMENT = 5;
const { enableEditCase } = CaseUIState.useState((s) => s.general);

return (
<Form.List name="segments">
{(fields, { add, remove }) => (
Expand All @@ -19,6 +23,7 @@ const SegmentForm = () => {
type="icon"
icon={<CloseOutlined />}
onClick={() => remove(name)}
disabled={!enableEditCase}
/>
) : null
}
Expand All @@ -39,7 +44,11 @@ const SegmentForm = () => {
},
]}
>
<Input width="100%" placeholder="Segment name" />
<Input
width="100%"
placeholder="Segment name"
disabled={!enableEditCase}
/>
</Form.Item>
</Col>
<Col span={10}>
Expand All @@ -48,6 +57,7 @@ const SegmentForm = () => {
placeholder="Number of farmers"
controls={false}
style={{ width: "100%" }}
disabled={!enableEditCase}
/>
</Form.Item>
</Col>
Expand All @@ -61,6 +71,7 @@ const SegmentForm = () => {
onClick={() => add()}
block
icon={<PlusOutlined />}
disabled={!enableEditCase}
>
Add another segment
</Button>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/cases/steps/SetIncomeTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ const SetIncomeTarget = ({ segment, setbackfunction, setnextfunction }) => {
},
]}
>
<Radio.Group options={yesNoOptions} />
<Radio.Group options={yesNoOptions} disabled={!enableEditCase} />
</Form.Item>
</Col>
{renderTargetInput(setTargetYourself)}
Expand Down

0 comments on commit 2a2107b

Please sign in to comment.