// 1 => LOADING // 2 => DONE // 3 => ERROR import * as api from "../api/courier.js" export default { namespaced: true, state: { couriers: [], loading: false, name: "", curr_page: 1, total: 0, error_dialog: false, error_message: "", is_courier: [], snackbar: false, snackbar_msg: "" // selected_courier: {}, // current_page: 1, // last_id: -1, // x_search: "", // search_status: 0, // // dropdown courier // namecouriers: [], // namecourier: {}, // autocomplete_status: 0, // save_status: 0, // error_message: "", // dialog_error: false, // alert_error: false, // msg_success: "", // alert_success: false, // get_data_status:0, // filter_branch: [], // selected_filter_branch: {}, // filter_kurir: [], // selected_filter_kurir: {}, // filter_status: [], // selected_filter_status: {}, }, mutations: { update_couriers(state, val) { state.couriers = val }, update_total(state, val) { state.total = val; }, update_loading(state, val) { state.loading = val; }, update_error_message(state, val) { state.error_message = val; }, update_name(state, val) { state.name = val; }, update_curr_page(state, val) { state.curr_page = val; }, update_is_courier(state, val) { state.is_courier = val; }, update_snackbar(state, val) { state.snackbar = val; }, update_snackbar_msg(state, val) { state.snackbar_msg = val; }, update_error_dialog(state, val) { state.error_dialog = val; }, }, actions: { async search(context) { context.commit("update_loading", true) try { // prm.token = one_token(); var param = { "name": context.state.name, "page": context.state.curr_page, "token": one_token(), } let resp = await api.search(param); if (resp.status !== "OK") { context.commit("update_loading", false); context.commit("update_error_dialog", true); context.commit("update_error_message", resp); } else { context.commit("update_loading", false); console.log(resp.data); context.commit("update_couriers", resp.data.records) context.commit("update_total", resp.data.total); let isCourier = []; for (let i = 0; i < resp.data.records.length; i++) { if (resp.data.records[i].flag !== "N") { isCourier.push(resp.data.records[i].userID) } } context.commit("update_is_courier", isCourier); } } catch (error) { context.commit("update_loading", false); context.commit("update_error_dialog", true); context.commit("update_error_message", error); } }, async add(context, prm) { context.commit("update_loading", true) try { prm.token = one_token(); // prm.userID = one_user(); let resp = await api.add(prm); if (resp.status !== "OK") { context.commit("update_loading", false); context.commit("update_error_message", resp); context.commit("update_error_dialog", true); } else { context.commit("update_loading", false); context.commit("update_snackbar", true); context.commit("update_snackbar_msg", "Berhasil Menambahkan Kurir"); console.log(resp.data); // var tempCourrier = JSON.stringify(context.state.is_courier); // var arrCourier = JSON.parse(tempCourrier); // arrCourier.push(prm.userID); // context.commit("update_is_courier", arrCourier); context.dispatch("search"); } } catch (error) { context.commit("update_loading", false); context.commit("update_error_message", error); context.commit("update_error_dialog", true); } }, async deleteData(context, prm) { context.commit("update_loading", true) try { prm.token = one_token(); // prm.userID = one_user(); let resp = await api.deleteData(prm); if (resp.status !== "OK") { context.commit("update_loading", false); context.commit("update_error_message", resp); context.commit("update_error_dialog", true); } else { context.commit("update_loading", false); console.log(resp.data); context.commit("update_snackbar", true); context.commit("update_snackbar_msg", "Berhasil Menghapus Kurir"); // var tempCourrier = JSON.stringify(context.state.is_courier); // var arrCourier = JSON.parse(tempCourrier); // arrCourier.filter((e) => e === prm.courierID); // context.commit("update_is_courier", arrCourier); context.dispatch("search"); } } catch (error) { context.commit("update_loading", false); context.commit("update_error_message", error); context.commit("update_error_dialog", true); } } } }