From 21c27c2cddbf1db2077cd7cfa472e4e8d1ee71e8 Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Sun, 29 Sep 2024 11:10:21 +0200 Subject: [PATCH] fix: Revalidate data after install vitnode --- packages/backend/scripts/database.ts | 2 +- .../src/hooks/core/sign/up/mutation-api.ts | 16 +++++++++---- .../hooks/core/sign/up/use-sign-up-view.ts | 23 ++++++++++++------- .../account/account-install-configs-view.tsx | 14 +---------- .../views/auth/sign/up/form/form-sign-up.tsx | 2 +- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/packages/backend/scripts/database.ts b/packages/backend/scripts/database.ts index 7cb3381f3..d8e1573ed 100644 --- a/packages/backend/scripts/database.ts +++ b/packages/backend/scripts/database.ts @@ -4,7 +4,7 @@ import { spawn } from 'child_process'; // Function to run commands interactively with the ability to handle user input const runInteractiveShellCommand = async (cmd: string, args: string[] = []) => { return new Promise((resolve, reject) => { - const child = spawn(cmd, args, { stdio: 'inherit' }); + const child = spawn(cmd, args, { stdio: 'inherit', shell: true }); child.on('error', error => { reject(error); diff --git a/packages/frontend/src/hooks/core/sign/up/mutation-api.ts b/packages/frontend/src/hooks/core/sign/up/mutation-api.ts index 52eea1d0c..c8c9c4cc7 100644 --- a/packages/frontend/src/hooks/core/sign/up/mutation-api.ts +++ b/packages/frontend/src/hooks/core/sign/up/mutation-api.ts @@ -6,12 +6,14 @@ import { Core_Sessions__Sign_UpMutation, Core_Sessions__Sign_UpMutationVariables, } from '@/graphql/mutations/sessions/core_Sessions__sign_up.generated'; +import { revalidatePath } from 'next/cache'; -interface Args extends Core_Sessions__Sign_UpMutationVariables { - token: string; -} - -export const mutationApi = async (variables: Args) => { +export const mutationApi = async ( + variables: { + installPage?: boolean; + token: string; + } & Core_Sessions__Sign_UpMutationVariables, +) => { try { await fetcher< Core_Sessions__Sign_UpMutation, @@ -23,6 +25,10 @@ export const mutationApi = async (variables: Args) => { 'x-vitnode-captcha-token': variables.token, }, }); + + if (variables.installPage) { + revalidatePath('/', 'layout'); + } } catch (error) { const e = error as Error; diff --git a/packages/frontend/src/hooks/core/sign/up/use-sign-up-view.ts b/packages/frontend/src/hooks/core/sign/up/use-sign-up-view.ts index ebd1069d3..a9ca2a295 100644 --- a/packages/frontend/src/hooks/core/sign/up/use-sign-up-view.ts +++ b/packages/frontend/src/hooks/core/sign/up/use-sign-up-view.ts @@ -9,7 +9,7 @@ import { mutationApi } from './mutation-api'; export const nameRegex = /^(?!.* {2})[\p{L}\p{N}._@ -]*$/u; -export const useSignUpView = () => { +export const useSignUpView = ({ installPage }: { installPage?: boolean }) => { const t = useTranslations('core'); const { setEmailSuccess } = useSignUp(); const { getTokenFromCaptcha, isReady } = useCaptcha(); @@ -39,12 +39,14 @@ export const useSignUpView = () => { /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()\-_=+{};:,<.>]).{8,}$/, ) .default(''), - terms: z - .boolean() - .refine(value => value, { - message: t('sign_up.form.terms.empty'), - }) - .default(false), + terms: installPage + ? z.boolean().optional() + : z + .boolean() + .refine(value => value, { + message: t('sign_up.form.terms.empty'), + }) + .default(false), newsletter: z.boolean().default(false).optional(), }); @@ -63,7 +65,12 @@ export const useSignUpView = () => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { terms, ...rest } = values; - const mutation = await mutationApi({ ...rest, token }); + const mutation = await mutationApi({ + ...rest, + token, + installPage, + newsletter: installPage ? true : values.newsletter, + }); if (mutation?.error) { if (mutation.error === 'CAPTCHA_FAILED') { diff --git a/packages/frontend/src/views/admin/views/install/content/steps/account/account-install-configs-view.tsx b/packages/frontend/src/views/admin/views/install/content/steps/account/account-install-configs-view.tsx index 6521c0acd..8468e3b03 100644 --- a/packages/frontend/src/views/admin/views/install/content/steps/account/account-install-configs-view.tsx +++ b/packages/frontend/src/views/admin/views/install/content/steps/account/account-install-configs-view.tsx @@ -1,7 +1,6 @@ 'use client'; import { AutoForm } from '@/components/form/auto-form'; -import { AutoFormCheckbox } from '@/components/form/fields/checkbox'; import { AutoFormInput, AutoFormInputProps, @@ -16,7 +15,7 @@ import { useInstallVitnode } from '../../hooks/use-install-vitnode'; export const AccountInstallConfigsView = () => { const t = useTranslations('core'); - const { onSubmit, formSchema } = useSignUpView(); + const { onSubmit, formSchema } = useSignUpView({ installPage: true }); const { setCurrentStep } = useInstallVitnode(); return ( @@ -90,17 +89,6 @@ export const AccountInstallConfigsView = () => { ); }, }, - { - id: 'terms', - label: t('sign_up.form.terms.label'), - component: AutoFormCheckbox, - }, - { - id: 'newsletter', - label: t('sign_up.form.newsletter.label'), - description: t('sign_up.form.newsletter.desc'), - component: AutoFormCheckbox, - }, ]} formSchema={formSchema} onSubmit={async (val, form) => { diff --git a/packages/frontend/src/views/theme/views/auth/sign/up/form/form-sign-up.tsx b/packages/frontend/src/views/theme/views/auth/sign/up/form/form-sign-up.tsx index d62315c52..104deff12 100644 --- a/packages/frontend/src/views/theme/views/auth/sign/up/form/form-sign-up.tsx +++ b/packages/frontend/src/views/theme/views/auth/sign/up/form/form-sign-up.tsx @@ -16,7 +16,7 @@ import { useTranslations } from 'next-intl'; export const FormSignUp = () => { const t = useTranslations('core'); - const { formSchema, onSubmit } = useSignUpView(); + const { formSchema, onSubmit } = useSignUpView({}); return (