283 lines
8.8 KiB
JavaScript
283 lines
8.8 KiB
JavaScript
// 1 => LOADING
|
|
// 2 => DONE
|
|
// 3 => ERROR
|
|
import * as api from "../api/counterService.js";
|
|
export default {
|
|
namespaced: true,
|
|
state: {
|
|
counter_service: [],
|
|
location: [],
|
|
service: [],
|
|
search_status: false,
|
|
lookup_counter_service: 2,
|
|
current_page: 1,
|
|
x_search: "",
|
|
total: 0,
|
|
total_filter: 0,
|
|
selected_item: {},
|
|
error_message: "",
|
|
success_message: "",
|
|
selected_item: {},
|
|
dialog_error: false,
|
|
snackbar: false,
|
|
dialog_form: false,
|
|
save_status: false,
|
|
last_id: -1,
|
|
ip_address: "0",
|
|
sorting: {
|
|
sortBy: "counterID",
|
|
type: "asc",
|
|
},
|
|
},
|
|
mutations: {
|
|
update_counter_service(state, val) {
|
|
state.counter_service = val.records;
|
|
state.total = val.total;
|
|
state.total_filter = val.total_filter;
|
|
},
|
|
update_search_status(state, val) {
|
|
state.search_status = val;
|
|
},
|
|
update_lookup_counter_service(state, val) {
|
|
state.lookup_counter_service = val;
|
|
},
|
|
update_error_message(state, val) {
|
|
state.error_message = val;
|
|
},
|
|
update_sorting(state, val) {
|
|
state.sorting = val;
|
|
},
|
|
update_selected_item(state, val) {
|
|
state.selected_item = val;
|
|
},
|
|
update_current_page(state, val) {
|
|
state.current_page = val;
|
|
},
|
|
update_x_search(state, val) {
|
|
state.x_search = val;
|
|
state.current_page = 1;
|
|
},
|
|
update_dialog_error(state, val) {
|
|
state.dialog_error = val;
|
|
},
|
|
update_snackbar(state, val) {
|
|
state.snackbar = val;
|
|
},
|
|
update_dialog_form(state, val) {
|
|
state.dialog_form = val;
|
|
},
|
|
update_success_message(state, val) {
|
|
state.success_message = val;
|
|
},
|
|
update_location(state, val) {
|
|
state.location = val.records;
|
|
},
|
|
update_service(state, val) {
|
|
state.service = val.records;
|
|
},
|
|
update_save_status(state, val) {
|
|
state.save_status = val;
|
|
},
|
|
update_last_id(state, val) {
|
|
state.last_id = val;
|
|
},
|
|
update_ip_address(state, val) {
|
|
state.ip_address = val;
|
|
},
|
|
},
|
|
actions: {
|
|
async lookup(context) {
|
|
context.commit("update_lookup_counter_service", 1);
|
|
context.commit("update_search_status", true);
|
|
try {
|
|
var tkn = JSON.stringify({
|
|
token: one_token(),
|
|
page: context.state.current_page,
|
|
name: context.state.x_search,
|
|
order_by: context.state.sorting.sortBy,
|
|
order: context.state.sorting.type,
|
|
});
|
|
let resp = await api.search(tkn);
|
|
if (resp.status != "OK") {
|
|
context.commit("update_search_status", false);
|
|
context.commit("update_lookup_counter_service", 3);
|
|
context.commit("update_error_message", resp.message);
|
|
console.log(resp.message);
|
|
} else {
|
|
context.commit("update_search_status", false);
|
|
context.commit("update_lookup_counter_service", 2);
|
|
let data = resp.data;
|
|
// console.log(resp.data);
|
|
context.commit("update_ip_address", resp.data.ip_address);
|
|
|
|
// if (
|
|
// context.selected_item !== undefined &&
|
|
// Object.keys(context.selected_item).length !== 0 &&
|
|
// selected_item.constructor === Object
|
|
// ) {
|
|
// id = context.selected_item.id;
|
|
// let idx = _.findIndex(resp.data.records, function (o) {
|
|
// return o.id == id;
|
|
// });
|
|
|
|
// if (idx !== undefined) {
|
|
// context.commit("update_selected_item", resp.data.records[idx]);
|
|
// }
|
|
// }
|
|
|
|
if (context.state.last_id != -1) {
|
|
let idx = _.findIndex(resp.data.records, function (o) {
|
|
return o.id == context.state.last_id;
|
|
});
|
|
context.commit("update_selected_item", resp.data.records[idx]);
|
|
}
|
|
|
|
context.commit("update_counter_service", data);
|
|
context.commit("update_last_id", -1);
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_search_status", false);
|
|
context.commit("update_lookup_counter_service", 3);
|
|
context.commit("update_error_message", e);
|
|
console.log(e);
|
|
}
|
|
},
|
|
async getLocation(context) {
|
|
try {
|
|
var tkn = {
|
|
token: one_token(),
|
|
};
|
|
let resp = await api.getLocation(tkn);
|
|
if (resp.status != "OK") {
|
|
context.commit("update_error_message", resp.message);
|
|
console.log(resp.message);
|
|
} else {
|
|
let data = resp.data;
|
|
|
|
context.commit("update_location", data);
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_error_message", e);
|
|
console.log(e);
|
|
}
|
|
},
|
|
async getService(context) {
|
|
try {
|
|
var tkn = {
|
|
token: one_token(),
|
|
};
|
|
let resp = await api.getService(tkn);
|
|
if (resp.status != "OK") {
|
|
context.commit("update_error_message", resp.message);
|
|
console.log(resp.message);
|
|
} else {
|
|
console.log(resp);
|
|
let data = resp.data;
|
|
|
|
context.commit("update_service", data);
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_error_message", e);
|
|
console.log(e);
|
|
}
|
|
},
|
|
async addData(context, prm) {
|
|
context.commit("update_save_status", true);
|
|
try {
|
|
prm.token = one_token();
|
|
let resp = await api.addData(prm);
|
|
if (resp.status != "OK") {
|
|
context.commit("update_save_status", false);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_dialog_error", true);
|
|
context.commit("update_error_message", resp.message);
|
|
console.log(resp.message);
|
|
} else {
|
|
context.commit("update_save_status", false);
|
|
let data = resp.data;
|
|
|
|
context.dispatch("lookup");
|
|
|
|
context.commit(
|
|
"update_success_message",
|
|
"Counter " + prm.code + " Sudah berhasil tersimpan"
|
|
);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_snackbar", true);
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_save_status", false);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_dialog_error", true);
|
|
context.commit("update_error_message", e);
|
|
console.log(e);
|
|
}
|
|
},
|
|
async editData(context, prm) {
|
|
context.commit("update_save_status", true);
|
|
try {
|
|
prm.token = one_token();
|
|
let resp = await api.editData(prm);
|
|
if (resp.status != "OK") {
|
|
context.commit("update_save_status", false);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_dialog_error", true);
|
|
context.commit("update_error_message", resp.message);
|
|
console.log(resp.message);
|
|
} else {
|
|
context.commit("update_save_status", false);
|
|
let data = resp.data;
|
|
|
|
context.dispatch("lookup");
|
|
|
|
context.commit(
|
|
"update_success_message",
|
|
"Counter " + prm.code + " Sudah berhasil tersimpan"
|
|
);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_snackbar", true);
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_save_status", false);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_dialog_error", true);
|
|
context.commit("update_error_message", e);
|
|
console.log(e);
|
|
}
|
|
},
|
|
async deleteData(context, prm) {
|
|
context.commit("update_save_status", true);
|
|
try {
|
|
prm.token = one_token();
|
|
let resp = await api.deleteData(prm);
|
|
if (resp.status != "OK") {
|
|
context.commit("update_save_status", false);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_dialog_error", true);
|
|
context.commit("update_error_message", resp.message);
|
|
console.log(resp.message);
|
|
} else {
|
|
context.commit("update_save_status", false);
|
|
let data = resp.data;
|
|
context.commit("update_selected_item", {});
|
|
|
|
context.dispatch("lookup");
|
|
|
|
context.commit(
|
|
"update_success_message",
|
|
"Counter " + prm.code + " Sudah berhasil dihapus"
|
|
);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_snackbar", true);
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_save_status", false);
|
|
context.commit("update_dialog_form", false);
|
|
context.commit("update_dialog_error", true);
|
|
context.commit("update_error_message", e);
|
|
console.log(e);
|
|
}
|
|
},
|
|
},
|
|
};
|