diff --git a/app/javascript/components/widgets/widgets/forest-change/fires-alerts/actions.js b/app/javascript/components/widgets/widgets/forest-change/fires-alerts/actions.js index d27eb67513..96a0de8614 100644 --- a/app/javascript/components/widgets/widgets/forest-change/fires-alerts/actions.js +++ b/app/javascript/components/widgets/widgets/forest-change/fires-alerts/actions.js @@ -1,7 +1,10 @@ -import { fetchFiresAlerts } from 'services/alerts'; +import { fetchFiresAlerts, fetchFiresLatest } from 'services/alerts'; +import axios from 'axios'; export default ({ params }) => - fetchFiresAlerts(params).then(alerts => { - const { data } = alerts.data; - return data || {}; - }); + axios.all([fetchFiresAlerts(params), fetchFiresLatest(params)]).then( + axios.spread((alerts, latest) => { + const { data } = alerts.data; + return { alerts: data, latest } || {}; + }) + ); diff --git a/app/javascript/components/widgets/widgets/forest-change/fires-alerts/selectors.js b/app/javascript/components/widgets/widgets/forest-change/fires-alerts/selectors.js index 4b772bdd10..76946d2ebe 100644 --- a/app/javascript/components/widgets/widgets/forest-change/fires-alerts/selectors.js +++ b/app/javascript/components/widgets/widgets/forest-change/fires-alerts/selectors.js @@ -12,7 +12,8 @@ import { getChartConfig } from 'components/widgets/utils/data'; -const getAlerts = state => state.data || null; +const getAlerts = state => (state.data && state.data.alerts) || null; +const getLatest = state => (state.data && state.data.latest) || null; const getColors = state => state.colors || null; const getActiveData = state => state.settings.activeData || null; const getWeeks = state => state.settings.weeks || null; @@ -87,13 +88,9 @@ export const parseData = createSelector([getDates, getWeeks], (data, weeks) => { return data.slice(-weeks); }); -export const parseConfig = createSelector([getColors], colors => - getChartConfig( - colors, - moment() - .subtract(2, 'w') - .format('YYYY-MM-DD') - ) +export const parseConfig = createSelector( + [getColors, getLatest], + (colors, latest) => getChartConfig(colors, moment(latest)) ); export const parseSentence = createSelector( diff --git a/app/javascript/components/widgets/widgets/forest-change/glad-alerts/actions.js b/app/javascript/components/widgets/widgets/forest-change/glad-alerts/actions.js index ec9367502e..5deca72b84 100644 --- a/app/javascript/components/widgets/widgets/forest-change/glad-alerts/actions.js +++ b/app/javascript/components/widgets/widgets/forest-change/glad-alerts/actions.js @@ -2,7 +2,7 @@ import { fetchGladAlerts, fetchGLADLatest } from 'services/alerts'; import axios from 'axios'; export default ({ params }) => - axios.all([fetchGladAlerts(params), fetchGLADLatest()]).then( + axios.all([fetchGladAlerts(params), fetchGLADLatest(params)]).then( axios.spread((alerts, latest) => { let data = {}; if (alerts && alerts.data && latest && latest.data) { @@ -10,7 +10,10 @@ export default ({ params }) => const latestData = latest.data.data; data = { alerts: alertsData, - latest: latestData.length && latestData[0].attributes.date + latest: + latestData && + latestData.attributes && + latestData.attributes.updatedAt }; } return data; diff --git a/app/javascript/components/widgets/widgets/forest-change/glad-ranked/actions.js b/app/javascript/components/widgets/widgets/forest-change/glad-ranked/actions.js index 6d1a042c93..460fc23f64 100644 --- a/app/javascript/components/widgets/widgets/forest-change/glad-ranked/actions.js +++ b/app/javascript/components/widgets/widgets/forest-change/glad-ranked/actions.js @@ -6,7 +6,7 @@ export default ({ params }) => axios .all([ fetchGladIntersectionAlerts({ ...params }), - fetchGLADLatest(), + fetchGLADLatest(params), getMultiRegionExtent({ ...params }) ]) .then( @@ -18,7 +18,7 @@ export default ({ params }) => ? { alerts: data, extent: areas, - latest: latestData[0].attributes.date + latest: latestData.attributes && latestData.attributes.updatedAt } : {}; }) diff --git a/app/javascript/services/alerts.js b/app/javascript/services/alerts.js index 0eaa0f7e4c..a7d3753727 100644 --- a/app/javascript/services/alerts.js +++ b/app/javascript/services/alerts.js @@ -73,6 +73,30 @@ export const fetchFiresAlerts = ({ adm0, adm1, adm2, dataset }) => { return request.get(url, 3600, 'firesRequest'); }; +export const fetchFiresLatest = ({ adm1, adm2 }) => { + let fires_summary_table = FIRES_ISO_DATASET; + if (adm2) { + fires_summary_table = FIRES_ADM2_DATASET; + } else if (adm1) { + fires_summary_table = FIRES_ADM1_DATASET; + } + const url = `${REQUEST_URL}/dataset/${fires_summary_table}`; + return request.get(url, 3600, 'firesRequest').catch(error => { + console.error('Error in firesRequest:', error); + return new Promise(resolve => + resolve({ + data: { + data: { + attributes: { updatedAt: lastFriday }, + id: null, + type: 'fires-alerts' + } + } + }) + ); + }); +}; + export const fetchViirsAlerts = ({ adm0, adm1, adm2, dates }) => { const url = `${REQUEST_URL}/viirs-active-fires/${!adm2 ? 'admin/' : ''}${ QUERIES.viirsAlerts @@ -112,20 +136,24 @@ export const fetchLatestDate = url => ); }); -export const fetchGLADLatest = () => { - const url = `${REQUEST_URL}/glad-alerts/latest`; +export const fetchGLADLatest = ({ adm1, adm2 }) => { + let glad_summary_table = GLAD_ISO_DATASET; + if (adm2) { + glad_summary_table = GLAD_ADM2_DATASET; + } else if (adm1) { + glad_summary_table = GLAD_ADM1_DATASET; + } + const url = `${REQUEST_URL}/dataset/${glad_summary_table}`; return request.get(url, 3600, 'gladRequest').catch(error => { console.error('Error in gladRequest:', error); return new Promise(resolve => resolve({ data: { - data: [ - { - attributes: { date: lastFriday }, - id: null, - type: 'glad-alerts' - } - ] + data: { + attributes: { updatedAt: lastFriday }, + id: null, + type: 'glad-alerts' + } } }) );