-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd63a3c
commit 9f36033
Showing
3 changed files
with
29 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,36 @@ | ||
"use client"; | ||
import PolicyForm, { PolicyFormValues } from "@/components/policy/policy-form"; | ||
import { CreatePolicyRequestSchema } from "@buf/safedep_api.bufbuild_es/safedep/services/controltower/v1/policy_pb"; | ||
import { create } from "@bufbuild/protobuf"; | ||
import { toast } from "sonner"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { useRouter } from "next/navigation"; | ||
import { createPolicy } from "./actions"; | ||
import { CreatePolicyRequest } from "@buf/safedep_api.bufbuild_es/safedep/services/controltower/v1/policy_pb"; | ||
import { redirect } from "next/navigation"; | ||
|
||
export default async function Page() { | ||
async function submitHandler(form: PolicyFormValues) { | ||
"use server"; | ||
const policyRequest = new CreatePolicyRequest({ ...form }); | ||
try { | ||
export default function Page() { | ||
const router = useRouter(); | ||
|
||
const { mutateAsync: createPolicyHandler } = useMutation({ | ||
mutationFn: async (form: PolicyFormValues) => { | ||
const policyRequest = create(CreatePolicyRequestSchema, { ...form }); | ||
await createPolicy(policyRequest); | ||
console.log(policyRequest); | ||
} catch { | ||
// TODO(arunanshub): render sonner | ||
} | ||
redirect("/v2/policy/list"); | ||
} | ||
}, | ||
onSuccess: () => { | ||
toast.success("Policy created successfully"); | ||
router.back(); | ||
}, | ||
onError: () => { | ||
toast.error("Failed to create policy"); | ||
}, | ||
}); | ||
|
||
return ( | ||
<div className="flex flex-col gap-6"> | ||
<div className="flex items-center justify-between"> | ||
<h1 className="text-3xl font-bold tracking-tight">Create New Policy</h1> | ||
</div> | ||
|
||
<PolicyForm onSubmit={submitHandler} /> | ||
<PolicyForm onSubmit={createPolicyHandler} /> | ||
</div> | ||
); | ||
} |