我正在为 WordPress 后端 (REST) 使用 React 和 Redux(带有 promise 中间件和 thunk)构建前端。
现在我正在使用 Redux 进行简单的调度以获取页面数据,例如:
export const fetchPostBySlug = (slug) => {
return {
type: "FETCH_MODULES",
payload: axios.get(`${REST_URL}/wp/v2/posts?slug=${slug}`)
}
}
现在我想做一个更复杂的调用。我想从 WordPress 中获取所有标签并返回这些标签。然后我获取所有特色标签。然后我获取所有具有特色标签的帖子。然后我获取所有具有特色标签的自定义帖子类型。
理想情况下,我的有效负载最终看起来像这样:
payload: {
id: 5,
name: 'tag_5',
slug: '/tag_5',
tags: [all tags]
posts: [all posts and cpt having a featured tag in here together]
}
这就是我现在所拥有的,它 fetches 基本上会获取所有内容,但还没有连接和分派。
const fetchTagPosts = () => {
axios.all([fetchAllTags(), fetchFeaturedTopics()])
.then(axios.spread((all, featured) => {
let payload = {}
const ft = featured.data.featured_tags
const ft_length = ft.length
ft.map((x, i) => {
getPostsByTag(x.term_id).then(y => payload = addPostToPayload(y.data, x.term_id, payload))
getCPTByTag(x.term_id).then(y => payload = addPostToPayload(y.data, x.term_id, payload))
// I need to check here if both are loaded, so I can concat the arrays
if (ft_length === i + 1) {
// I need to dispatch/return the payload here
}
})
}))
}
const addPostToPayload = (p, id, payload) => {
return {
[id]: {
posts: payload[id] ? payload[id].posts.concat(p) : p
}
}
}