From 62e02966fe6619fd8a594fadd08d5284cdfa87e6 Mon Sep 17 00:00:00 2001 From: Filip Slezak Date: Thu, 21 Sep 2023 13:19:16 +0200 Subject: [PATCH] wip update existing instance --- src-tauri/src/commands.rs | 11 +++++----- src-tauri/src/database/models/instance.rs | 1 + .../AddInstanceModal/AddInstanceModal.tsx | 6 ++++- .../steps/AddInstanceDeviceStep.tsx | 5 +++++ .../steps/AddInstanceInitStep.tsx | 22 +++++++++++++++++-- src/pages/client/types.ts | 2 +- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 1feaa5f5..22c7c220 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -137,12 +137,17 @@ pub async fn all_instances(app_state: State<'_, AppState>) -> Result { @@ -45,4 +46,7 @@ export const AddInstanceModal = () => { ); }; -const steps: ReactNode[] = []; +const steps: ReactNode[] = [ + , + , +]; diff --git a/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceDeviceStep.tsx b/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceDeviceStep.tsx index 0d5c6563..c15966ce 100644 --- a/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceDeviceStep.tsx +++ b/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceDeviceStep.tsx @@ -1,4 +1,5 @@ import { zodResolver } from '@hookform/resolvers/zod'; +import { useQueryClient } from '@tanstack/react-query'; import { invoke } from '@tauri-apps/api'; import { useMemo, useState } from 'react'; import { SubmitHandler, useForm } from 'react-hook-form'; @@ -18,6 +19,7 @@ import { CreateDeviceResponse, } from '../../../../../../shared/hooks/api/types'; import { generateWGKeys } from '../../../../../../shared/utils/generateWGKeys'; +import { clientQueryKeys } from '../../../../query'; import { useAddInstanceModal } from '../hooks/useAddInstanceModal'; type FormFields = { @@ -34,6 +36,7 @@ export const AddInstanceDeviceStep = () => { const toaster = useToaster(); const close = useAddInstanceModal((state) => state.close); const [isLoading, setIsLoading] = useState(false); + const queryClient = useQueryClient(); const [proxyUrl] = useAddInstanceModal((state) => [state.proxyUrl], shallow); @@ -78,6 +81,8 @@ export const AddInstanceDeviceStep = () => { .then(() => { setIsLoading(false); toaster.success(componentLL.messages.success.add()); + queryClient.invalidateQueries([clientQueryKeys.getInstances]); + queryClient.invalidateQueries([clientQueryKeys.getLocations]); close(); }) .catch((e) => { diff --git a/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceInitStep.tsx b/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceInitStep.tsx index fa19fb83..501ce595 100644 --- a/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceInitStep.tsx +++ b/src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceInitStep.tsx @@ -1,5 +1,6 @@ import { zodResolver } from '@hookform/resolvers/zod'; import dayjs from 'dayjs'; +import { isUndefined } from 'lodash-es'; import { useMemo } from 'react'; import { SubmitHandler, useForm } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; @@ -16,6 +17,7 @@ import { useToaster } from '../../../../../../shared/defguard-ui/hooks/toasts/us import { EnrollmentStartResponse } from '../../../../../../shared/hooks/api/types'; import { routes } from '../../../../../../shared/routes'; import { useEnrollmentStore } from '../../../../../enrollment/hooks/store/useEnrollmentStore'; +import { clientApi } from '../../../../clientAPI/clientApi'; import { useAddInstanceModal } from '../hooks/useAddInstanceModal'; type FormFields = { @@ -93,13 +95,29 @@ export const AddInstanceModalInitStep = () => { setModalState({ loading: false }); return; } - res.json().then((r: EnrollmentStartResponse) => { - setModalState({ loading: false }); + res.json().then(async (r: EnrollmentStartResponse) => { + // get client registered instances + const clientInstances = await clientApi.getInstances(); + const instance = clientInstances.find((i) => i.uuid === r.instance.id); let proxy_api_url = import.meta.env.DEV ? '' : values.url; if (proxy_api_url[proxy_api_url.length - 1] === '/') { proxy_api_url = proxy_api_url.slice(0, -1); } proxy_api_url = proxy_api_url + '/api/v1'; + setModalState({ loading: false }); + + if (instance) { + // update already registered instance instead + const instanceInfo = await fetch(`${proxy_api_url}/enrollment/network_info`, { + method: 'POST', + headers, + body: JSON.stringify({ + pubKey: instance.pubkey, + }), + }); + return; + } + // register new instance // is user in need of full enrollment ? if (r.user.is_active) { //no, only create new device for desktop client diff --git a/src/pages/client/types.ts b/src/pages/client/types.ts index f8a7cefa..b6b55ee1 100644 --- a/src/pages/client/types.ts +++ b/src/pages/client/types.ts @@ -4,6 +4,7 @@ export type DefguardInstance = { name: string; url: string; connected: boolean; + pubkey: string; }; export type DefguardLocation = { @@ -12,7 +13,6 @@ export type DefguardLocation = { network_id: number; name: string; address: string; - pubkey: string; endpoint: string; allowed_ips: string; };