6

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

谁应该处理标准化?

使用规范化数据发送成功操作之前的传奇本身?

或者路由器应该在建立新状态之前标准化日期吗?

编辑我选择在传奇中规范化并保持减速器清洁。它只是用新的活动替换了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)]

4

2 回答 2

1

最终,在这种情况下,你可以自由地做任何对你有用的事情——没有一个强有力的理由支持另一个。根据数据的结构,您最终可能会发现,在 saga 中执行此操作将使用更少的代码,因为您只需将结果拆分一次而不是两次(在关心数据的 2 个 reducer 中的每个中执行一次。但是可能是这样,也可能不是。我也喜欢在减速器中做这件事的想法,因为减速器通常应该尽可能简单,这个模型很适合。

但就像我说的那样,我不认为有一个强有力的普遍论据来支持另一个。

于 2016-03-02T21:57:15.983 回答
-1

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

- 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.
于 2021-07-09T03:56:45.297 回答