Untitled

 avatar
user_4058768
plain_text
18 days ago
5.4 kB
1
Indexable
Never
import T from 'view/js/common';
import { getSvSdhAdmin } from '../fwSinhVienSdh/redux';

const sdhDeTaiAll = 'SdhDeTai:GetAll';
export default function SdhDeTaiReducer(state = null, data) {
    switch (data.type) {
        case sdhDeTaiAll:
            return Object.assign({}, state, { page: data.payload });
        default:
            return state;
    }
}
export function createSdhDeTai(data, done) {
    const cookie = T.updatePage('SdhDetai', pageNumber, pageSize, pageCondition, filter);
    const { pageNumber, pageSize, pageCondition, filter } = cookie;
    return async dispatch => {
        try {
            const url = '/api/sdh/de-tai';
            let res = await T.async.post(url, { data });
            console.log('createSdhDeTai', res);
            T.notify('Tạo đề tài thành công!', 'success');
            dispatch(getDeTaiPage(pageNumber, pageSize, pageCondition, filter));
            done && done(res.items);
            return res.items;
        } catch (error) {
            T.notify(`Lỗi tạo mới ${error.message && (':<br>' + error.message)}`, 'danger');
            console.error(error);
        }
    };
}
export function updateSdhDeTai(id, data, done) {
    const cookie = T.updatePage('SdhDetai', pageNumber, pageSize, pageCondition, filter);
    const { pageNumber, pageSize, pageCondition, filter } = cookie;
    return async dispatch => {
        try {
            const url = '/api/sdh/de-tai';
            let res = await T.async.put(url, { id, data });
            T.notify('Cập nhật đề tài thành công!', 'success');
            dispatch(getDeTaiPage(pageNumber, pageSize, pageCondition, filter));
            done && done(res.items);
            return res.items;
        } catch (error) {
            T.notify(`Lỗi tạo mới ${error.message && (':<br>' + error.message)}`, 'danger');
            console.error(error);
        }
    };
}


T.initPage('SdhDetai');
// export function getDeTaiPage(pageNumber, pageSize, pageCondition, filter, done) {
//     const page = T.updatePage('SdhDetai', pageNumber, pageSize, pageCondition, filter);
//     return async dispatch => {
//         try {
//             const url = `/api/sdh/de-tai/page/${page.pageNumber}/${page.pageSize}`;
//             let res = await T.async.get(url, { condition: pageCondition, filter });
//             console.log('getDeTaiPage', res);
//             dispatch({ type: sdhDeTaiAll, payload: res.page });
//             done && done(res.items);
//         }
//         catch (error) {
//             T.notify(`Lỗi lấy dữ liệu ${error.message && (':<br>' + error.message)}`, 'danger');
//             console.error(error);
//         }
//     };
// }
export function getDeTaiPage(pageNumber, pageSize, pageCondition, filter, done) {
    const page = T.updatePage('SdhDetai', pageNumber, pageSize, pageCondition, filter);
    return dispatch => {
        const url = `/api/sdh/de-tai/page/${page.pageNumber}/${page.pageSize}`;
        T.get(url, { condition: pageCondition, filter }, result => {
            if (result.error) {
                T.notify(`Lỗi lấy dữ liệu ${result.error.message && (':<br>' + result.error.message)}`, 'danger');
                console.error(result.error);
            } else {
                dispatch({ type: sdhDeTaiAll, payload: result.page });
                done && done(result.items);
            }
        });
    };
}

export const SelectAdapter_FwSinhVienSDH = {
    url: '/api/sdh/sinh-vien/page/1/20',
    data: params => ({ condition: params.term }),
    processResults: response => ({
        results: response?.page?.list ? response.page.list.map(item => ({
            id: item.mssv,
            text: `${item.mssv}: ${(item.ho + ' ' + item.ten).normalizedName()}`
        })) : []
    }),
    fetchOne: (mssv, done) => (getSvSdhAdmin(mssv, item => done && done({
        id: item.mssv,
        text: `${item.msvv}: ${(item.ho + ' ' + item.ten).normalizedName()}`
    })))()
};


export const SelectAdapter_FwCbhd = {
    url: '/api/sdh/cbhd/page/1/20',
    data: params => ({ condition: params.term }),
    processResults: response => ({
        results: response?.page?.list ? response.page.list.map(item => ({
            id: `${item.shcc} - ${item.belong_to}`,
            text: `${item.shcc}: ${item.trinhDo ?? ''} ${(item.hoTen).normalizedName()} - (${item.belong_to})`,
            belong_to: `${item.belong_to}`,
            shcc: `${item.shcc}`,
        })) : []
    }),
    fetchOne: (shcc, belong_to, done) => (getCbhd(shcc, belong_to, item => done && done({
        id: item.shcc,
        text: `${item.shcc}: ${(item.ho + ' ' + item.ten).normalizedName()}`
    })))()

};

export function getCbhd(shcc, belong_to, done) {
    return () => {
        const url = `/api/sdh/cbhd/item/${shcc}?belong_to=${belong_to}`;
        T.get(url, data => {
            if (data.error) {
                T.notify('Lấy thông tin cán bộ sau đại học không thành công!', 'danger');
                console.error(`GET: ${url}.`, data.error);
            } else {
                console.log(data.item);
                done && done(data.item);
            }
        }, () => T.notify('Lấy cán bộ sau đại học bị lỗi!', 'danger'));
    };
}
Leave a Comment