处理redux-saga或reducer中下载的项目规范化?

时间:2016-03-02 20:39:27

标签: reactjs redux redux-saga

我从删除API获取的数据不是我的应用可以处理的格式。 我的传奇下载数据。

谁应该处理规范化?

在使用规范化数据调度成功操作之前,saga本身?

或者路由器应该在构建新状态之前规范日期吗?

编辑我选择在传奇中进行规范化并保持减速器清洁。它只是用新的activitiesUpdated替换了活动。

减速机

export default function account(state = ACCOUNT, action) {
  switch (action.type) {
    case "account/LOGIN_SUCCESS":
      const { access_token, user } = action
      return { ...state, user, access_token, authenticated: true, error: "" }
    case "account/LOGOUT_SUCCESS":
      return ACCOUNT
    case "account/LOGIN_ERROR":
      return { ...state, error: action.error }
    case "account/ACTIVITIES_UPDATED":
      return { ...state, activities: action.activities }
    default:
      return state
  }
}

这些是传奇:

function sortActivities(activities) {
  return action.activities.sort((a,b) => b.timestamp.localeCompare(a.timestamp))
}

function addInvoices(activities) {
  let lastYearMonth, invoiceItem
  return activities.reduce((state, item, index) => {
    const currentYearMonth = item.timestamp.substr(0,7)
    if (currentYearMonth != lastYearMonth) {
      lastYearMonth = currentYearMonth
      invoiceItem = {
        id: currentYearMonth,
        type: "invoice",
        parking: 0,
        rebates: 0,
        sum: 0,
        timestamp: currentYearMonth
      }
      state.push(invoiceItem)
    }
    const amount = Math.abs(Number(item.gross_amount))
    if (item.type == "parking") {
      invoiceItem.parking += amount
      invoiceItem.sum -= amount
    } else if (item.type == "rebate" || item.type == "surplus") {
      invoiceItem.rebates += amount
      invoiceItem.sum += amount
    }
    state.push(item)
    return state
  }, [])
}

function *getActivities(access_token) {
  console.info("fetch activities")
  try {
    const activities = yield call(getActivitiesAsync, access_token)
    console.info("activities fetched")
    yield put(activitiesUpdated(addInvoices(activities.sortActivities(activities))))
  } catch (error) {
  }
}

function *updateActivities() {
  while (true) {
    const { access_token } = yield take(LOGIN_SUCCESS)
    console.info("Calling getActivities")
    yield call(getActivities, access_token)
    while (true) {
      const {type } = yield take([REFRESH_ACTIVITIES, LOGOUT])
      if (type == LOGOUT) {
        break
      }
      yield call(getActivities, access_token)
    }
  }
}

当你想到updateActivities传奇中的双包裹while循环时?

也是正确的

yield take([REFRESH_ACTIVITIES, LOGOUT])

只是

的快捷方式

yield race[take(REFRESH_ACTIVITIES), take(LOGOUT)]

2 个答案:

答案 0 :(得分:1)

在这种情况下,你最终可以自由地为你做任何事情 - 对于一个人来说,没有一个强有力的理由。你最终可能会发现,根据数据的结构,在saga中执行它会产生更少的代码,因为你只需要将结果分成两次(两个减少器中的每一个都关注数据)但是这可能是也可能不是这样的。我也喜欢在减速器中这样做的想法,因为减速器通常应该尽可能简单,并且这个模型适合它。

但就像我说的那样,我认为对于一方而言,这是一个强烈的一般性论证。

答案 1 :(得分:-1)

您也可以使用 createSelector 进行数据规范化,以保持 saga 干净:

- Selectors can compute derived data, allowing Redux to store the minimal possible state.
- Selectors are efficient. A selector is not recomputed unless one of its arguments changes.
- Selectors are composable. They can be used as input to other selectors.