From fa165afd612e01c1f4aa93153eb2432c76d90d66 Mon Sep 17 00:00:00 2001 From: JulienChampagnol Date: Wed, 21 Aug 2024 17:50:14 +0200 Subject: [PATCH 1/3] fix(feedbacks): different function creators for errors & success --- composables/api_fetch.js | 34 +++++++------------ composables/upload_file.js | 26 +++++++------- composables/viewer_call.js | 21 ++++-------- stores/feedback.js | 17 ++++++++-- .../components/FeedBack/Snackers.nuxt.test.js | 9 +---- test/stores/Feedback.test.js | 17 +++++----- 6 files changed, 57 insertions(+), 67 deletions(-) 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..10c7a5c 100644 --- a/composables/upload_file.js +++ b/composables/upload_file.js @@ -22,13 +22,12 @@ 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 +42,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..b56eb28 100644 --- a/stores/feedback.js +++ b/stores/feedback.js @@ -4,8 +4,21 @@ 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..707df8a 100644 --- a/test/components/FeedBack/Snackers.nuxt.test.js +++ b/test/components/FeedBack/Snackers.nuxt.test.js @@ -33,14 +33,7 @@ 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) }) From 0ea3b28debd71b1df25cf4397b2404330df9d87d Mon Sep 17 00:00:00 2001 From: JulienChampagnol Date: Wed, 21 Aug 2024 18:01:25 +0200 Subject: [PATCH 2/3] prettier --- composables/upload_file.js | 7 +------ stores/feedback.js | 2 +- test/components/FeedBack/Snackers.nuxt.test.js | 7 ++++++- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/composables/upload_file.js b/composables/upload_file.js index 10c7a5c..f2e2174 100644 --- a/composables/upload_file.js +++ b/composables/upload_file.js @@ -22,12 +22,7 @@ export async function upload_file( ...request_options, onRequestError({ error }) { geode_store.stop_request() - feedback_store.add_error( - error.code, - route, - error.message, - error.stack - ) + feedback_store.add_error(error.code, route, error.message, error.stack) if (request_error_function) { request_error_function(error) } diff --git a/stores/feedback.js b/stores/feedback.js index b56eb28..2b37a86 100644 --- a/stores/feedback.js +++ b/stores/feedback.js @@ -14,7 +14,7 @@ export const use_feedback_store = defineStore("feedback", { }) }, - add_success( description) { + add_success(description) { this.feedbacks.push({ type: "success", description, diff --git a/test/components/FeedBack/Snackers.nuxt.test.js b/test/components/FeedBack/Snackers.nuxt.test.js index 707df8a..7cd77d7 100644 --- a/test/components/FeedBack/Snackers.nuxt.test.js +++ b/test/components/FeedBack/Snackers.nuxt.test.js @@ -33,7 +33,12 @@ describe("FeedBackSnackers.vue", async () => { console.log("wrapper", wrapper) const feedback_store = use_feedback_store() - await feedback_store.add_error(404, "/test", "Test message", "Test desription") + 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) From 0beb7073dd89d5c3db8809cae6fd5e434300b798 Mon Sep 17 00:00:00 2001 From: JulienChampagnol Date: Wed, 21 Aug 2024 18:02:20 +0200 Subject: [PATCH 3/3] prettier 2 --- stores/feedback.js | 1 - 1 file changed, 1 deletion(-) diff --git a/stores/feedback.js b/stores/feedback.js index 2b37a86..9c087d4 100644 --- a/stores/feedback.js +++ b/stores/feedback.js @@ -13,7 +13,6 @@ export const use_feedback_store = defineStore("feedback", { description, }) }, - add_success(description) { this.feedbacks.push({ type: "success",