Repository navigation
feature: Overdue inbound dashboard card #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: spec-driven-development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| /* eslint no-param-reassign: ["error", { "props": false }] */ | ||
| import _ from 'lodash'; | ||
| import queryString from 'query-string'; | ||
| import { addTranslationForLanguage } from 'react-localize-redux'; | ||
| import { addTranslationForLanguage, getTranslate } from 'react-localize-redux'; | ||
|
|
||
| import { | ||
| ADD_EMPTY_ROW, | ||
|
|
@@ -85,7 +85,9 @@ import RoleType from 'consts/roleType'; | |
| import { UnitOfMeasureType } from 'consts/UnitOfMeasureType'; | ||
| import apiClient, { parseResponse } from 'utils/apiClient'; | ||
| import { removeBinLocationData } from 'utils/cycleCountUtils'; | ||
| import dashboardResponseAdapters from 'utils/dashboard-response-adapters'; | ||
| import { fetchBins, getLotNumbersByProductIds, mapShipmentTypes } from 'utils/option-utils'; | ||
| import { translateWithDefaultMessage } from 'utils/Translate'; | ||
|
|
||
| export function showSpinner() { | ||
| return { | ||
|
|
@@ -304,16 +306,27 @@ function getParameterList(params = '', locationId = '', userId = '') { | |
| return listParams; | ||
| } | ||
|
|
||
| function getTranslateFromState(getState) { | ||
| return translateWithDefaultMessage(getTranslate(getState().localize)); | ||
| } | ||
|
|
||
| function adaptIndicatorResponse(indicatorConfig, responseData, translate) { | ||
| const adapter = dashboardResponseAdapters[indicatorConfig.responseAdapter]; | ||
| return adapter ? adapter(responseData, translate) : responseData; | ||
| } | ||
|
|
||
| function fetchGraphIndicator( | ||
| dispatch, | ||
| indicatorConfig, | ||
| locationId = '', | ||
| params = '', | ||
| translate = (id, defaultMessage) => defaultMessage || id, | ||
| ) { | ||
| const id = indicatorConfig.order; | ||
|
|
||
| const listParams = getParameterList(params, locationId); | ||
| const url = `${indicatorConfig.endpoint}?${listParams}`; | ||
| const separator = indicatorConfig.endpoint.includes('?') ? '&' : '?'; | ||
| const url = `${indicatorConfig.endpoint}${separator}${listParams}`; | ||
|
Comment on lines
+328
to
+329
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Endpoint query-string separator change only affects the new widget The Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| dispatch({ | ||
| type: FETCH_GRAPHS, | ||
|
|
@@ -328,7 +341,7 @@ function fetchGraphIndicator( | |
| }); | ||
|
|
||
| apiClient.get(url).then((res) => { | ||
| const indicatorData = res.data; | ||
| const indicatorData = adaptIndicatorResponse(indicatorConfig, res.data, translate); | ||
| dispatch({ | ||
| type: FETCH_GRAPHS, | ||
| payload: { | ||
|
|
@@ -400,14 +413,27 @@ function fetchNumberIndicator( | |
| } | ||
|
|
||
| export function reloadIndicator(indicatorConfig, params, locationId) { | ||
| return (dispatch) => { | ||
| return (dispatch, getState) => { | ||
| // new reference so that the original config is not modified | ||
| const indicatorConfigData = JSON.parse(JSON.stringify(indicatorConfig)); | ||
| fetchGraphIndicator(dispatch, indicatorConfigData, locationId, params); | ||
| fetchGraphIndicator( | ||
| dispatch, | ||
| indicatorConfigData, | ||
| locationId, | ||
| params, | ||
| getTranslateFromState(getState), | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| function getData(dispatch, dashboardConfig, locationId, config = 'personal', userId = '') { | ||
| function getData( | ||
| dispatch, | ||
| dashboardConfig, | ||
| locationId, | ||
| config = 'personal', | ||
| userId = '', | ||
| translate, | ||
| ) { | ||
| // new reference so that the original config is not modified | ||
|
|
||
| const dashboard = dashboardConfig.dashboard[config] || {}; | ||
|
|
@@ -423,7 +449,7 @@ function getData(dispatch, dashboardConfig, locationId, config = 'personal', use | |
|
|
||
| _.forEach(visibleWidgets, (widgetConfig) => { | ||
| if (widgetConfig.type === 'graph') { | ||
| fetchGraphIndicator(dispatch, widgetConfig, locationId, ''); | ||
| fetchGraphIndicator(dispatch, widgetConfig, locationId, '', translate); | ||
| } else if (widgetConfig.type === 'number') { | ||
| fetchNumberIndicator(dispatch, widgetConfig, locationId, userId); | ||
| } | ||
|
|
@@ -436,15 +462,15 @@ export function fetchIndicators( | |
| locationId, | ||
| userId, | ||
| ) { | ||
| return (dispatch) => { | ||
| return (dispatch, getState) => { | ||
| dispatch({ | ||
| type: SET_ACTIVE_CONFIG, | ||
| payload: { | ||
| data: config, | ||
| }, | ||
| }); | ||
|
|
||
| getData(dispatch, configData, locationId, config, userId); | ||
| getData(dispatch, configData, locationId, config, userId, getTranslateFromState(getState)); | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -455,9 +481,15 @@ export function resetIndicators() { | |
| } | ||
|
|
||
| export function addToIndicators(widgetConfig, locationId, userId = '') { | ||
| return (dispatch) => { | ||
| return (dispatch, getState) => { | ||
| if (widgetConfig.type === 'graph') { | ||
| fetchGraphIndicator(dispatch, widgetConfig, locationId, ''); | ||
| fetchGraphIndicator( | ||
| dispatch, | ||
| widgetConfig, | ||
| locationId, | ||
| '', | ||
| getTranslateFromState(getState), | ||
| ); | ||
| } else if (widgetConfig.type === 'number') { | ||
| fetchNumberIndicator(dispatch, widgetConfig, locationId, userId); | ||
| } | ||
|
|
@@ -478,8 +510,8 @@ export function reorderIndicators({ oldIndex, newIndex }, e, type) { | |
| } | ||
|
|
||
| // eslint-disable-next-line default-param-last | ||
| export function fetchConfigAndData(locationId, config = 'personal', userId, id, filterSelected) { | ||
| return (dispatch) => { | ||
| export function fetchConfigAndData(locationId, config = 'personal', userId, id) { | ||
| return (dispatch, getState) => { | ||
| apiClient.get(`/api/dashboard/${id}/config`).then((res) => { | ||
| dispatch({ | ||
| type: FETCH_CONFIG_AND_SET_ACTIVE, | ||
|
|
@@ -488,7 +520,7 @@ export function fetchConfigAndData(locationId, config = 'personal', userId, id, | |
| activeConfig: config, | ||
| }, | ||
| }); | ||
| getData(dispatch, res.data, locationId, config, userId, filterSelected); | ||
| getData(dispatch, res.data, locationId, config, userId, getTranslateFromState(getState)); | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ import React from 'react'; | |
|
|
||
| import PropTypes from 'prop-types'; | ||
|
|
||
| import Translate from 'utils/Translate'; | ||
|
|
||
| /* global _ */ | ||
| const TableCard = (props) => { | ||
| const { | ||
|
|
@@ -60,6 +62,16 @@ const TableCard = (props) => { | |
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {!data.body.length && data.emptyMessage ? ( | ||
| <tr className="empty-table-row"> | ||
| <td colSpan={3}> | ||
| <Translate | ||
| id={data.emptyMessage.id} | ||
| defaultMessage={data.emptyMessage.defaultMessage} | ||
| /> | ||
| </td> | ||
| </tr> | ||
| ) : null} | ||
|
Comment on lines
+65
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Empty-message row colSpan is consistent with rendered header columns The empty-state row uses Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| {data.body.map((item) => ( | ||
| <tr | ||
| onClick={() => { | ||
|
|
@@ -116,6 +128,10 @@ TableCard.propTypes = { | |
| nameLink: PropTypes.string, | ||
| nameDataList: PropTypes.arrayOf(PropTypes.string), | ||
| nameLinksList: PropTypes.arrayOf(PropTypes.string), | ||
| emptyMessage: PropTypes.shape({ | ||
| id: PropTypes.string, | ||
| defaultMessage: PropTypes.string, | ||
| }), | ||
| }).isRequired, | ||
| columnsSize: PropTypes.shape({ | ||
| name: PropTypes.string, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import React, { useMemo, useState } from 'react'; | ||
|
|
||
| import PropTypes from 'prop-types'; | ||
| import queryString from 'query-string'; | ||
| import { connect } from 'react-redux'; | ||
| import { withRouter } from 'react-router-dom'; | ||
| import { useLocation, withRouter } from 'react-router-dom'; | ||
|
|
||
| import filterFields from 'components/stock-movement/inbound/FilterFields'; | ||
| import StockMovementInboundFilters from 'components/stock-movement/inbound/StockMovementInboundFilters'; | ||
|
|
@@ -18,7 +19,10 @@ const StockMovementInboundList = (props) => { | |
| setFilterValues, | ||
| filterParams, | ||
| } = useInboundFilters(); | ||
| const [overdue, setOverdue] = useState(false); | ||
| const { search } = useLocation(); | ||
| const [overdue, setOverdue] = useState( | ||
| queryString.parse(search).overdue === 'true', | ||
| ); | ||
|
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Overdue toggle seeded once from URL query
Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| const tableFilterParams = useMemo(() => { | ||
| if (Object.keys(filterParams).length === 0 && !overdue) return filterParams; | ||
| return { ...filterParams, overdue }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import dashboardResponseAdapters from 'utils/dashboard-response-adapters'; | ||
|
|
||
| const translate = (id, defaultMessage) => defaultMessage; | ||
|
|
||
| describe('overdueInboundShipments adapter', () => { | ||
| it('maps rows onto the table card shape, preserving the response order', () => { | ||
| const { data, link } = dashboardResponseAdapters.overdueInboundShipments({ | ||
| data: [ | ||
| { | ||
| id: 'ship1', | ||
| shipmentNumber: 'S1', | ||
| origin: { id: 'a', name: 'Warehouse A' }, | ||
| daysLate: 9, | ||
| }, | ||
| { | ||
| id: 'ship2', | ||
| shipmentNumber: 'S2', | ||
| origin: { id: 'b', name: 'Warehouse B' }, | ||
| daysLate: 2, | ||
| }, | ||
| ], | ||
| count: 2, | ||
| totalCount: 2, | ||
| }, translate); | ||
|
|
||
| expect(data.number).toBe('Shipment'); | ||
| expect(data.name).toBe('Origin'); | ||
| expect(data.value).toBe('Days late'); | ||
| expect(data.body).toEqual([ | ||
| { | ||
| number: 'S1', numberLink: '/openboxes/stockMovement/show/ship1', name: 'Warehouse A', value: '9', | ||
| }, | ||
| { | ||
| number: 'S2', numberLink: '/openboxes/stockMovement/show/ship2', name: 'Warehouse B', value: '2', | ||
| }, | ||
| ]); | ||
| expect(link).toContain('overdue=true'); | ||
| }); | ||
|
|
||
| it('returns an empty body with an empty message when there are no overdue shipments', () => { | ||
| const { data } = dashboardResponseAdapters.overdueInboundShipments({ | ||
| data: [], | ||
| count: 0, | ||
| totalCount: 0, | ||
| }, translate); | ||
|
|
||
| expect(data.body).toEqual([]); | ||
| expect(data.emptyMessage.id).toBe('react.dashboard.overdueInbound.empty.label'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { STOCK_MOVEMENT_URL } from 'consts/applicationUrls'; | ||
|
|
||
| const overdueInboundShipments = (responseData, translate) => { | ||
| const rows = responseData?.data ?? []; | ||
|
|
||
| return { | ||
| link: `${STOCK_MOVEMENT_URL.listInbound()}&overdue=true`, | ||
| data: { | ||
| number: translate('react.dashboard.overdueInbound.column.shipment.label', 'Shipment'), | ||
| name: translate('react.dashboard.overdueInbound.column.origin.label', 'Origin'), | ||
| value: translate('react.dashboard.overdueInbound.column.daysLate.label', 'Days late'), | ||
| emptyMessage: { | ||
| id: 'react.dashboard.overdueInbound.empty.label', | ||
| defaultMessage: 'No overdue inbound shipments', | ||
| }, | ||
| body: rows.map((row) => ({ | ||
| number: row.shipmentNumber, | ||
| numberLink: row.id ? STOCK_MOVEMENT_URL.show(row.id) : undefined, | ||
| name: row.origin?.name, | ||
| value: `${row.daysLate}`, | ||
| })), | ||
| }, | ||
| }; | ||
| }; | ||
|
Comment on lines
+3
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Column headers captured at dispatch time, relying on default-message fallback
Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| /** | ||
| * Maps a response from an API outside /api/dashboard onto the shape a dashboard card expects. | ||
| * Keyed by the responseAdapter declared on the widget in openboxes.dashboardConfig. | ||
| */ | ||
| const dashboardResponseAdapters = { | ||
| overdueInboundShipments, | ||
| }; | ||
|
|
||
| export default dashboardResponseAdapters; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: responseAdapter hook is a generic, backward-compatible extension
The new
responseAdaptermechanism insrc/js/actions/index.js:313-316andsrc/js/utils/dashboard-response-adapters.jsis threaded correctly through all four thunks that reachfetchGraphIndicator(reloadIndicator,fetchIndicators,addToIndicators,fetchConfigAndData), each now passinggetTranslateFromState(getState). For widgets without aresponseAdapter,adaptIndicatorResponsereturns the rawres.data, preserving existing behavior. The backend passes the full config through (UserService.getDashboardConfigdeep-copies the entiredashboardConfig), soresponseAdapter/endpointreach the frontend. I confirmedstate.localizeis the correct store slice (used identically insrc/js/middlewares/index.js:12).Was this helpful? React with 👍 or 👎 to provide feedback.