mirror of
https://github.com/fosrl/pangolin.git
synced 2026-09-09 20:46:55 +02:00
fix toggle on pin or passcode not working on policy form
This commit is contained in:
@@ -27,6 +27,7 @@ import { getUserDisplayName } from "@app/lib/getUserDisplayName";
|
|||||||
import { resourceQueries } from "@app/lib/queries";
|
import { resourceQueries } from "@app/lib/queries";
|
||||||
import { useResourcePolicyContext } from "@app/providers/ResourcePolicyProvider";
|
import { useResourcePolicyContext } from "@app/providers/ResourcePolicyProvider";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
|
import type { GetResourcePolicyResponse } from "@server/routers/policy";
|
||||||
import { UserType } from "@server/types/UserTypes";
|
import { UserType } from "@server/types/UserTypes";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import type { AxiosResponse } from "axios";
|
import type { AxiosResponse } from "axios";
|
||||||
@@ -105,7 +106,7 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
}: PolicyAuthStackSectionEditProps) {
|
}: PolicyAuthStackSectionEditProps) {
|
||||||
const t = useTranslations();
|
const t = useTranslations();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { policy } = useResourcePolicyContext();
|
const { policy, updatePolicy } = useResourcePolicyContext();
|
||||||
const api = createApiClient(useEnvContext());
|
const api = createApiClient(useEnvContext());
|
||||||
|
|
||||||
const isResourceOverlay = resourceId !== undefined;
|
const isResourceOverlay = resourceId !== undefined;
|
||||||
@@ -215,7 +216,7 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
users: policyUserItems,
|
users: policyUserItems,
|
||||||
password: null,
|
password: null,
|
||||||
pincode: null,
|
pincode: null,
|
||||||
headerAuth: policy.headerAuth
|
headerAuth: policy.headerAuth?.id
|
||||||
? {
|
? {
|
||||||
user: "",
|
user: "",
|
||||||
password: "",
|
password: "",
|
||||||
@@ -236,8 +237,12 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
);
|
);
|
||||||
const [pinActive, setPinActive] = useState(Boolean(policy.pincodeId));
|
const [pinActive, setPinActive] = useState(Boolean(policy.pincodeId));
|
||||||
const [headerAuthActive, setHeaderAuthActive] = useState(
|
const [headerAuthActive, setHeaderAuthActive] = useState(
|
||||||
Boolean(policy.headerAuth)
|
Boolean(policy.headerAuth?.id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const passcodeOnServerRef = useRef(Boolean(policy.passwordId));
|
||||||
|
const pincodeOnServerRef = useRef(Boolean(policy.pincodeId));
|
||||||
|
const headerAuthOnServerRef = useRef(Boolean(policy.headerAuth?.id));
|
||||||
const [editingMethod, setEditingMethod] =
|
const [editingMethod, setEditingMethod] =
|
||||||
useState<PolicyAuthMethodId | null>(null);
|
useState<PolicyAuthMethodId | null>(null);
|
||||||
|
|
||||||
@@ -283,6 +288,7 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
|
|
||||||
const payload = form.getValues();
|
const payload = form.getValues();
|
||||||
const requests: Array<Promise<AxiosResponse<{}> | void>> = [];
|
const requests: Array<Promise<AxiosResponse<{}> | void>> = [];
|
||||||
|
const policyUpdates: Parameters<typeof updatePolicy>[0] = {};
|
||||||
|
|
||||||
requests.push(
|
requests.push(
|
||||||
api
|
api
|
||||||
@@ -307,7 +313,8 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
)
|
)
|
||||||
.catch(handleError)
|
.catch(handleError)
|
||||||
);
|
);
|
||||||
} else if (!passcodeActive && policy.passwordId) {
|
policyUpdates.passwordId = policy.passwordId ?? -1;
|
||||||
|
} else if (!passcodeActive && passcodeOnServerRef.current) {
|
||||||
requests.push(
|
requests.push(
|
||||||
api
|
api
|
||||||
.put(
|
.put(
|
||||||
@@ -316,6 +323,7 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
)
|
)
|
||||||
.catch(handleError)
|
.catch(handleError)
|
||||||
);
|
);
|
||||||
|
policyUpdates.passwordId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pinActive && payload.pincode?.pincode?.length === 6) {
|
if (pinActive && payload.pincode?.pincode?.length === 6) {
|
||||||
@@ -327,7 +335,8 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
)
|
)
|
||||||
.catch(handleError)
|
.catch(handleError)
|
||||||
);
|
);
|
||||||
} else if (!pinActive && policy.pincodeId) {
|
policyUpdates.pincodeId = policy.pincodeId ?? -1;
|
||||||
|
} else if (!pinActive && pincodeOnServerRef.current) {
|
||||||
requests.push(
|
requests.push(
|
||||||
api
|
api
|
||||||
.put(
|
.put(
|
||||||
@@ -336,6 +345,7 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
)
|
)
|
||||||
.catch(handleError)
|
.catch(handleError)
|
||||||
);
|
);
|
||||||
|
policyUpdates.pincodeId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -351,7 +361,12 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
)
|
)
|
||||||
.catch(handleError)
|
.catch(handleError)
|
||||||
);
|
);
|
||||||
} else if (!headerAuthActive && policy.headerAuth) {
|
policyUpdates.headerAuth = {
|
||||||
|
id: policy.headerAuth?.id ?? -1,
|
||||||
|
extendedCompability:
|
||||||
|
payload.headerAuth.extendedCompatibility ?? true
|
||||||
|
};
|
||||||
|
} else if (!headerAuthActive && headerAuthOnServerRef.current) {
|
||||||
requests.push(
|
requests.push(
|
||||||
api
|
api
|
||||||
.put(
|
.put(
|
||||||
@@ -360,6 +375,10 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
)
|
)
|
||||||
.catch(handleError)
|
.catch(handleError)
|
||||||
);
|
);
|
||||||
|
policyUpdates.headerAuth = {
|
||||||
|
id: null,
|
||||||
|
extendedCompability: null
|
||||||
|
} as unknown as GetResourcePolicyResponse["headerAuth"];
|
||||||
}
|
}
|
||||||
|
|
||||||
requests.push(
|
requests.push(
|
||||||
@@ -370,10 +389,29 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
})
|
})
|
||||||
.catch(handleError)
|
.catch(handleError)
|
||||||
);
|
);
|
||||||
|
policyUpdates.emailWhitelistEnabled = payload.emailWhitelistEnabled;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const results = await Promise.all(requests);
|
const results = await Promise.all(requests);
|
||||||
if (results.every((res) => res && res.status === 200)) {
|
if (results.every((res) => res && res.status === 200)) {
|
||||||
|
if (policyUpdates.passwordId !== undefined) {
|
||||||
|
passcodeOnServerRef.current = Boolean(
|
||||||
|
policyUpdates.passwordId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (policyUpdates.pincodeId !== undefined) {
|
||||||
|
pincodeOnServerRef.current = Boolean(
|
||||||
|
policyUpdates.pincodeId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (policyUpdates.headerAuth !== undefined) {
|
||||||
|
headerAuthOnServerRef.current = Boolean(
|
||||||
|
policyUpdates.headerAuth?.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
updatePolicy(policyUpdates);
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: t("success"),
|
title: t("success"),
|
||||||
description: t("policyUpdatedSuccess")
|
description: t("policyUpdatedSuccess")
|
||||||
@@ -740,7 +778,7 @@ export function PolicyAuthStackSectionEdit({
|
|||||||
}
|
}
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
existingConfigured={Boolean(policy.headerAuth)}
|
existingConfigured={Boolean(policy.headerAuth?.id)}
|
||||||
onSave={(value) => {
|
onSave={(value) => {
|
||||||
form.setValue("headerAuth", value);
|
form.setValue("headerAuth", value);
|
||||||
setHeaderAuthActive(true);
|
setHeaderAuthActive(true);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { createContext, useContext, useState } from "react";
|
import { createContext, useContext, useEffect, useState } from "react";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
import type { GetResourcePolicyResponse } from "@server/routers/policy";
|
import type { GetResourcePolicyResponse } from "@server/routers/policy";
|
||||||
|
|
||||||
@@ -16,6 +16,10 @@ export function ResourcePolicyProvider({
|
|||||||
const [policy, setPolicy] =
|
const [policy, setPolicy] =
|
||||||
useState<GetResourcePolicyResponse>(serverPolicy);
|
useState<GetResourcePolicyResponse>(serverPolicy);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setPolicy(serverPolicy);
|
||||||
|
}, [serverPolicy]);
|
||||||
|
|
||||||
const t = useTranslations();
|
const t = useTranslations();
|
||||||
|
|
||||||
const updatePolicy = (
|
const updatePolicy = (
|
||||||
|
|||||||
Reference in New Issue
Block a user