From d57ffcbc4bcf4fdaa414739838ce2377ca65a480 Mon Sep 17 00:00:00 2001 From: Kristina Durivage Date: Wed, 3 Jul 2024 12:58:23 -0500 Subject: [PATCH] This works! --- public/app/core/utils/fetch.ts | 5 ++++- .../features/explore/QueryLibrary/QueryTemplateForm.tsx | 9 +++++++-- public/app/features/query-library/api/factory.ts | 2 +- public/app/features/query-library/api/query.ts | 4 ++++ public/app/features/query-library/types.ts | 4 +++- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/public/app/core/utils/fetch.ts b/public/app/core/utils/fetch.ts index 49253f74a31..01e92e9f84a 100644 --- a/public/app/core/utils/fetch.ts +++ b/public/app/core/utils/fetch.ts @@ -74,7 +74,10 @@ export const isContentTypeApplicationJson = (headers: Headers) => { } const contentType = headers.get('content-type'); - if (contentType && contentType.toLowerCase() === 'application/json') { + if ( + contentType && + (contentType.toLowerCase() === 'application/json' || contentType.toLowerCase() === 'application/json-patch+json') + ) { return true; } diff --git a/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx b/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx index 6b0a2e99fe5..c550506b4aa 100644 --- a/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx +++ b/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx @@ -1,3 +1,4 @@ +import { Observer, deepClone, generate, observe } from 'fast-json-patch'; import { useForm } from 'react-hook-form'; import { useAsync } from 'react-use'; @@ -103,8 +104,12 @@ export const QueryTemplateForm = ({ onCancel, onSave, queryToAdd, templateData } const temporaryDefaultTitle = data.description || t('explore.query-library.default-description', 'Public', { timestamp: timestamp }); - if (templateData?.uid) { - handleEditQueryTemplate({ uid: templateData.uid, partialSpec: { title: data.description } }).then((isSuccess) => { + if (templateData?.fullSpec && templateData.uid) { + const docCopy: DataQueryFullSpec = deepClone(templateData.fullSpec); + const observer: Observer = observe(docCopy); + docCopy.spec.title = data.description; + const patch = generate(observer); + handleEditQueryTemplate({ uid: templateData.uid, jsonPatch: patch }).then((isSuccess) => { onSave(isSuccess); }); } else if (queryToAdd) { diff --git a/public/app/features/query-library/api/factory.ts b/public/app/features/query-library/api/factory.ts index 8496df9731e..cc76f2a2d8b 100644 --- a/public/app/features/query-library/api/factory.ts +++ b/public/app/features/query-library/api/factory.ts @@ -33,7 +33,7 @@ export const queryLibraryApi = createApi({ query: (editQueryTemplateCommand) => ({ url: `${editQueryTemplateCommand.uid}`, method: 'PATCH', - data: [{ op: 'replace', path: '/spec/title', value: editQueryTemplateCommand.partialSpec.title }], + data: editQueryTemplateCommand.jsonPatch, }), invalidatesTags: ['QueryTemplatesList'], }), diff --git a/public/app/features/query-library/api/query.ts b/public/app/features/query-library/api/query.ts index b3baced6237..88ac21ca271 100644 --- a/public/app/features/query-library/api/query.ts +++ b/public/app/features/query-library/api/query.ts @@ -27,6 +27,7 @@ export const BASE_URL = `/apis/${API_VERSION}/namespaces/default/querytemplates/ // URL is optional for these requests interface QueryLibraryBackendRequest extends Pick { url?: string; + jsonFormat?: boolean; } /** @@ -41,6 +42,9 @@ export const baseQuery: BaseQueryFn