Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
646f6f9
fix: toasts-not-dismissings
AkshayBandi027 Oct 10, 2024
4cfef40
fix: toast-in-permissions
AkshayBandi027 Oct 10, 2024
ef559f1
Update introduction.mdx
perkinsjr Oct 10, 2024
0013f32
fix: bucket caching issue for multiple keys on audit log. (#2305)
MichaelUnkey Oct 10, 2024
87ee808
chore: add roadmap link in the footer (#2291)
harshsbhat Oct 10, 2024
db0384e
feat: add template
chronark Oct 11, 2024
239e616
Update 3_follow_the_unkey_x_account.md (#2323)
Emmarie-Ahtunan Oct 11, 2024
99145de
fix: capture uniqueness error and return it correctly (#2337)
chronark Oct 11, 2024
4a8933a
docs: getVerifications instead of get (#2329)
harshsbhat Oct 11, 2024
4c7fdb2
Update 3_follow_the_unkey_x_account.md (#2324)
aritradevelops Oct 11, 2024
6433270
fix: transaction ...: in use: for query (#2278)
Flo4604 Oct 11, 2024
0bc677f
fix: handle undefined ratelimits
chronark Oct 11, 2024
0333cb1
Update 3_follow_the_unkey_x_account.md (#2338)
Devang0907 Oct 11, 2024
1e58f71
fix: handle case when blogs array is empty
chronark Oct 11, 2024
c9ec637
[oss.gg] side quest : added another framework; hono on cloudflare wor…
prabincankod Oct 11, 2024
697d265
fix: weird spacing in changelog by removing the extra gap (#2340)
Khaan25 Oct 11, 2024
8b3c9cc
oss.gg: create a template for ratelimiting a oak server in deno with …
Devansh-Baghel Oct 12, 2024
fa3d0a3
follow unkey on X #2252 (#2315)
shreenarayan123 Oct 12, 2024
93c5333
Follow the Unkey X account: @unkeydev Complete! (#2332)
Chirag8023 Oct 12, 2024
b5a9efb
fix(www): analytics bento code snippet is not readable (#2311)
unrenamed Oct 12, 2024
fc38e9b
Update 7_create_a_template.md
chronark Oct 12, 2024
46e5766
Update 7_create_a_template.md
chronark Oct 12, 2024
91ca5e9
chore(deps-dev): bump @content-collections/core from 0.6.2 to 0.7.2 (…
dependabot[bot] Oct 13, 2024
b231220
fix: order audit logs by time, show latest on top (#2295)
chronark Oct 13, 2024
3a10eb8
feat: add template
chronark Oct 13, 2024
e06427c
feat: Unkey FastAPI boilerplate (#2307)
harshsbhat Oct 13, 2024
918332d
feat: add template
chronark Oct 13, 2024
c8d9410
feat: follow unkey on X (#2357)
Sakethpavan Oct 13, 2024
317760a
Update 6_record_onboarding.md (#2301)
Ionfinisher Oct 13, 2024
4381b08
fix: reset input fields in API delete dialog after reopening (#2316)
Vardhaman619 Oct 13, 2024
64a93ce
Update 3_follow_the_unkey_x_account.md (#2364)
Atharva-3000 Oct 13, 2024
6628aae
ci: add label
chronark Oct 13, 2024
2abc528
feat: add hover to input fields
chronark Oct 13, 2024
6e72da9
Added gaps and width for md (#2371)
rishipatel9 Oct 13, 2024
8de4ab2
docs: update overview.mdx (#2384)
eltociear Oct 13, 2024
2b866ab
oss.gg side quest 3_follow_the_unkey_x_account.md (#2399)
trinetra110 Oct 13, 2024
3e25824
feat: Following unkey acc on twitter #2407 (#2408)
kunalarya873 Oct 14, 2024
82159fd
refactor to use toast.promise
AkshayBandi027 Oct 14, 2024
38037ee
Merge branch 'main' into fix/toasts-not-dismissings
AkshayBandi027 Oct 14, 2024
65203ad
refactor changes
AkshayBandi027 Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Checkbox } from "@/components/ui/checkbox";
import { toast } from "@/components/ui/toaster";
import { trpc } from "@/lib/trpc/client";
import { Loader2 } from "lucide-react";
import { CircleCheck, Loader2 } from "lucide-react";
import { useRouter } from "next/navigation";
import { useState } from "react";
type Props = {
Expand All @@ -17,53 +17,71 @@ export const RoleToggle: React.FC<Props> = ({ roleId, keyId, checked }) => {

const [optimisticChecked, setOptimisticChecked] = useState(checked);
const connect = trpc.rbac.connectRoleToKey.useMutation({
onMutate: () => {
setOptimisticChecked(true);
toast.loading("Adding Role");
},
onSuccess: () => {
toast.success("Role added", {
description: "Changes may take up to 60 seconds to take effect.",
cancel: {
label: "Undo",
onClick: () => {
disconnect.mutate({ roleId, keyId });
},
},
});
},
onError(err) {
console.error(err);
toast.error(err.message);
},
onSettled: () => {
router.refresh();
},
});
const disconnect = trpc.rbac.disconnectRoleFromKey.useMutation({
onMutate: () => {
setOptimisticChecked(false);
toast.loading("Removing role");
},
onSuccess: () => {
toast.success("Role removed", {
description: "Changes may take up to 60 seconds to take effect.",
cancel: {
label: "Undo",
onClick: () => {
connect.mutate({ roleId, keyId });
},
},
});
},
onError(err) {
console.error(err);
toast.error(err.message);
},
onSettled: () => {
router.refresh();
},
});

const handleConnect = async ({ roleId, keyId }: { roleId: string; keyId: string }) => {
setOptimisticChecked(true);
toast.promise(connect.mutateAsync({ roleId, keyId }), {
loading: "Adding Role ...",
success: () => (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we doing custom components here?

the toast already has a way to display an action
https://sonner.emilkowal.ski/toast#action

<div className="flex items-center gap-2">
<CircleCheck className="w-4 h-4 text-gray-300" />

<div className="max-w-[250px]">
<h2 className="font-semibold">Role added</h2>
<p>Changes may take up to 60 seconds to take effect.</p>
</div>

<div>
<button
onClick={() => disconnect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't hardcode colors please

>
Comment on lines +44 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add explicit type attribute to the <button> element

The <button> element should have an explicit type attribute to prevent unintended form submissions when used inside a <form>. Add type="button" to specify the button's behavior.

Apply this diff to fix the issue:

 <button
+  type="button"
   onClick={() => disconnect.mutate({ roleId, keyId })}
   className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
 >
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<button
onClick={() => disconnect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
<button
type="button"
onClick={() => disconnect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
🧰 Tools
🪛 Biome

[error] 44-47: Provide an explicit type prop for the button element.

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

(lint/a11y/useButtonType)

Undo
</button>
Comment on lines +44 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update state when 'Undo' is clicked to maintain UI consistency

When the 'Undo' button is clicked, the optimisticChecked state is not updated. This may cause the checkbox to display an incorrect state. Update optimisticChecked to ensure the UI reflects the actual state.

Apply this diff to update the state:

 <button
   type="button"
-  onClick={() => disconnect.mutate({ roleId, keyId })}
+  onClick={() => {
+    disconnect.mutate({ roleId, keyId });
+    setOptimisticChecked(false);
+  }}
   className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
 >
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<button
onClick={() => disconnect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
Undo
</button>
<button
type="button"
onClick={() => {
disconnect.mutate({ roleId, keyId });
setOptimisticChecked(false);
}}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
Undo
</button>
🧰 Tools
🪛 Biome

[error] 44-47: Provide an explicit type prop for the button element.

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

(lint/a11y/useButtonType)

</div>
</div>
),
error: (error) => `${error.message || "An error occurred while adding the Role ."}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct error message formatting

Remove the extra space before the period and use consistent capitalization in the error message.

Apply this diff to fix the error message:

-      error: (error) => `${error.message || "An error occurred while adding the Role ."}`,
+      error: (error) => `${error.message || "An error occurred while adding the role."}`,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
error: (error) => `${error.message || "An error occurred while adding the Role ."}`,
error: (error) => `${error.message || "An error occurred while adding the role."}`,

});
};

const handleDisconnect = async ({ roleId, keyId }: { roleId: string; keyId: string }) => {
setOptimisticChecked(false);

toast.promise(disconnect.mutateAsync({ roleId, keyId }), {
loading: "Removing Role...",
success: () => (
<div className="flex items-center gap-2">
<CircleCheck className="w-4 h-4 text-gray-300" />

<div className="max-w-[250px]">
<h2 className="font-semibold">Role removed</h2>
<p>Changes may take up to 60 seconds to take effect.</p>
</div>

<div>
<button
onClick={() => connect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
Comment on lines +72 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add explicit type attribute to the <button> element

Similarly, the <button> element here should have an explicit type attribute to prevent default form submission behavior. Add type="button".

Apply this diff to fix the issue:

 <button
+  type="button"
   onClick={() => connect.mutate({ roleId, keyId })}
   className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
 >
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<button
onClick={() => connect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
<button
type="button"
onClick={() => connect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
🧰 Tools
🪛 Biome

[error] 72-75: Provide an explicit type prop for the button element.

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

(lint/a11y/useButtonType)

Undo
</button>
Comment on lines +72 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update state when 'Undo' is clicked to maintain UI consistency

In this instance, clicking the 'Undo' button does not update the optimisticChecked state. To keep the UI in sync, update optimisticChecked after the mutation.

Apply this diff to update the state:

 <button
   type="button"
-  onClick={() => connect.mutate({ roleId, keyId })}
+  onClick={() => {
+    connect.mutate({ roleId, keyId });
+    setOptimisticChecked(true);
+  }}
   className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
 >
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<button
onClick={() => connect.mutate({ roleId, keyId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
Undo
</button>
<button
type="button"
onClick={() => {
connect.mutate({ roleId, keyId });
setOptimisticChecked(true);
}}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
Undo
</button>
🧰 Tools
🪛 Biome

[error] 72-75: Provide an explicit type prop for the button element.

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

(lint/a11y/useButtonType)

</div>
</div>
),
error: (error) => `${error.message || "An error occurred while removing the Role."}`,
});
};

if (connect.isLoading || disconnect.isLoading) {
return <Loader2 className="w-4 h-4 animate-spin" />;
}
Expand All @@ -72,9 +90,9 @@ export const RoleToggle: React.FC<Props> = ({ roleId, keyId, checked }) => {
checked={optimisticChecked}
onClick={() => {
if (optimisticChecked) {
disconnect.mutate({ roleId, keyId });
handleDisconnect({ roleId, keyId });
} else {
connect.mutate({ roleId, keyId });
handleConnect({ roleId, keyId });
}
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,17 @@ export const DeleteRole: React.FC<Props> = ({ trigger, role }) => {
const isValid = form.watch("name") === role.name;

const deleteRole = trpc.rbac.deleteRole.useMutation({
onMutate() {
toast.loading("Deleting Role");
},
onSuccess() {
toast.success("Role deleted successfully");
router.push("/authorization/roles");
},
onError(err) {
toast.error(err.message);
},
});

async function onSubmit() {
deleteRole.mutate({ roleId: role.id });
toast.promise(deleteRole.mutateAsync({ roleId: role.id }), {
loading: "Deleting Role",
success: "Role deleted",
error: (error) => `${error.message || "An error occurred while deleting the Role ."}`,
});
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import { Checkbox } from "@/components/ui/checkbox";
import { toast } from "@/components/ui/toaster";
import { trpc } from "@/lib/trpc/client";
import { Loader2 } from "lucide-react";
import { CircleCheck, Loader2 } from "lucide-react";
import { useRouter } from "next/navigation";
import { useState } from "react";

type Props = {
permissionId: string;
roleId: string;
Expand All @@ -16,54 +17,73 @@ export const PermissionToggle: React.FC<Props> = ({ roleId, permissionId, checke
const router = useRouter();

const [optimisticChecked, setOptimisticChecked] = useState(checked);

const connect = trpc.rbac.connectPermissionToRole.useMutation({
onMutate: () => {
setOptimisticChecked(true);
toast.loading("Adding Permission");
},
onSuccess: () => {
toast.success("Permission added", {
description: "Changes may take up to 60 seconds to take effect.",
cancel: {
label: "Undo",
onClick: () => {
disconnect.mutate({ roleId, permissionId });
},
},
});
},
onError(err) {
console.error(err);
toast.error(err.message);
},
onSettled: () => {
router.refresh();
},
});

const disconnect = trpc.rbac.disconnectPermissionFromRole.useMutation({
onMutate: () => {
setOptimisticChecked(false);
toast.loading("Removing Permission");
},
onSuccess: () => {
toast.success("Permission removed", {
description: "Changes may take up to 60 seconds to take effect.",
cancel: {
label: "Undo",
onClick: () => {
connect.mutate({ roleId, permissionId });
},
},
});
},
onError(err) {
console.error(err);
toast.error(err.message);
},
onSettled: () => {
router.refresh();
},
});

const handleConnect = async (roleId: string, permissionId: string) => {
setOptimisticChecked(true);
toast.promise(connect.mutateAsync({ roleId, permissionId }), {
Comment on lines +33 to +35
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Revert optimistic UI update if the mutation fails

In handleConnect, you're setting optimisticChecked to true before the mutation completes. If the mutation fails, the checkbox remains checked, which doesn't reflect the actual state.

Consider reverting optimisticChecked in the error handler to ensure UI consistency.

Apply this change:

 const handleConnect = async (roleId: string, permissionId: string) => {
     setOptimisticChecked(true);
     toast.promise(connect.mutateAsync({ roleId, permissionId }), {
       loading: "Adding Permission ...",
       success: () => (
         // success handling
       ),
       error: (error) => {
+        setOptimisticChecked(false);
         `${error.message || "An error occurred while adding the permission."}`
       },
     });
   };

Similarly, in handleDisconnect, ensure the state is reverted on error.

 const handleDisconnect = async (roleId: string, permissionId: string) => {
     setOptimisticChecked(false);
     toast.promise(disconnect.mutateAsync({ roleId, permissionId }), {
       loading: "Removing Permission ...",
       success: () => (
         // success handling
       ),
       error: (error) => {
+        setOptimisticChecked(true);
         `${error.message || "An error occurred while removing the permission."}`
       },
     });
   };

Also applies to: 57-58

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense I think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chronark, glad the suggestion makes sense! Let me know if you have any questions or need further assistance.

(_/)
(•ᴥ•)
/ ❤️

loading: "Adding Permission ...",
success: () => (
<div className="flex items-center gap-2">
<CircleCheck className="w-4 h-4 text-gray-300" />

<div className="max-w-[250px]">
<h2 className="font-semibold">Permission added</h2>
<p>Changes may take up to 60 seconds to take effect.</p>
</div>

<div>
<button
onClick={() => disconnect.mutate({ roleId, permissionId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
Comment on lines +47 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add explicit type="button" to the <button> element to prevent unintended form submissions

The button element defaults to type="submit" if no type is specified, which can cause unintended form submissions when the button is inside a form. To prevent this, explicitly set the type attribute to "button".

Apply this diff to fix the issue:

             <button
+              type="button"
               onClick={() => disconnect.mutate({ roleId, permissionId })}
               className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
             >
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<button
onClick={() => disconnect.mutate({ roleId, permissionId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
<button
type="button"
onClick={() => disconnect.mutate({ roleId, permissionId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
🧰 Tools
🪛 Biome

[error] 47-50: Provide an explicit type prop for the button element.

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

(lint/a11y/useButtonType)

Undo
</button>
</div>
</div>
),
error: (error) => `${error.message || "An error occurred while adding the permission."}`,
});
};

const handleDisconnect = async (roleId: string, permissionId: string) => {
setOptimisticChecked(false);
toast.promise(disconnect.mutateAsync({ roleId, permissionId }), {
loading: "Removing Permission ...",
success: () => (
<div className="flex items-center gap-2">
<CircleCheck className="w-4 h-4 text-gray-300" />

<div className="max-w-[250px]">
<h2 className="font-semibold">Permission removed</h2>
<p>Changes may take up to 60 seconds to take effect.</p>
</div>

<div>
<button
onClick={() => connect.mutate({ roleId, permissionId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
Comment on lines +74 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add explicit type="button" to the <button> element to prevent unintended form submissions

Similar to the previous instance, ensure the type attribute is set to "button" to avoid unintended form behavior.

Apply this diff to fix the issue:

             <button
+              type="button"
               onClick={() => connect.mutate({ roleId, permissionId })}
               className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
             >
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<button
onClick={() => connect.mutate({ roleId, permissionId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
<button
type="button"
onClick={() => connect.mutate({ roleId, permissionId })}
className="bg-[#2c2c2c] text-white px-[4px] py-[2px] rounded hover:bg-[#3c3c3c] transition-colors"
>
🧰 Tools
🪛 Biome

[error] 75-78: Provide an explicit type prop for the button element.

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

(lint/a11y/useButtonType)

Undo
</button>
</div>
</div>
),
error: (error) => `${error.message || "An error occurred while removing the permission."}`,
});
};

if (connect.isLoading || disconnect.isLoading) {
return <Loader2 className="w-4 h-4 animate-spin" />;
}
Expand All @@ -72,9 +92,9 @@ export const PermissionToggle: React.FC<Props> = ({ roleId, permissionId, checke
checked={optimisticChecked}
onClick={() => {
if (optimisticChecked) {
disconnect.mutate({ roleId, permissionId });
handleDisconnect(roleId, permissionId);
} else {
connect.mutate({ roleId, permissionId });
handleConnect(roleId, permissionId);
}
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ export const UpdateRole: React.FC<Props> = ({ trigger, role }) => {
});

const updateRole = trpc.rbac.updateRole.useMutation({
onMutate() {
toast.loading("Updating Role");
},
onSuccess() {
toast.success("Role updated");
router.refresh();
setOpen(false);
},
onError(err) {
toast.error(err.message);
},
});

async function onSubmit(values: z.infer<typeof formSchema>) {
updateRole.mutate({
id: role.id,
name: values.name,
description: values.description ?? null,
});
toast.promise(
updateRole.mutateAsync({
id: role.id,
name: values.name,
description: values.description ?? null,
}),
{
loading: "updating Role",
success: "Role updated",
error: (error) => error.message || `error while updating role ${role.name}`,
},
);
}

return (
Expand Down