我有这个动作:
import uuid from 'uuid'
import { findPaymentCategoryByName } from './Categories/selectors'
import { addPaymentCategory } from './Categories/actions'
export const addPayment = payment => (dispatch, getState) => {
const id = uuid.v1()
const { paymentCategory, ...paymentValues } = payment
let existentPaymentCategory = findPaymentCategoryByName(getState(), paymentCategory.name)
if(!existentPaymentCategory) {
existentPaymentCategory = dispatch(addPaymentCategory(paymentCategory)).payload
}
dispatch({
type: 'ADD_PAYMENT',
payload: { payment: { ...paymentValues, id, paymentCategoryId: existentPaymentCategory.id }}
})
}
他的行动会产生付款。如果该类别不存在,则会创建该类别。
但我读到在一个动作中改变两个商店并不好。所以,任何人都知道如何改进这一行动?
答案 0 :(得分:1)
您可以遵循以下原则:"告诉他人' t-Ask" 方法" findPaymentCategoryByName"可以被称为" paymentCategoryByName"如果它不存在,他自己负责创建类别。
有关TellDontAsk的更多详情: http://martinfowler.com/bliki/TellDontAsk.html
export const addPayment = payment => (dispatch, getState) => {
const id = uuid.v1()
const { paymentCategory, ...paymentValues } = payment
let existentPaymentCategory = PaymentCategoryByName(getState(), paymentCategory.name)
dispatch({
type: 'ADD_PAYMENT',
payload: { payment: { ...paymentValues, id, paymentCategoryId: existentPaymentCategory.id }}
})
}