Skip to content

Commit

Permalink
[#391] Handle new user journey when prev case doesn't have any segmen…
Browse files Browse the repository at this point in the history
…ts yet
  • Loading branch information
wayangalihpratama committed Jan 17, 2025
1 parent 88e48ef commit ca35141
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
27 changes: 16 additions & 11 deletions frontend/src/pages/cases/Case.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,14 @@ const SegmentTabsWrapper = ({ children, setbackfunction, setnextfunction }) => {

// set default active segmentId
useEffect(() => {
if (!activeSegmentId && !isEmpty(currentCase.segments)) {
CaseUIState.update((s) => ({
...s,
general: {
...s.general,
activeSegmentId: currentCase.segments?.[0]?.id || null,
},
}));
}
}, [activeSegmentId, currentCase.segments]);
CaseUIState.update((s) => ({
...s,
general: {
...s.general,
activeSegmentId: currentCase.segments?.[0]?.id || null,
},
}));
}, [currentCase.segments]);

const segmentTabItems = useMemo(() => {
return currentCase.segments.map((segment) => ({
Expand Down Expand Up @@ -133,7 +131,13 @@ const SegmentTabsWrapper = ({ children, setbackfunction, setnextfunction }) => {
: null
),
}));
}, [currentCase, children, setbackfunction, setnextfunction, childrenCount]);
}, [
currentCase.segments,
children,
setbackfunction,
setnextfunction,
childrenCount,
]);

return (
<Row id="steps" gutter={[20, 20]}>
Expand Down Expand Up @@ -285,6 +289,7 @@ const Case = () => {
.catch((e) => {
const { status } = e.response;
updateStepIncomeTargetState("regionOptionStatus", status);
updateStepIncomeTargetState("regionOptions", []);
})
.finally(() => {
updateStepIncomeTargetState("regionOptionLoading", false);
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/pages/cases/Cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { api } from "../../lib";
import { isEmpty } from "lodash";
import { adminRole } from "../../store/static";
import { stepPath } from "./store";
import { resetCurrentCaseState } from "./store/current_case";

const { Search } = Input;

Expand Down Expand Up @@ -260,6 +261,11 @@ const Cases = () => {
},
];

useEffect(() => {
// reset currentCase state
resetCurrentCaseState();
}, []);

useEffect(() => {
if (userID || refresh) {
const { country, commodity, tags, year, email } = filters;
Expand Down
18 changes: 13 additions & 5 deletions frontend/src/pages/cases/layout/CaseWrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useRef } from "react";
import "./case-wrapper.scss";
import { useNavigate } from "react-router-dom";
import { Row, Col, Steps, Layout, Affix, Button, Space } from "antd";
import { Row, Col, Steps, Layout, Affix, Button, Space, Alert } from "antd";
import { ContentLayout } from "../../../components/layout";
import {
ArrowLeftOutlined,
Expand Down Expand Up @@ -107,10 +107,18 @@ const CaseWrapper = ({ children, step, caseId, currentCase }) => {
</Button>
}
>
{React.cloneElement(children, {
setbackfunction: (fn) => (backFunctionRef.current = fn),
setnextfunction: (fn) => (nextFunctionRef.current = fn),
})}
{currentCase.segments.filter((s) => s.id).length ? (
React.cloneElement(children, {
setbackfunction: (fn) => (backFunctionRef.current = fn),
setnextfunction: (fn) => (nextFunctionRef.current = fn),
})
) : (
// Show alert if current case doesn't have any segments
<Alert
message="Unable to load the page. Please add segments in the Case settings first."
type="warning"
/>
)}
</ContentLayout>
</Content>
</Col>
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/pages/cases/steps/SetIncomeTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
PrevCaseState,
stepPath,
} from "../store";
import { resetCurrentCaseState } from "../store/current_case";
import { yesNoOptions } from "../../../store/static";
import {
InputNumberThousandFormatter,
Expand Down Expand Up @@ -384,7 +383,6 @@ const SetIncomeTarget = ({ segment, setbackfunction, setnextfunction }) => {
]);

const backFunction = useCallback(() => {
resetCurrentCaseState();
navigate("/cases");
}, [navigate]);

Expand Down

0 comments on commit ca35141

Please sign in to comment.