All files / laravel-saas/resources/js/Components/roles RoleModal.tsx

0% Statements 0/140
100% Branches 1/1
100% Functions 1/1
0% Lines 0/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                                                                                                                                                                                                                                                                                                                                                                                                     
import React from 'react';
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from '@/Components/ui/Dialog';
import { Button } from '@/Components/ui/Button';
import { Input } from '@/Components/ui/Input';
import { Label } from '@/Components/ui/Label';
import { Checkbox } from '@/Components/ui/Checkbox';
import { Alert, AlertDescription } from '@/Components/ui/Alert';
import { AlertCircle } from 'lucide-react';
import { ScrollArea } from '@/Components/ui/ScrollArea';
 
interface Permission {
  id: number;
  name: string;
  slug: string;
}
 
interface Role {
  id: number;
  name: string;
  slug: string;
  users_count: number;
  permissions_count: number;
  permissions?: Permission[];
}
 
interface RoleModalProps {
  isOpen: boolean;
  onClose: () => void;
  role?: Role | null;
  permissions: Permission[];
  data: {
    name: string;
    slug: string;
    permissions: number[];
  };
  setData: (field: string, value: any) => void;
  onSubmit: (e: React.FormEvent) => void;
  processing: boolean;
  errors: Record<string, string>;
}
 
export function RoleModal({
  isOpen,
  onClose,
  role,
  permissions,
  data,
  setData,
  onSubmit,
  processing,
  errors,
}: RoleModalProps) {
  const isEditing = !!role;
  const isSystemRole = role && ['admin', 'user'].includes(role.slug);
 
  const handlePermissionToggle = (permissionId: number) => {
    const newPermissions = data.permissions.includes(permissionId)
      ? data.permissions.filter(id => id !== permissionId)
      : [...data.permissions, permissionId];
    
    setData('permissions', newPermissions);
  };
 
  const handleSelectAll = () => {
    setData('permissions', permissions.map(p => p.id));
  };
 
  const handleDeselectAll = () => {
    setData('permissions', []);
  };
 
  return (
    <Dialog open={isOpen} onOpenChange={onClose}>
      <DialogContent className="sm:max-w-[600px]">
        <DialogHeader>
          <DialogTitle>
            {isEditing ? 'Edit Role' : 'Create New Role'}
          </DialogTitle>
        </DialogHeader>
        
        <form onSubmit={onSubmit} className="space-y-4">
          {/* Name Field */}
          <div className="space-y-2">
            <Label htmlFor="name">Role Name</Label>
            <Input
              id="name"
              type="text"
              value={data.name}
              onChange={(e) => setData('name', e.target.value)}
              placeholder="e.g., Editor"
              required
            />
            {errors.name && (
              <Alert variant="destructive">
                <AlertCircle className="h-4 w-4" />
                <AlertDescription>{errors.name}</AlertDescription>
              </Alert>
            )}
          </div>
 
          {/* Slug Field */}
          <div className="space-y-2">
            <Label htmlFor="slug">Role Slug</Label>
            <Input
              id="slug"
              type="text"
              value={data.slug}
              onChange={(e) => setData('slug', e.target.value.toLowerCase().replace(/\s+/g, '_'))}
              placeholder="e.g., editor"
              required
              disabled={isSystemRole}
            />
            {isSystemRole && (
              <p className="text-sm text-muted-foreground">
                System roles cannot have their slug changed
              </p>
            )}
            {errors.slug && (
              <Alert variant="destructive">
                <AlertCircle className="h-4 w-4" />
                <AlertDescription>{errors.slug}</AlertDescription>
              </Alert>
            )}
          </div>
 
          {/* Permissions */}
          <div className="space-y-2">
            <div className="flex items-center justify-between">
              <Label>Permissions</Label>
              <div className="space-x-2">
                <Button 
                  type="button" 
                  variant="outline" 
                  size="sm"
                  onClick={handleSelectAll}
                >
                  Select All
                </Button>
                <Button 
                  type="button" 
                  variant="outline" 
                  size="sm"
                  onClick={handleDeselectAll}
                >
                  Deselect All
                </Button>
              </div>
            </div>
            
            <ScrollArea className="h-48 border rounded-md p-4">
              <div className="space-y-3">
                {permissions.map((permission) => (
                  <div key={permission.id} className="flex items-center space-x-2">
                    <Checkbox
                      id={`permission-${permission.id}`}
                      checked={data.permissions.includes(permission.id)}
                      onCheckedChange={(checked) => {
                        if (checked !== null) {
                          handlePermissionToggle(permission.id);
                        }
                      }}
                    />
                    <Label 
                      htmlFor={`permission-${permission.id}`}
                      className="flex-1 cursor-pointer text-sm font-normal"
                    >
                      <span className="font-medium">{permission.name}</span>
                      <span className="text-muted-foreground ml-2">
                        ({permission.slug})
                      </span>
                    </Label>
                  </div>
                ))}
              </div>
            </ScrollArea>
            
            {errors.permissions && (
              <Alert variant="destructive">
                <AlertCircle className="h-4 w-4" />
                <AlertDescription>{errors.permissions}</AlertDescription>
              </Alert>
            )}
          </div>
 
          <DialogFooter>
            <Button type="button" variant="outline" onClick={onClose}>
              Cancel
            </Button>
            <Button type="submit" disabled={processing}>
              {processing ? 'Saving...' : isEditing ? 'Update Role' : 'Create Role'}
            </Button>
          </DialogFooter>
        </form>
      </DialogContent>
    </Dialog>
  );
}