|
| 1 | +import * as EngineCredentialsForm from "@/app/forms/engine-credentials-form"; |
| 2 | +import { |
| 3 | + DialogDescription, |
| 4 | + DialogFooter, |
| 5 | + DialogHeader, |
| 6 | + DialogTitle, |
| 7 | + Flex, |
| 8 | + getConfig, |
| 9 | + ls, |
| 10 | + toast, |
| 11 | +} from "@/components"; |
| 12 | +import { queryClient } from "@/queries/global"; |
| 13 | +import { createClient } from "@/queries/manager-engine"; |
| 14 | + |
| 15 | +export default function ProvideEngineCredentialsDialogContent() { |
| 16 | + return ( |
| 17 | + <EngineCredentialsForm.Form |
| 18 | + defaultValues={{ token: "" }} |
| 19 | + errors={ |
| 20 | + ls.engineCredentials.get(getConfig().apiUrl) |
| 21 | + ? { token: { message: "Invalid token.", type: "manual" } } |
| 22 | + : {} |
| 23 | + } |
| 24 | + onSubmit={async (values, form) => { |
| 25 | + const client = createClient({ |
| 26 | + token: values.token, |
| 27 | + }); |
| 28 | + |
| 29 | + try { |
| 30 | + await client.namespaces.list(); |
| 31 | + |
| 32 | + await queryClient.invalidateQueries({ |
| 33 | + refetchType: "active", |
| 34 | + }); |
| 35 | + |
| 36 | + ls.engineCredentials.set(getConfig().apiUrl, values.token); |
| 37 | + |
| 38 | + toast.success( |
| 39 | + "Successfully authenticated with Rivet Engine", |
| 40 | + ); |
| 41 | + } catch (e) { |
| 42 | + if (e && typeof e === "object" && "statusCode" in e) { |
| 43 | + if (e.statusCode === 403) { |
| 44 | + form.setError("token", { |
| 45 | + message: "Invalid token.", |
| 46 | + }); |
| 47 | + return; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + form.setError("token", { |
| 52 | + message: "Failed to connect. Please try again.", |
| 53 | + }); |
| 54 | + return; |
| 55 | + } |
| 56 | + }} |
| 57 | + > |
| 58 | + <DialogHeader> |
| 59 | + <DialogTitle>Missing Rivet Engine credentials</DialogTitle> |
| 60 | + <DialogDescription> |
| 61 | + It looks like the instance of Rivet Engine that you're |
| 62 | + connected to requires additional credentials, please provide |
| 63 | + them below. |
| 64 | + </DialogDescription> |
| 65 | + </DialogHeader> |
| 66 | + <Flex gap="4" direction="col"> |
| 67 | + <EngineCredentialsForm.Token /> |
| 68 | + </Flex> |
| 69 | + <DialogFooter> |
| 70 | + <EngineCredentialsForm.Submit type="submit" allowPristine> |
| 71 | + Save |
| 72 | + </EngineCredentialsForm.Submit> |
| 73 | + </DialogFooter> |
| 74 | + </EngineCredentialsForm.Form> |
| 75 | + ); |
| 76 | +} |
0 commit comments