chore: refactor

This commit is contained in:
Meet
2024-10-01 01:34:24 +05:30
parent b0e536e576
commit 22b6e0afcd
9 changed files with 58 additions and 76 deletions
+19 -23
View File
@@ -1,31 +1,27 @@
import { useMutation } from "@tanstack/react-query";
import { AxiosError } from "axios";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { workspaceKeys } from "../workspace";
export const useImportEnvKey = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({ encryptedJson, decryptionKey }: { encryptedJson: {
nonce: string,
data: string
}, decryptionKey: string }) : Promise<{ success: boolean, message:string }>=> {
try{
const { data } = await apiRequest.post<{
success: boolean,
message: string
}>("/api/v3/migrate/envkey/", {
encryptedJson,
decryptionKey
});
return data;
} catch (err) {
if ((err as AxiosError<{
message: string
}>).response) {
return { success: false, message: (err as AxiosError<{message: string}>).response?.data?.message as string};
}
}
return { success: false, message: "Something went wrong" };
mutationFn: async ({ encryptedJson, decryptionKey }: {
encryptedJson: {
nonce: string,
data: string
}, decryptionKey: string
}): Promise<{ success: boolean, message: string }> => {
const { data } = await apiRequest.post("/api/v3/migrate/env-key/", {
encryptedJson,
decryptionKey
});
return data;
},
onSuccess: () => {
queryClient.invalidateQueries(workspaceKeys.getAllUserWorkspace);
}
});
};
@@ -11,7 +11,7 @@ import { Button, FormControl, IconButton } from "@app/components/v2";
import { useImportEnvKey } from "@app/hooks/api/migration/mutations";
const formSchema = z.object({
decryptionKey: z.string().min(1),
encryptionKey: z.string().min(1),
file: z.unknown(),
encryptedJson: z.object({
nonce: z.string().min(1),
@@ -39,7 +39,7 @@ export const ImportTab = () => {
} = useForm<TForm>({
resolver: zodResolver(formSchema),
values: {
decryptionKey: "",
encryptionKey: "",
encryptedJson: {
nonce: "",
data: ""
@@ -49,7 +49,6 @@ export const ImportTab = () => {
});
const parseJson = (src: ArrayBuffer) => {
console.log("here")
const file = src.toString();
const formatedData: Record<string, string> = JSON.parse(file);
if (Object.keys(formatedData).includes("nonce") && Object.keys(formatedData).includes("data")) {
@@ -59,7 +58,6 @@ export const ImportTab = () => {
};
setValue("encryptedJson", data);
trigger("encryptedJson");
console.log(data);
} else {
setValue("encryptedJson", {
nonce: "",
@@ -101,8 +99,8 @@ export const ImportTab = () => {
return;
}
const res = await importEnvKey({ encryptedJson: data.encryptedJson, decryptionKey: data.decryptionKey });
if (res.success) {
try{
await importEnvKey({ encryptedJson: data.encryptedJson, decryptionKey: data.encryptionKey });
createNotification({
text: "Data imported successfully.",
type: "success"
@@ -111,12 +109,10 @@ export const ImportTab = () => {
if (fileUploadRef.current) {
fileUploadRef.current.value = "";
}
} else {
createNotification({
text: res.message,
type: "error"
});
} catch (error) {
reset();
}
}
const watchEncryptedJsonFile: any = watch("file");
@@ -152,17 +148,17 @@ export const ImportTab = () => {
<form onSubmit={handleSubmit(submitExport)}>
<Controller
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<FormControl errorText={error?.message} isError={Boolean(error)} label="Decryption Key">
<FormControl errorText={error?.message} isError={Boolean(error)} label="Encryption Key">
<input
{...field}
onChange={onChange}
type="password"
className="w-full bg-mineshaft-800 text-white rounded-lg py-2 px-4"
placeholder="Enter decryption key"
placeholder="Enter encryption key"
/>
</FormControl>
)}
name="decryptionKey"
name="encryptionKey"
control={control}
/>
<div className="flex justify-left">