|
| 1 | +# Segmentation UI and Flow Improvements |
| 2 | + |
| 3 | +This task aims to improve the visibility of farmer counts per segment and refine the manual segment adjustment process to make it more intuitive and prioritized. |
| 4 | + |
| 5 | +## User Review Required |
| 6 | + |
| 7 | +> [!IMPORTANT] |
| 8 | +> **Cascading Exclusivity**: As requested in the feedback, adjusting the **Maximum** of Segment N will automatically update the **Minimum** of Segment N+1. This ensures segments remain exclusive without manual entry for both sides. |
| 9 | +
|
| 10 | +> [!NOTE] |
| 11 | +> **Scope Clarification**: This plan focuses strictly on the UI refinement (visibility, layout, and cascading boundaries). The separate technical issue of "Inverted Ranges" (Auto-Sorting) is documented in [docs/segmentation_fix/implementation_plan.md](file:///Users/galihpratama/Sites/IDH-IDC/docs/segmentation_fix/implementation_plan.md) and is considered out of scope for *this* UI task unless explicitly combined. |
| 12 | +
|
| 13 | +> [!NOTE] |
| 14 | +> **Manual Preference**: I will move the "Add segment manually" button to be more prominent, suggesting it as the primary way to define segments if automatic generation isn't used. |
| 15 | +
|
| 16 | +## User Acceptance Criteria (UAC) |
| 17 | + |
| 18 | +1. **Enhanced Farmer Visibility**: [x] |
| 19 | + * Each segment card displays the farmer count in a prominent box. |
| 20 | + * **Style**: IDC Green background (`#00605A`), white text, bold font. |
| 21 | + * **Label**: `Number of Farmers: [Count]`. |
| 22 | +2. **Segment Name Initialization**: [x] |
| 23 | + * New segments have blank names by default for numerical variables. |
| 24 | + * Placeholder: `"Please specify the segment name"`. |
| 25 | +3. **Numerical Range Configuration UI**: [x] |
| 26 | + * Minimum and Maximum thresholds are displayed in two clearly labeled white boxes. |
| 27 | + * An **"Adjust"** button (IDC Green) is placed next to these boxes. |
| 28 | + * The thresholds are editable upon clicking "Adjust" or directly (as per existing logic, but with improved styling). |
| 29 | +4. **Bi-directional Segment Exclusivity**: [x] |
| 30 | + * Modifying the **Maximum** of Segment N automatically updates the **Minimum** of Segment N+1. |
| 31 | + * Modifying the **Minimum** of Segment N+1 automatically updates the **Maximum** of Segment N. |
| 32 | + * This ensures segments remain exclusive and continuous regardless of which side is adjusted. |
| 33 | +5. **Prioritized Manual Flow**: [x] |
| 34 | + * The option to adjust segments manually is presented as the preferred/first method. |
| 35 | +6. **Recalculation Trigger**: [x] |
| 36 | + * Inputting values should not auto-trigger recalculation until "Adjust" is clicked. |
| 37 | +7. **Static Calculated Range Display**: [x] |
| 38 | + * The "Segment range: X - Y" label should show the *calculated* range from the backend. |
| 39 | + * It should NOT update when the user is typing into the Min or Max boxes. |
| 40 | + * It should only update when the "Adjust" operation is completed and new backend data is received. |
| 41 | + |
| 42 | +## Numerical Range Behavior: Manual Authority |
| 43 | + |
| 44 | +To ensure the UI respects user intent exactly as discussed, we will adopt the **Manual Authority** model: |
| 45 | + |
| 46 | +1. **Ground Truth**: The values currently visible in the **Min** and **Max** input boxes are the only values used to filter data for a segment. |
| 47 | +2. **Recalculation**: When "Adjust" is clicked, the backend will count farmers where `Min < Value <= Max`. |
| 48 | +3. **Full Bi-directional Cascading**: |
| 49 | + * **Backward**: Adjusting Segment N **Min** to `V` updates Segment N-1 **Max** to `V - offset`. |
| 50 | + * **Forward**: Adjusting Segment N **Max** to `V` updates Segment N+1 **Min** to `V + offset`. |
| 51 | + * *Note*: `offset` is constant at `0.01` for all numerical data types to ensure maximum mathematical precision and prevent data gaps. |
| 52 | +4. **Payload Synchronization**: Both `min` and `max` fields must be explicitly synchronized in the API payload to prevent stale values from overriding the new `value` (reversion bug). |
| 53 | +5. **No Reversion**: The backend must accept the provided `Min` and `Max` and return them as-is, rather than recalculating the `Min` based on a previous segment. |
| 54 | + |
| 55 | +## Bug Fixes |
| 56 | + |
| 57 | +### Answer Value Generation Fix |
| 58 | +- [x] **Inclusive Lower Bound**: Modified `recalculate_numerical_segments` and `process_confirmed_segmentation` to use `>=` for the first segment of each variable. This ensures the minimum value (e.g., 0) is included and answer values are generated. |
| 59 | + |
| 60 | +### Save Case React Crash Fix |
| 61 | +- [ ] **Error Detail Stringification**: Update `CaseSettings.js` to handle `detail` objects (422 errors) from FastAPI, preventing React from crashing when an object is rendered as a child. |
| 62 | +- [ ] **Payload Sanitization**: Ensure `min` and `max` are only sent as numbers for numerical segments. For categorical segments, these should be `null` to avoid Pydantic validation errors (422). |
| 63 | + |
| 64 | +### UnboundLocalError Fix |
| 65 | +- [x] **Scoping Fix**: Move `prev_seg_same_var` lookup outside the `if/else` block in `process_confirmed_segmentation.py` to ensure it is always defined before being referenced. |
| 66 | + |
| 67 | +## Technical Acceptance Criteria (TAC) |
| 68 | + |
| 69 | +1. **Frontend Component Updates**: |
| 70 | + * Modify `renderSegmentCard` in [DataUploadSegmentForm.js](file:///Users/galihpratama/Sites/IDH-IDC/frontend/src/pages/cases/components/DataUploadSegmentForm.js). |
| 71 | + * Implement the horizontal layout for thresholds: Range Boxes + Adjust Button. |
| 72 | + * Update [SegmentConfigurationForm.js](file:///Users/galihpratama/Sites/IDH-IDC/frontend/src/pages/cases/components/SegmentConfigurationForm.js) to handle both `min` and `max` cascading range updates in `handleOnChangeFieldValue`. |
| 73 | +2. **Styling**: |
| 74 | + * Define new utility classes in `steps.scss` for the "Threshold Box" styles. |
| 75 | + * Apply IDC branding colors: `$primary-color` (`#00605A`) for backgrounds and buttons. |
| 76 | +3. **Data Logic**: |
| 77 | + * Ensure the `segments` array in the form state correctly initializes `name` to `null` or `""` for numerical variables. |
| 78 | + * Implement boundary checks to prevent overlapping logic in the frontend before sending to the backend for recalculation. |
| 79 | +4. **Verification**: |
| 80 | + * Verify cascading logic for at least 3 segments. |
| 81 | + * Verify farmer count visibility across different screen sizes. |
| 82 | + |
| 83 | +## Estimation |
| 84 | + |
| 85 | +| Component | Task | Effort | |
| 86 | +| :--- | :--- | :--- | |
| 87 | +| **Frontend UI** | Refactoring `renderSegmentCard` layout and implementing new boxes. | 4h | |
| 88 | +| **Logic** | Implementing cascading range updates and name initialization. | 2h | |
| 89 | +| **Styling** | SCSS updates for IDC branding and alignment. | 2h | |
| 90 | +| **Total** | | **8h (~1 Story Point)** | |
| 91 | + |
| 92 | +## Proposed Changes |
| 93 | + |
| 94 | +### [Frontend] |
| 95 | + |
| 96 | +#### [MODIFY] [DataUploadSegmentForm.js](file:///Users/galihpratama/Sites/IDH-IDC/frontend/src/pages/cases/components/DataUploadSegmentForm.js) |
| 97 | +- Update `renderSegmentCard` to include Min/Max threshold boxes with "Adjust" button. |
| 98 | + |
| 99 | +#### [MODIFY] [SegmentConfigurationForm.js](file:///Users/galihpratama/Sites/IDH-IDC/frontend/src/pages/cases/components/SegmentConfigurationForm.js) |
| 100 | +- Update `handleOnChangeFieldValue` to implement bi-directional cascading adjustment logic (Min updates previous Max). |
| 101 | + |
| 102 | +#### [MODIFY] [steps.scss](file:///Users/galihpratama/Sites/IDH-IDC/frontend/src/pages/cases/styles/steps.scss) |
| 103 | +- Add styles for `.threshold-box-container` and `.threshold-adjust-btn`. |
| 104 | + |
| 105 | +## Verification Plan |
| 106 | + |
| 107 | +### Automated Tests |
| 108 | +- Run existing segmentation tests to ensure no regressions: |
| 109 | + `./dc.sh test backend/tests/test_unit_segmentation_strategy.py` |
| 110 | + |
| 111 | +### Manual Verification |
| 112 | +1. Open segment configuration in Step 1. |
| 113 | +2. Select a numerical variable and set 3 segments. |
| 114 | +3. Verify the "Number of Farmers" is clearly visible in green. |
| 115 | +4. Adjust the Maximum of Segment 1. |
| 116 | +5. Verify Segment 2's Minimum updates automatically. |
| 117 | +6. Verify segment names are blank with the correct placeholder. |
0 commit comments