Skip to content

Commit

Permalink
PMM-12344 Clear state before querying details (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkubinec authored Aug 16, 2023
1 parent 56bb2ef commit 9552ad0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pmm-app/src/pmm-qan/panel/components/Details/Details.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import { useContext, useEffect, useState } from 'react';
import { QueryAnalyticsProvider } from 'pmm-qan/panel/provider/provider';
import { Databases } from 'shared/core';
import DetailsService from './Details.service';
import { DatabasesType, QueryExampleResponseItem } from './Details.types';

export const useDetails = (): [boolean, QueryExampleResponseItem[], DatabasesType] => {
const {
panelState: {
queryId, groupBy, from, to, labels,
queryId,
groupBy,
from,
to,
labels,
},
} = useContext(QueryAnalyticsProvider);
const [loading, setLoading] = useState<boolean>(false);
const [examples, setExamples] = useState<QueryExampleResponseItem[]>([]);
const [databaseType, setDatabaseType] = useState<DatabasesType>(Databases.mysql);
const [databaseType, setDatabaseType] = useState<DatabasesType>();

useEffect(() => {
(async () => {
try {
setLoading(true);
// clear state so we don't get
// invalid data in between query changes
setExamples([]);
setDatabaseType(undefined);

const result = await DetailsService.getExample({
filterBy: queryId,
groupBy,
Expand All @@ -29,8 +37,10 @@ export const useDetails = (): [boolean, QueryExampleResponseItem[], DatabasesTyp
const examples = result.query_examples;
const databaseType = result.query_examples[0].service_type;

setExamples(examples);
// clear database type first
// won't be an issue once we upgrade to React 18 (batched updates)
setDatabaseType(databaseType);
setExamples(examples);
} catch (e) {
console.error(e);
} finally {
Expand Down

0 comments on commit 9552ad0

Please sign in to comment.