index.js 3.89 KB
import {
    FETCH_HOME_REQUEST,
    FETCH_HOME_FAILURE,
    FETCH_HOME_SUCCESS,
    FETCH_CHANNEL_REQUEST,
    FETCH_CHANNEL_SUCCESS,
    FETCH_CHANNEL_FAILURE,
    FETCH_ABOUT_REQUEST,
    FETCH_ABOUT_SUCCESS,
    FETCH_ABOUT_FAILURE
} from './types';
import {
    HOME_CONTENT_CODE,
    CHANNEL_MAN_CONTENT_CODE,
    CHANNEL_WOMEN_CONTENT_CODE,
    ABOUT_CONTENT_CODE
} from '../content-code';

export default {
    state: {
        home: {},
        about: {},
        gender: 'men',
        men: {
            sort: {},
            floors: {},
            isFetching: false
        },
        women: {
            sort: {},
            floors: {},
            isFetching: false
        },
        fethingHome: false,
        fetchingAbout: false,
    },
    mutations: {
        [FETCH_HOME_REQUEST](state) {
            state.fethingHome = true;
        },
        [FETCH_HOME_FAILURE](state) {
            state.fethingHome = false;
        },
        [FETCH_HOME_SUCCESS](state, {data}) {
            state.fethingHome = false;
            state.home = data;
        },
        [FETCH_ABOUT_REQUEST](state) {
            state.fetchingAbout = true;
        },
        [FETCH_ABOUT_FAILURE](state) {
            state.fetchingAbout = false;
        },
        [FETCH_ABOUT_SUCCESS](state, {data}) {
            state.fetchingAbout = false;
            state.about = data[0];
        },
        [FETCH_CHANNEL_REQUEST](state, gender) {
            state.gender = gender;
            state[gender].isFetching = true;
        },
        [FETCH_CHANNEL_FAILURE](state) {
            state[state.gender].isFetching = false;
        },
        [FETCH_CHANNEL_SUCCESS](state, {data, sort}) {
            const keyCvt = {
                men: 'MEN男士',
                women: 'WOMEN女士',
            };

            state[state.gender].isFetching = false;

            // mock data
            data.splice(1, 0 , {
                template_name: 'category',
                data: {
                    gender: 'men',
                    sort: sort[keyCvt[state.gender]]

                }
            });
            state[state.gender].floors = data;
            state[state.gender].sort = sort[keyCvt[state.gender]];
        },
    },
    actions: {
        async [FETCH_HOME_REQUEST]({commit, state}) {
            // if (!_.isEmpty(state.home)) {
            //     return Promise.resolve(state.home);
            // }
            commit(FETCH_HOME_REQUEST);
            try {
                const result = await this.$api.get('/api/resource/get', {content_code: HOME_CONTENT_CODE});

                commit(FETCH_HOME_SUCCESS, result);
                return result;
            } catch (e) {
                console.error(e);
                commit(FETCH_HOME_FAILURE);
            }
        },
        async [FETCH_CHANNEL_REQUEST]({commit}, gender) {
            commit(FETCH_CHANNEL_REQUEST, gender);

            try {
                const result = await this.$api.get('/api/resource/get', {
                    content_code: gender === 'men' ? CHANNEL_MAN_CONTENT_CODE : CHANNEL_WOMEN_CONTENT_CODE
                });

                const sort = await this.$api.get('/sort/list', {
                    channel: gender
                });

                commit(FETCH_CHANNEL_SUCCESS, {data: result.data, sort: sort.data});
                return result;
            } catch (e) {
                console.error(e);
                commit(FETCH_CHANNEL_FAILURE);
            }
        },
        async [FETCH_ABOUT_REQUEST]({commit}, gender) {
            commit(FETCH_ABOUT_REQUEST, gender);

            try {
                const result = await this.$api.get('/api/resource/get', {
                    content_code: ABOUT_CONTENT_CODE
                });

                commit(FETCH_ABOUT_SUCCESS, {data: result.data});
                return result;
            } catch (e) {
                commit(FETCH_ABOUT_FAILURE);
            }
        }
    }
};