import client from '../../app-shell/client'

export const fetchSettingList = query => async dispatch => {
  try {
    const data = await client.fetchSettingList()

    dispatch({
      type: 'REFRESH-SETTINGS',
      list: data.settings
    })
  } catch (error) {
    console.error(error)
    // dispatch({
    //   type: 'CLEAR-BOARD-LIST'
    // });
  }
}

export const createSetting = setting => async dispatch => {
  try {
    const data = await client.createSetting(setting)

    const createdSetting = data.createSetting

    dispatch(fetchSettingList())
  } catch (error) {
    console.error(error)
  }
}

export const updateSetting = setting => async dispatch => {
  try {
    const data = await client.updateSetting(setting)

    const updatedSetting = data.updateSetting

    dispatch(fetchSettingList())
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}

export const deleteSetting = setting => async dispatch => {
  try {
    var { name } = setting

    const data = await client.deleteSetting(name)

    const deletedSetting = data.deleteSetting

    dispatch(fetchSettingList())
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}
