From f4c498bce3fd55adcfd91bf9956cb12543dcfffd Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Sun, 14 Jul 2024 00:51:08 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fixed=20caching=20issue=20with?= =?UTF-8?q?=20DC++=20Hubs=20settings=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirDCPPSettings/AirDCPPHubsForm.tsx | 71 +++++++++++++------ src/client/store/index.ts | 4 +- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/client/components/Settings/AirDCPPSettings/AirDCPPHubsForm.tsx b/src/client/components/Settings/AirDCPPSettings/AirDCPPHubsForm.tsx index 21e61d6..8da4166 100644 --- a/src/client/components/Settings/AirDCPPSettings/AirDCPPHubsForm.tsx +++ b/src/client/components/Settings/AirDCPPSettings/AirDCPPHubsForm.tsx @@ -1,9 +1,10 @@ -import React, { ReactElement, useEffect, useState, useContext } from "react"; +import React, { ReactElement, useState } from "react"; import { Form, Field } from "react-final-form"; import { isEmpty, isNil, isUndefined } from "lodash"; import Select from "react-select"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import axios from "axios"; +import { produce } from "immer"; import { AIRDCPP_SERVICE_BASE_URI } from "../../../constants/endpoints"; export const AirDCPPHubsForm = (): ReactElement => { @@ -13,6 +14,7 @@ export const AirDCPPHubsForm = (): ReactElement => { data: settings, isLoading, isError, + refetch, } = useQuery({ queryKey: ["settings"], queryFn: async () => @@ -20,11 +22,9 @@ export const AirDCPPHubsForm = (): ReactElement => { url: "http://localhost:3000/api/settings/getAllSettings", method: "GET", }), + staleTime: Infinity, }); - /** - * Get the hubs list from an AirDCPP Socket - */ const { data: hubs } = useQuery({ queryKey: ["hubs"], queryFn: async () => @@ -37,14 +37,16 @@ export const AirDCPPHubsForm = (): ReactElement => { }), enabled: !isEmpty(settings?.data.directConnect?.client?.host), }); - let hubList = {}; + + let hubList: any[] = []; if (!isNil(hubs)) { hubList = hubs?.data.map(({ hub_url, identity }) => ({ value: hub_url, label: identity.name, })); } - const { mutate } = useMutation({ + + const mutation = useMutation({ mutationFn: async (values) => await axios({ url: `http://localhost:3000/api/settings/saveSettings`, @@ -55,21 +57,43 @@ export const AirDCPPHubsForm = (): ReactElement => { settingsKey: "directConnect", }, }), - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ["settings"] }); + onSuccess: (data) => { + queryClient.setQueryData(["settings"], (oldData: any) => + produce(oldData, (draft: any) => { + draft.data.directConnect.client = { + ...draft.data.directConnect.client, + ...data.data.directConnect.client, + }; + }), + ); }, }); - const validate = async () => {}; + + const validate = async (values) => { + const errors = {}; + // Add any validation logic here if needed + return errors; + }; const SelectAdapter = ({ input, ...rest }) => { return