// 1 => LOADING // 2 => DONE // 3 => ERROR import * as api from "../api/reference.js"; export default { namespaced: true, state: { search_status: 0, search_error_message: "", references: [], selected_reference: [], ordertypes: [], selected_ordertype: null }, mutations: { update_references(state, value) { state.references = Array.isArray(value) ? value : []; }, update_selected_reference(state, value) { if (Array.isArray(value)) state.selected_reference = value; else if (value && typeof value === "object") state.selected_reference = [value]; else state.selected_reference = []; }, update_ordertypes(state, value) { state.ordertypes = Array.isArray(value) ? value : []; }, update_selected_ordertype(state, value) { if (value && typeof value === "object") state.selected_ordertype = value; else state.selected_ordertype = null; }, update_search_error_message(state, msg) { state.search_error_message = msg || ""; }, update_search_status(state, status) { state.search_status = status; } }, actions: { async searchreference({ commit }, search = "") { commit("update_search_status", 1); try { const resp = await api.searchreference(search); if (!resp || resp.status !== "OK") { commit("update_search_status", 3); commit("update_search_error_message", resp ? resp.message : "Unknown error"); commit("update_references", []); return; } const records = (resp.data && Array.isArray(resp.data.records) && resp.data.records) || (Array.isArray(resp.records) && resp.records) || []; commit("update_references", records); commit("update_search_status", 2); commit("update_search_error_message", ""); } catch (e) { commit("update_search_status", 3); commit("update_search_error_message", e.message); commit("update_references", []); } }, async searchordertype({ commit }, search = "") { commit("update_search_status", 1); try { const resp = await api.searchordertype(search); if (!resp || resp.status !== "OK") { commit("update_search_status", 3); commit("update_search_error_message", resp ? resp.message : "Unknown error"); commit("update_ordertypes", []); return; } const records = (resp.data && Array.isArray(resp.data.records) && resp.data.records) || (Array.isArray(resp.records) && resp.records) || []; commit("update_ordertypes", records); commit("update_search_status", 2); commit("update_search_error_message", ""); } catch (e) { commit("update_search_status", 3); commit("update_search_error_message", e.message); commit("update_ordertypes", []); } } } };