fulltext.js 1.28 KB
import {
    FETCH_FULLTEXT_FAILURE,
    FETCH_FULLTEXT_REQUEST,
    FETCH_FULLTEXT_SUCCESS
} from './types';
import api from 'common/api';

export default {
    state: {
        fetching: false,
        fulltexts: [],
        searchParams: {
            page: 1,
            count: 10
        },
        count: 0
    },
    mutations: {
        [FETCH_FULLTEXT_REQUEST](state, params) {
            state.fetching = true;
            state.searchParams = Object.assign(state.searchParams, params);
        },
        [FETCH_FULLTEXT_FAILURE](state) {
            state.fetching = false;
        },
        [FETCH_FULLTEXT_SUCCESS](state, params) {
            state.fetching = false;
            state.fulltexts = params.data;
            state.count = params.count;
        }
    },
    actions: {
        [FETCH_FULLTEXT_REQUEST]({commit, state}, params) {
            commit(FETCH_FULLTEXT_REQUEST, params);
            return api.get('/list', state.searchParams).then(res => {
                if (res.code === 200) {
                    commit(FETCH_FULLTEXT_SUCCESS, {data: res.data, count: res.count});
                } else {
                    commit(FETCH_FULLTEXT_FAILURE);
                }
            }, () => {
                commit(FETCH_FULLTEXT_FAILURE);
            });
        }
    }
};