Redux вложенные Thunks, выборка из двух разных конечных точек - PullRequest
0 голосов
/ 19 июня 2019

Я хотел бы объединить мои thunks, чтобы получить исходные данные для моего приложения из wordpress, а затем получить tags из другой конечной точки.

Вот мое первое действие, чтобы получить мои posts (видео) данные JSON:

export function fetchData() {
    const url = "http://app.rguc.co.uk/?rest_route=/wp/v2/posts";

    return dispatch => {
        fetch(url)
            .then(response => {
                return response;
            })
            .then(response => response.json())
            .then(data => {
                if (data) {
                    dispatch(fetchSuccessVideos(data));
                } else {
                    dispatch(fetchErrorVideos(data));
                }
            })
            .catch(error => {
                console.log(error);
            });
    };
}

Для каждого сообщения я хотел бы получить имена тегов, используя массив тегов из сообщения json:

tags: [12]

export function getTag(id) {
    const url = "http://app.rguc.co.uk/?rest_route=/wp/v2/tags/" + id;
    return dispatch => {
        fetch(url)
            .then(response => {
                return response;
            })
            .then(response => response.json())
            .then(data => {
                if (data) {
                    dispatch(fetchSuccessTags(data));
                } else {
                    dispatch(fetchErrorTags(data));
                }
            })
            .catch(error => {
                console.log(error);
            });
    };
}

Когда я объединяю их, как мне запускать fetch для каждого поста?

export function getDataAndTag() {

    return (dispatch, getState) => {

        return dispatch(fetchData()).then(() => {

            // For each post get tag names
            return dispatch(getTag(tagId));
        });
    };
}
...