feat: make company selection mandatory for partner role and add inline company creation in UserDialog
All checks were successful
Staging Build / build (push) Successful in 2m21s
All checks were successful
Staging Build / build (push) Successful in 2m21s
This commit is contained in:
@@ -25,6 +25,7 @@ import {
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { createUser } from '@/lib/actions/users'
|
||||
import { createCompany } from '@/lib/actions/companies'
|
||||
import { Plus, Building2 } from 'lucide-react'
|
||||
|
||||
const userSchema = z.object({
|
||||
@@ -33,6 +34,14 @@ const userSchema = z.object({
|
||||
company_id: z.string().optional().or(z.literal('')),
|
||||
first_name: z.string().optional().or(z.literal('')),
|
||||
last_name: z.string().optional().or(z.literal('')),
|
||||
}).refine((data) => {
|
||||
if (data.role === 'partner' && !data.company_id) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}, {
|
||||
message: 'Für Partner-Benutzer ist eine Firmenauswahl erforderlich.',
|
||||
path: ['company_id'],
|
||||
})
|
||||
|
||||
type UserFormValues = z.infer<typeof userSchema>
|
||||
@@ -45,9 +54,17 @@ export function UserDialog({ companies }: UserDialogProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [dropdownOpen, setDropdownOpen] = useState(false)
|
||||
const [search, setSearch] = useState('')
|
||||
const [localCompanies, setLocalCompanies] = useState<{ id: string; name: string }[]>(companies)
|
||||
const [showNewCompanyInput, setShowNewCompanyInput] = useState(false)
|
||||
const [newCompanyName, setNewCompanyName] = useState('')
|
||||
const [isCreatingCompany, setIsCreatingCompany] = useState(false)
|
||||
const dropdownRef = useRef<HTMLDivElement>(null)
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
setLocalCompanies(companies)
|
||||
}, [companies])
|
||||
|
||||
useEffect(() => {
|
||||
function handleClickOutside(e: MouseEvent) {
|
||||
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
|
||||
@@ -95,6 +112,8 @@ export function UserDialog({ companies }: UserDialogProps) {
|
||||
form.reset()
|
||||
setDropdownOpen(false)
|
||||
setSearch('')
|
||||
setShowNewCompanyInput(false)
|
||||
setNewCompanyName('')
|
||||
}
|
||||
}}>
|
||||
<DialogTrigger asChild>
|
||||
@@ -176,11 +195,12 @@ export function UserDialog({ companies }: UserDialogProps) {
|
||||
control={form.control}
|
||||
name="company_id"
|
||||
render={({ field }) => {
|
||||
const selectedCompany = companies.find((c) => c.id === field.value)
|
||||
const selectedCompany = localCompanies.find((c) => c.id === field.value)
|
||||
return (
|
||||
<FormItem className="flex flex-col">
|
||||
<FormLabel className="text-white">Firma (optional)</FormLabel>
|
||||
<FormLabel className="text-white">Firma</FormLabel>
|
||||
<FormControl>
|
||||
<div className="flex flex-col gap-2">
|
||||
<div className="relative" ref={dropdownRef}>
|
||||
<div className="flex items-center gap-2">
|
||||
<Building2 className="w-4 h-4 text-slate-500 shrink-0" />
|
||||
@@ -221,7 +241,7 @@ export function UserDialog({ companies }: UserDialogProps) {
|
||||
>
|
||||
Keine Firma
|
||||
</button>
|
||||
{companies
|
||||
{localCompanies
|
||||
.filter((c) => c.name.toLowerCase().includes(search.toLowerCase()))
|
||||
.map((c) => (
|
||||
<button
|
||||
@@ -243,6 +263,65 @@ export function UserDialog({ companies }: UserDialogProps) {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{showNewCompanyInput ? (
|
||||
<div className="flex gap-2 mt-1">
|
||||
<Input
|
||||
placeholder="Name der neuen Firma"
|
||||
value={newCompanyName}
|
||||
onChange={(e) => setNewCompanyName(e.target.value)}
|
||||
className="bg-white/5 border-white/10 text-white text-xs h-8"
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
onClick={async () => {
|
||||
const trimmed = newCompanyName.trim()
|
||||
if (trimmed) {
|
||||
setIsCreatingCompany(true)
|
||||
try {
|
||||
const created = await createCompany(trimmed)
|
||||
if (created) {
|
||||
setLocalCompanies((prev) => [...prev, created])
|
||||
field.onChange(created.id)
|
||||
setShowNewCompanyInput(false)
|
||||
setNewCompanyName('')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to create company:', err)
|
||||
} finally {
|
||||
setIsCreatingCompany(false)
|
||||
}
|
||||
}
|
||||
}}
|
||||
disabled={isCreatingCompany}
|
||||
className="bg-primary text-xs h-8"
|
||||
>
|
||||
{isCreatingCompany ? 'Erstellt...' : 'Speichern'}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
setShowNewCompanyInput(false)
|
||||
setNewCompanyName('')
|
||||
}}
|
||||
className="text-xs h-8 text-slate-400"
|
||||
>
|
||||
Abbrechen
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowNewCompanyInput(true)}
|
||||
className="text-xs text-primary hover:underline mt-1 self-start font-medium"
|
||||
>
|
||||
+ Neue Firma anlegen
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
|
||||
@@ -11,7 +11,7 @@ export async function getCompanies() {
|
||||
|
||||
export async function createCompany(name: string) {
|
||||
const admin = createAdminClient()
|
||||
const { data, error } = await admin.from('companies').insert({ name }).single()
|
||||
const { data, error } = await admin.from('companies').insert({ name }).select().single()
|
||||
if (error) throw error
|
||||
revalidatePath('/admin/companies')
|
||||
return data
|
||||
@@ -19,7 +19,7 @@ export async function createCompany(name: string) {
|
||||
|
||||
export async function updateCompany(id: string, name: string) {
|
||||
const admin = createAdminClient()
|
||||
const { data, error } = await admin.from('companies').update({ name }).eq('id', id).single()
|
||||
const { data, error } = await admin.from('companies').update({ name }).eq('id', id).select().single()
|
||||
if (error) throw error
|
||||
revalidatePath('/admin/companies')
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user