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 { Input } from '@/components/ui/input'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { createUser } from '@/lib/actions/users'
|
import { createUser } from '@/lib/actions/users'
|
||||||
|
import { createCompany } from '@/lib/actions/companies'
|
||||||
import { Plus, Building2 } from 'lucide-react'
|
import { Plus, Building2 } from 'lucide-react'
|
||||||
|
|
||||||
const userSchema = z.object({
|
const userSchema = z.object({
|
||||||
@@ -33,6 +34,14 @@ const userSchema = z.object({
|
|||||||
company_id: z.string().optional().or(z.literal('')),
|
company_id: z.string().optional().or(z.literal('')),
|
||||||
first_name: z.string().optional().or(z.literal('')),
|
first_name: z.string().optional().or(z.literal('')),
|
||||||
last_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>
|
type UserFormValues = z.infer<typeof userSchema>
|
||||||
@@ -45,9 +54,17 @@ export function UserDialog({ companies }: UserDialogProps) {
|
|||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
const [dropdownOpen, setDropdownOpen] = useState(false)
|
const [dropdownOpen, setDropdownOpen] = useState(false)
|
||||||
const [search, setSearch] = useState('')
|
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 dropdownRef = useRef<HTMLDivElement>(null)
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setLocalCompanies(companies)
|
||||||
|
}, [companies])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function handleClickOutside(e: MouseEvent) {
|
function handleClickOutside(e: MouseEvent) {
|
||||||
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
|
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
|
||||||
@@ -95,6 +112,8 @@ export function UserDialog({ companies }: UserDialogProps) {
|
|||||||
form.reset()
|
form.reset()
|
||||||
setDropdownOpen(false)
|
setDropdownOpen(false)
|
||||||
setSearch('')
|
setSearch('')
|
||||||
|
setShowNewCompanyInput(false)
|
||||||
|
setNewCompanyName('')
|
||||||
}
|
}
|
||||||
}}>
|
}}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
@@ -176,11 +195,12 @@ export function UserDialog({ companies }: UserDialogProps) {
|
|||||||
control={form.control}
|
control={form.control}
|
||||||
name="company_id"
|
name="company_id"
|
||||||
render={({ field }) => {
|
render={({ field }) => {
|
||||||
const selectedCompany = companies.find((c) => c.id === field.value)
|
const selectedCompany = localCompanies.find((c) => c.id === field.value)
|
||||||
return (
|
return (
|
||||||
<FormItem className="flex flex-col">
|
<FormItem className="flex flex-col">
|
||||||
<FormLabel className="text-white">Firma (optional)</FormLabel>
|
<FormLabel className="text-white">Firma</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
<div className="relative" ref={dropdownRef}>
|
<div className="relative" ref={dropdownRef}>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<Building2 className="w-4 h-4 text-slate-500 shrink-0" />
|
<Building2 className="w-4 h-4 text-slate-500 shrink-0" />
|
||||||
@@ -221,7 +241,7 @@ export function UserDialog({ companies }: UserDialogProps) {
|
|||||||
>
|
>
|
||||||
Keine Firma
|
Keine Firma
|
||||||
</button>
|
</button>
|
||||||
{companies
|
{localCompanies
|
||||||
.filter((c) => c.name.toLowerCase().includes(search.toLowerCase()))
|
.filter((c) => c.name.toLowerCase().includes(search.toLowerCase()))
|
||||||
.map((c) => (
|
.map((c) => (
|
||||||
<button
|
<button
|
||||||
@@ -243,6 +263,65 @@ export function UserDialog({ companies }: UserDialogProps) {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</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>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export async function getCompanies() {
|
|||||||
|
|
||||||
export async function createCompany(name: string) {
|
export async function createCompany(name: string) {
|
||||||
const admin = createAdminClient()
|
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
|
if (error) throw error
|
||||||
revalidatePath('/admin/companies')
|
revalidatePath('/admin/companies')
|
||||||
return data
|
return data
|
||||||
@@ -19,7 +19,7 @@ export async function createCompany(name: string) {
|
|||||||
|
|
||||||
export async function updateCompany(id: string, name: string) {
|
export async function updateCompany(id: string, name: string) {
|
||||||
const admin = createAdminClient()
|
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
|
if (error) throw error
|
||||||
revalidatePath('/admin/companies')
|
revalidatePath('/admin/companies')
|
||||||
return data
|
return data
|
||||||
|
|||||||
Reference in New Issue
Block a user