diff --git a/composables/api_fetch.js b/composables/api_fetch.js index 86cc079..3534b3e 100644 --- a/composables/api_fetch.js +++ b/composables/api_fetch.js @@ -12,13 +12,7 @@ export function api_fetch( const { valid, error } = validate_schema(schema, body) if (!valid) { - feedback_store.add_feedback({ - type: "error", - code: 400, - route: schema.$id, - name: "Bad request", - description: error, - }) + feedback_store.add_error(400, schema.$id, "Bad request", error) throw new Error(schema.$id.concat(": ", error)) } @@ -40,13 +34,12 @@ export function api_fetch( ...request_options, onRequestError({ error }) { geode_store.stop_request() - feedback_store.add_feedback({ - type: "error", - code: error.code, - route: schema.$id, - name: error.message, - description: error.stack, - }) + feedback_store.add_error( + error.code, + schema.$id, + error.message, + error.stack, + ) if (request_error_function) { request_error_function(error) } @@ -61,13 +54,12 @@ export function api_fetch( }, onResponseError({ response }) { geode_store.stop_request() - feedback_store.add_feedback({ - type: "error", - code: response.status, - route: schema.$id, - name: response._data.name, - description: response._data.description, - }) + feedback_store.add_error( + response.status, + schema.$id, + response._data.name, + response._data.description, + ) if (response_error_function) { response_error_function(response) } diff --git a/composables/upload_file.js b/composables/upload_file.js index e14c285..f2e2174 100644 --- a/composables/upload_file.js +++ b/composables/upload_file.js @@ -22,13 +22,7 @@ export async function upload_file( ...request_options, onRequestError({ error }) { geode_store.stop_request() - feedback_store.add_feedback({ - type: "error", - code: error.code, - route: route, - name: error.message, - description: error.stack, - }) + feedback_store.add_error(error.code, route, error.message, error.stack) if (request_error_function) { request_error_function(error) } @@ -43,13 +37,12 @@ export async function upload_file( }, onResponseError({ response }) { geode_store.stop_request() - feedback_store.add_feedback({ - type: "error", - code: response.status, - route: route, - name: response._data.name, - description: response._data.description, - }) + feedback_store.add_error( + response.status, + route, + response._data.name, + response._data.description, + ) if (response_error_function) { response_error_function(response) } diff --git a/composables/viewer_call.js b/composables/viewer_call.js index 9768c6f..2be7706 100644 --- a/composables/viewer_call.js +++ b/composables/viewer_call.js @@ -8,13 +8,7 @@ export function viewer_call( const { valid, error } = validate_schema(schema, params) if (!valid) { - feedback_store.add_feedback({ - type: "error", - code: 400, - route: schema.route, - name: "Bad request", - description: error, - }) + feedback_store.add_error(400, schema.route, "Bad request", error) throw new Error(schema.route.concat(": ", error)) } @@ -42,13 +36,12 @@ export function viewer_call( }, ) .catch((error) => { - feedback_store.add_feedback({ - type: "error", - code: error.code, - route: schema.route, - name: error.message, - description: error.message, - }) + feedback_store.add_error( + error.code, + schema.route, + error.message, + error.message, + ) if (response_error_function) { response_error_function(error) } diff --git a/stores/feedback.js b/stores/feedback.js index 5b25001..9c087d4 100644 --- a/stores/feedback.js +++ b/stores/feedback.js @@ -4,8 +4,20 @@ export const use_feedback_store = defineStore("feedback", { server_error: false, }), actions: { - add_feedback(feedback) { - this.feedbacks.push(feedback) + add_error(code, route, name, description) { + this.feedbacks.push({ + type: "error", + code, + route, + name, + description, + }) + }, + add_success(description) { + this.feedbacks.push({ + type: "success", + description, + }) }, delete_feedback(feedback_index) { this.feedbacks.splice(feedback_index, 1) diff --git a/test/components/FeedBack/Snackers.nuxt.test.js b/test/components/FeedBack/Snackers.nuxt.test.js index 1b4ddfb..7cd77d7 100644 --- a/test/components/FeedBack/Snackers.nuxt.test.js +++ b/test/components/FeedBack/Snackers.nuxt.test.js @@ -33,14 +33,12 @@ describe("FeedBackSnackers.vue", async () => { console.log("wrapper", wrapper) const feedback_store = use_feedback_store() - const error = { - type: "error", - code: 404, - route: "/test", - name: "Test message", - description: "Test desription", - } - await feedback_store.add_feedback(error) + await feedback_store.add_error( + 404, + "/test", + "Test message", + "Test desription", + ) expect(feedback_store.feedbacks.length).toBe(1) const v_btn = await wrapper.findComponent(components.VBtn) console.log("v_btn", v_btn) diff --git a/test/stores/Feedback.test.js b/test/stores/Feedback.test.js index 1bcbfe4..b67171f 100644 --- a/test/stores/Feedback.test.js +++ b/test/stores/Feedback.test.js @@ -7,16 +7,17 @@ describe("Feedback store", () => { setActivePinia(createPinia()) }) - it("add_feedback", () => { + it("add_error", () => { const feedback_store = use_feedback_store() expect(feedback_store.feedbacks.length).toBe(0) - feedback_store.add_feedback({ - type: "error", - code: 500, - route: "/test", - name: "test message", - description: "test description", - }) + feedback_store.add_error(500, "/test", "test message", "test description") + expect(feedback_store.feedbacks.length).toBe(1) + }) + + it("add_success", () => { + const feedback_store = use_feedback_store() + expect(feedback_store.feedbacks.length).toBe(0) + feedback_store.add_success(500, "/test", "test message", "test description") expect(feedback_store.feedbacks.length).toBe(1) })