Reset vendor form on assessment
Signed-off-by: Jonathan <contact@grafikart.fr>
This commit is contained in:
@@ -5,6 +5,7 @@ import { useFragment } from "react-relay";
|
|||||||
import type { useVendorFormFragment$key } from "./__generated__/useVendorFormFragment.graphql";
|
import type { useVendorFormFragment$key } from "./__generated__/useVendorFormFragment.graphql";
|
||||||
import { useMutationWithToasts } from "../useMutationWithToasts";
|
import { useMutationWithToasts } from "../useMutationWithToasts";
|
||||||
import { useTranslate } from "@probo/i18n";
|
import { useTranslate } from "@probo/i18n";
|
||||||
|
import { useEffect, useMemo } from "react";
|
||||||
|
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
@@ -68,8 +69,8 @@ export function useVendorForm(vendorKey: useVendorFormFragment$key) {
|
|||||||
errorMessage: __("Failed to update vendor. Please try again."),
|
errorMessage: __("Failed to update vendor. Please try again."),
|
||||||
});
|
});
|
||||||
|
|
||||||
const form = useFormWithSchema(schema, {
|
const defaultValues = useMemo(
|
||||||
defaultValues: {
|
() => ({
|
||||||
name: vendor.name,
|
name: vendor.name,
|
||||||
description: vendor.description ?? "",
|
description: vendor.description ?? "",
|
||||||
statusPageUrl: vendor.statusPageUrl ?? "",
|
statusPageUrl: vendor.statusPageUrl ?? "",
|
||||||
@@ -85,7 +86,12 @@ export function useVendorForm(vendorKey: useVendorFormFragment$key) {
|
|||||||
trustPageUrl: vendor.trustPageUrl ?? "",
|
trustPageUrl: vendor.trustPageUrl ?? "",
|
||||||
businessOwnerId: vendor.businessOwner?.id,
|
businessOwnerId: vendor.businessOwner?.id,
|
||||||
securityOwnerId: vendor.securityOwner?.id,
|
securityOwnerId: vendor.securityOwner?.id,
|
||||||
},
|
}),
|
||||||
|
[vendor],
|
||||||
|
);
|
||||||
|
|
||||||
|
const form = useFormWithSchema(schema, {
|
||||||
|
defaultValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSubmit = form.handleSubmit((data) => {
|
const handleSubmit = form.handleSubmit((data) => {
|
||||||
@@ -101,6 +107,10 @@ export function useVendorForm(vendorKey: useVendorFormFragment$key) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
form.reset(defaultValues, { keepDirty: true });
|
||||||
|
}, [defaultValues, form]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...form,
|
...form,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export default function VendorOverviewTab() {
|
|||||||
control,
|
control,
|
||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
formState: { errors, isDirty, isSubmitting },
|
formState: { errors, isSubmitting },
|
||||||
} = useVendorForm(vendor);
|
} = useVendorForm(vendor);
|
||||||
|
|
||||||
const urls = useMemo(
|
const urls = useMemo(
|
||||||
@@ -38,7 +38,7 @@ export default function VendorOverviewTab() {
|
|||||||
{ name: "securityPageUrl", label: __("Security page URL") },
|
{ name: "securityPageUrl", label: __("Security page URL") },
|
||||||
{ name: "trustPageUrl", label: __("Trust page URL") },
|
{ name: "trustPageUrl", label: __("Trust page URL") },
|
||||||
] as const,
|
] as const,
|
||||||
[]
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
usePageTitle(vendor.name + " - " + __("Overview"));
|
usePageTitle(vendor.name + " - " + __("Overview"));
|
||||||
@@ -84,7 +84,7 @@ export default function VendorOverviewTab() {
|
|||||||
|
|
||||||
{/* Ownership */}
|
{/* Ownership */}
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<h2 className="text-base font-medium">{__("Ownership details`")}</h2>
|
<h2 className="text-base font-medium">{__("Ownership details")}</h2>
|
||||||
<Card className="space-y-4" padded>
|
<Card className="space-y-4" padded>
|
||||||
<PeopleSelectField
|
<PeopleSelectField
|
||||||
organizationId={organizationId}
|
organizationId={organizationId}
|
||||||
@@ -134,11 +134,9 @@ export default function VendorOverviewTab() {
|
|||||||
|
|
||||||
{/* Submit */}
|
{/* Submit */}
|
||||||
<div className="flex justify-end">
|
<div className="flex justify-end">
|
||||||
{isDirty && (
|
<Button type="submit" disabled={isSubmitting}>
|
||||||
<Button type="submit" disabled={isSubmitting}>
|
{__("Update vendor")}
|
||||||
{__("Update vendor")}
|
</Button>
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user