From db47c1f10c65192d539410587b89aa952c856b35 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 27 Feb 2022 17:21:07 +0100 Subject: [PATCH] chore: move converting params to service --- src/helpers/case.js | 4 ---- src/services/abstractService.js | 29 +++++++++++++++++++++++++++-- src/views/tasks/ShowTasks.vue | 3 +-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/helpers/case.js b/src/helpers/case.js index 392c7cf3..6246f7cf 100644 --- a/src/helpers/case.js +++ b/src/helpers/case.js @@ -45,10 +45,6 @@ export function objectToCamelCase(object) { */ export function objectToSnakeCase(object) { - if (object instanceof Date) { - return object.toISOString() - } - // When calling recursively, this can be called without being and object or array in which case we just return the value if (typeof object !== 'object') { return object diff --git a/src/services/abstractService.js b/src/services/abstractService.js index a28334f3..4dc0bd6b 100644 --- a/src/services/abstractService.js +++ b/src/services/abstractService.js @@ -2,6 +2,31 @@ import axios from 'axios' import {objectToSnakeCase} from '@/helpers/case' import {getToken} from '@/helpers/auth' +function convertObject(o) { + if (o instanceof Date) { + return o.toISOString() + } + + return o +} + +function prepareParams(params) { + if (typeof params !== 'object') { + return params + } + + for (const p in params) { + if (Array.isArray(params[p])) { + params[p] = params[p].map(convertObject) + continue + } + + params[p] = convertObject(params[p]) + } + + return objectToSnakeCase(params) +} + export default class AbstractService { ///////////////////////////// @@ -292,7 +317,7 @@ export default class AbstractService { const finalUrl = this.getReplacedRoute(url, model) try { - const response = await this.http.get(finalUrl, {params}) + const response = await this.http.get(finalUrl, {params: prepareParams(params)}) const result = this.modelGetFactory(response.data) result.maxRight = Number(response.headers['x-max-right']) return result @@ -331,7 +356,7 @@ export default class AbstractService { const finalUrl = this.getReplacedRoute(this.paths.getAll, model) try { - const response = await this.http.get(finalUrl, {params: params}) + const response = await this.http.get(finalUrl, {params: prepareParams(params)}) this.resultCount = Number(response.headers['x-pagination-result-count']) this.totalPages = Number(response.headers['x-pagination-total-pages']) diff --git a/src/views/tasks/ShowTasks.vue b/src/views/tasks/ShowTasks.vue index 28cdc212..71e16476 100644 --- a/src/views/tasks/ShowTasks.vue +++ b/src/views/tasks/ShowTasks.vue @@ -53,7 +53,6 @@ import {useI18n} from 'vue-i18n' import TaskModel from '@/models/task' import {formatDate} from '@/helpers/time/formatDate' import {setTitle} from '@/helpers/setTitle' -import {objectToSnakeCase} from '@/helpers/case' import Fancycheckbox from '@/components/input/fancycheckbox.vue' import SingleTaskInList from '@/components/tasks/partials/singleTaskInList.vue' @@ -202,7 +201,7 @@ async function loadPendingTasks(from: string, to: string) { } } - tasks.value = await store.dispatch('tasks/loadTasks', objectToSnakeCase(params)) + tasks.value = await store.dispatch('tasks/loadTasks', params) } // FIXME: this modification should happen in the store