mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-22 08:45:24 +00:00
🚧 wip: add password
This commit is contained in:
@@ -16,7 +16,7 @@ import { useTranslations } from "next-intl";
|
|||||||
|
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
|
|
||||||
import { createApiClient } from "@app/lib/api";
|
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { createPolicySchema } from ".";
|
import { createPolicySchema } from ".";
|
||||||
|
|
||||||
@@ -53,6 +53,8 @@ import { cn } from "@app/lib/cn";
|
|||||||
import { useResourcePolicyContext } from "@app/providers/ResourcePolicyProvider";
|
import { useResourcePolicyContext } from "@app/providers/ResourcePolicyProvider";
|
||||||
import { useActionState, useState } from "react";
|
import { useActionState, useState } from "react";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
|
import { toast } from "@app/hooks/useToast";
|
||||||
|
import type { AxiosResponse } from "axios";
|
||||||
|
|
||||||
// ─── PolicyAuthMethodsSection ─────────────────────────────────────────────────
|
// ─── PolicyAuthMethodsSection ─────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -87,7 +89,6 @@ export function EditPolicyAuthMethodsSectionForm() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const t = useTranslations();
|
const t = useTranslations();
|
||||||
const [isExpanded, setIsExpanded] = useState(false);
|
|
||||||
const [isSetPasswordOpen, setIsSetPasswordOpen] = useState(false);
|
const [isSetPasswordOpen, setIsSetPasswordOpen] = useState(false);
|
||||||
const [isSetPincodeOpen, setIsSetPincodeOpen] = useState(false);
|
const [isSetPincodeOpen, setIsSetPincodeOpen] = useState(false);
|
||||||
const [isSetHeaderAuthOpen, setIsSetHeaderAuthOpen] = useState(false);
|
const [isSetHeaderAuthOpen, setIsSetHeaderAuthOpen] = useState(false);
|
||||||
@@ -98,6 +99,10 @@ export function EditPolicyAuthMethodsSectionForm() {
|
|||||||
form.watch("headerAuth") ?? policy.headerAuth
|
form.watch("headerAuth") ?? policy.headerAuth
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const [isExpanded, setIsExpanded] = useState(
|
||||||
|
hasPassword || hasPincode || hasHeaderAuth
|
||||||
|
);
|
||||||
|
|
||||||
const passwordForm = useForm({
|
const passwordForm = useForm({
|
||||||
resolver: zodResolver(setPasswordSchema),
|
resolver: zodResolver(setPasswordSchema),
|
||||||
defaultValues: { password: "" }
|
defaultValues: { password: "" }
|
||||||
@@ -121,7 +126,43 @@ export function EditPolicyAuthMethodsSectionForm() {
|
|||||||
if (!isValid) return;
|
if (!isValid) return;
|
||||||
|
|
||||||
const payload = form.getValues();
|
const payload = form.getValues();
|
||||||
console.log({ payload });
|
console.log({ payload, policy });
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await api
|
||||||
|
.put<AxiosResponse<{}>>(
|
||||||
|
`/resource-policy/${policy.resourcePolicyId}/password`,
|
||||||
|
{
|
||||||
|
password: payload.password?.password ?? null
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.catch((e) => {
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: t("policyErrorUpdate"),
|
||||||
|
description: formatAxiosError(
|
||||||
|
e,
|
||||||
|
t("policyErrorUpdateDescription")
|
||||||
|
)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res && res.status === 200) {
|
||||||
|
toast({
|
||||||
|
title: t("success"),
|
||||||
|
description: t("policyUpdatedSuccess")
|
||||||
|
});
|
||||||
|
router.refresh();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: t("policyErrorUpdate"),
|
||||||
|
description: t("policyErrorUpdateMessageDescription")
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isExpanded) {
|
if (!isExpanded) {
|
||||||
@@ -407,7 +448,7 @@ export function EditPolicyAuthMethodsSectionForm() {
|
|||||||
</Credenza>
|
</Credenza>
|
||||||
|
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form action={() => {}}>
|
<form action={formAction}>
|
||||||
<SettingsSection>
|
<SettingsSection>
|
||||||
<SettingsSectionHeader>
|
<SettingsSectionHeader>
|
||||||
<SettingsSectionTitle>
|
<SettingsSectionTitle>
|
||||||
|
|||||||
Reference in New Issue
Block a user