move logic saving only selected values logic to reusable function

pull/2821/head
Iris Scholten 2018-02-16 16:59:53 -08:00
parent 81c5b800c5
commit c51b534d32
1 changed files with 14 additions and 8 deletions

View File

@ -199,13 +199,7 @@ export const putDashboard = dashboard => async dispatch => {
try {
// for server, template var values should be all values for csv
// and should be only the selected value for non csv types
const templates = dashboard.templates.map(template => {
const values =
template.type === 'csv'
? template.values
: [template.values.find(val => val.selected)] || []
return {...template, values}
})
const templates = dashboardWithOnlySelectedTemplateValues(dashboard)
const {data} = await updateDashboardAJAX({...dashboard, templates})
// updateDashboardAJAX removed the values for the template variables
// when saving to the server
@ -216,11 +210,23 @@ export const putDashboard = dashboard => async dispatch => {
}
}
const dashboardWithOnlySelectedTemplateValues = dashboard => {
const templates = dashboard.templates.map(template => {
const values =
template.type === 'csv'
? template.values
: [template.values.find(val => val.selected)] || []
return {...template, values}
})
return templates
}
export const putDashboardByID = dashboardID => async (dispatch, getState) => {
try {
const {dashboardUI: {dashboards}} = getState()
const dashboard = dashboards.find(d => d.id === +dashboardID)
await updateDashboardAJAX(dashboard)
const templates = dashboardWithOnlySelectedTemplateValues(dashboard)
await updateDashboardAJAX({...dashboard, templates})
} catch (error) {
console.error(error)
dispatch(errorThrown(error))