/** @format */

import * as React from 'react';
import Axios from '../utils/xhr';
import {GET_ALL_ERRORCODE} from '../const/path';

//错误码得
export const useCode = () => {
    const [state, setState] = React.useState({
        loading: false,
        codeList: [],
    });
    React.useEffect(() => {
        setState(preState => ({...preState, loading: true}));
        Axios({
            url: GET_ALL_ERRORCODE,
            params: {
                is_all: true,
            },
        })
            .then(({code, data}) => {
                if (code === 0) {
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        codeList: data.error_code_list.map((element: {code: string; id: number; msg: string}) => {
                            return {
                                key: element.id,
                                value: element.id,
                                label: `${element.code}(${element.msg})`,
                            };
                        }),
                    }));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false}));
            });
    }, []);
    return [state] as const;
};
