|
| 1 | +import Breadcrumbs from "@components/Breadcrumbs"; |
| 2 | +import Button from "@components/Button"; |
| 3 | +import FancyToggleSwitch from "@components/FancyToggleSwitch"; |
| 4 | +import { notify } from "@components/Notification"; |
| 5 | +import * as Tabs from "@radix-ui/react-tabs"; |
| 6 | +import { useApiCall } from "@utils/api"; |
| 7 | +import { GaugeIcon, LockIcon } from "lucide-react"; |
| 8 | +import React, { useState } from "react"; |
| 9 | +import { useSWRConfig } from "swr"; |
| 10 | +import SettingsIcon from "@/assets/icons/SettingsIcon"; |
| 11 | +import { useHasChanges } from "@/hooks/useHasChanges"; |
| 12 | +import { Account } from "@/interfaces/Account"; |
| 13 | + |
| 14 | +type Props = { |
| 15 | + account: Account; |
| 16 | +}; |
| 17 | + |
| 18 | +export default function PermissionsTab({ account }: Props) { |
| 19 | + const { mutate } = useSWRConfig(); |
| 20 | + const saveRequest = useApiCall<Account>("/accounts/" + account.id); |
| 21 | + |
| 22 | + const [userViewBlocked, setUserViewBlocked] = useState<boolean>( |
| 23 | + account?.settings.regular_users_view_blocked ?? false, |
| 24 | + ); |
| 25 | + |
| 26 | + const { hasChanges, updateRef } = useHasChanges([userViewBlocked]); |
| 27 | + |
| 28 | + const saveChanges = async () => { |
| 29 | + notify({ |
| 30 | + title: "Permission Settings", |
| 31 | + description: "Permissions were updated successfully.", |
| 32 | + promise: saveRequest |
| 33 | + .put({ |
| 34 | + id: account.id, |
| 35 | + settings: { |
| 36 | + regular_users_view_blocked: userViewBlocked, |
| 37 | + groups_propagation_enabled: |
| 38 | + account.settings?.groups_propagation_enabled, |
| 39 | + peer_login_expiration_enabled: |
| 40 | + account.settings?.peer_login_expiration_enabled, |
| 41 | + peer_login_expiration: account.settings?.peer_login_expiration, |
| 42 | + jwt_groups_enabled: account.settings?.jwt_groups_enabled, |
| 43 | + jwt_groups_claim_name: account.settings?.jwt_groups_claim_name, |
| 44 | + jwt_allow_groups: account.settings?.jwt_allow_groups, |
| 45 | + }, |
| 46 | + }) |
| 47 | + .then(() => { |
| 48 | + mutate("/accounts"); |
| 49 | + updateRef([userViewBlocked]); |
| 50 | + }), |
| 51 | + loadingMessage: "Updating permissions...", |
| 52 | + }); |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <Tabs.Content value={"permissions"} className={"w-full"}> |
| 57 | + <div className={"p-default py-6 max-w-xl"}> |
| 58 | + <Breadcrumbs> |
| 59 | + <Breadcrumbs.Item |
| 60 | + href={"/settings"} |
| 61 | + label={"Settings"} |
| 62 | + icon={<SettingsIcon size={13} />} |
| 63 | + /> |
| 64 | + <Breadcrumbs.Item |
| 65 | + href={"/settings?tab=permissions"} |
| 66 | + label={"Permissions"} |
| 67 | + icon={<LockIcon size={14} />} |
| 68 | + active |
| 69 | + /> |
| 70 | + </Breadcrumbs> |
| 71 | + <div className={"flex items-start justify-between"}> |
| 72 | + <h1>Permissions</h1> |
| 73 | + <Button |
| 74 | + variant={"primary"} |
| 75 | + disabled={!hasChanges} |
| 76 | + onClick={saveChanges} |
| 77 | + > |
| 78 | + Save Changes |
| 79 | + </Button> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div className={"flex flex-col gap-6 mt-8 mb-3"}> |
| 83 | + <FancyToggleSwitch |
| 84 | + value={userViewBlocked} |
| 85 | + onChange={setUserViewBlocked} |
| 86 | + label={ |
| 87 | + <> |
| 88 | + <GaugeIcon size={15} /> |
| 89 | + Restrict dashboard for regular users |
| 90 | + </> |
| 91 | + } |
| 92 | + helpText={ |
| 93 | + "Access to the dashboard will be limited and regular users will not be able to view any peers." |
| 94 | + } |
| 95 | + /> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </Tabs.Content> |
| 99 | + ); |
| 100 | +} |
0 commit comments