操作和还原结构应该如何使来自2个单独API的数据可以显示在同一页面上?

发布于 2025-02-07 08:53:28 字数 2369 浏览 0 评论 0原文

action.js

export const getSeverityDistData = (dispatch, urlqueryStr) => {
handleFetch(
    {
        ...getSeverityDist(),
        urlqueryStr,
    }, // fetch params
    (res) => {
        dispatch({
            type: ActionTypes.GET_DASHBOARD_LIST_SUCCESS,
            payload: {
                data: res.data,
                isLoading: false,
                onError: false,
            },
        });
    },
    (e) => {
        dispatch({
            type: ActionTypes.GET_DASHBOARD_LIST_ERROR,
            payload: {
                errorMsg: e.message,
                isLoading: false,
            },
        });
    },
    (loading) => {
        dispatch({
            type: ActionTypes.GET_DASHBOARD_LIST_LOADING,
            payload: loading,
        });
    }
);};
export const getGroupData = (dispatch, urlqueryStr) => {
handleFetch(
{
    ...getGruop(),
    urlqueryStr,
},//...sameOperations SameActionTypes...}

reducers.js



const setChartsData = (state, action) => ({
    ...state,
    data: [...action.payload.data],
    isLoading: action.payload.isLoading,
    onError: action.payload.onError,
});

const setLoading = (state, action) => ({
    ...state,
    isLoading: action.payload,
});

const setTimeOut = (state, action) => ({
    ...state,
    isTimeout: action.payload,
});

const onError = (state, action) => ({
    ...state,
    onError: action.payload.errorMsg,
    isLoading: action.payload.isLoading,
});

const initialState = {
    data: [],
    isLoading: false,
    isTimeout: false,
    onError: '',
};

export default (state = initialState, action) => {
    switch (action.type) {
        case ActionTypes.GET_DASHBOARD_LIST_SUCCESS:
            return setChartsData(state, action);
        case ActionTypes.GET_DASHBOARD_LIST_LOADING:
            return setLoading(state, action);
        case ActionTypes.GET_DASHBOARD_LIST_TIME_OUT:
            return setTimeOut(state, action);
        case ActionTypes.GET_DASHBOARD_LIST_ERROR:
            return onError(state, action);
        default:
            return state;
    }
};

让我简要告诉你我想做什么。 考虑一页上的2个图表。这些图表的数据来自不同的API,但根据UI的相同过滤器。 例如,用户将从“过滤器”部分中选择某些参数,并且根据此过滤状态将查询发送到API,每个API将返回其自己的数据,这些不同的数据将在同一图上以不同的图表显示页。我要做的是通过图将数据与API分开。当我按照上面写的交易进行交易时,我会从商店中提取的值更改。我猜哪个出现的覆盖物。我该如何摆脱这种情况?

actions.js

export const getSeverityDistData = (dispatch, urlqueryStr) => {
handleFetch(
    {
        ...getSeverityDist(),
        urlqueryStr,
    }, // fetch params
    (res) => {
        dispatch({
            type: ActionTypes.GET_DASHBOARD_LIST_SUCCESS,
            payload: {
                data: res.data,
                isLoading: false,
                onError: false,
            },
        });
    },
    (e) => {
        dispatch({
            type: ActionTypes.GET_DASHBOARD_LIST_ERROR,
            payload: {
                errorMsg: e.message,
                isLoading: false,
            },
        });
    },
    (loading) => {
        dispatch({
            type: ActionTypes.GET_DASHBOARD_LIST_LOADING,
            payload: loading,
        });
    }
);};
export const getGroupData = (dispatch, urlqueryStr) => {
handleFetch(
{
    ...getGruop(),
    urlqueryStr,
},//...sameOperations SameActionTypes...}

reducers.js



const setChartsData = (state, action) => ({
    ...state,
    data: [...action.payload.data],
    isLoading: action.payload.isLoading,
    onError: action.payload.onError,
});

const setLoading = (state, action) => ({
    ...state,
    isLoading: action.payload,
});

const setTimeOut = (state, action) => ({
    ...state,
    isTimeout: action.payload,
});

const onError = (state, action) => ({
    ...state,
    onError: action.payload.errorMsg,
    isLoading: action.payload.isLoading,
});

const initialState = {
    data: [],
    isLoading: false,
    isTimeout: false,
    onError: '',
};

export default (state = initialState, action) => {
    switch (action.type) {
        case ActionTypes.GET_DASHBOARD_LIST_SUCCESS:
            return setChartsData(state, action);
        case ActionTypes.GET_DASHBOARD_LIST_LOADING:
            return setLoading(state, action);
        case ActionTypes.GET_DASHBOARD_LIST_TIME_OUT:
            return setTimeOut(state, action);
        case ActionTypes.GET_DASHBOARD_LIST_ERROR:
            return onError(state, action);
        default:
            return state;
    }
};

Let me tell you briefly what I want to do.
Consider 2 charts on one page. The data coming to these charts is from different api but according to the same filter by the ui.
For example, the user will select certain parameters from the filter section and a query will be sent to the APIs according to this filter status, each API will return a data of its own and these different data will be displayed in different graphs on the same page. What I want to do is to separate the data from the API by graphs. When I make a transaction as I wrote above, the values I withdraw from the store change. I guess whichever one comes up overwrites the other. How can I get out of this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文