fix
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { useTranslations } from "next-intl"
|
||||
import type { Position } from "@/types"
|
||||
import { Item, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field"
|
||||
import { Spinner } from "@/components/ui/spinner"
|
||||
import { listAllPositions, upsertPosition, getMyPositions } from "@/actions/positions"
|
||||
import { toast } from "sonner"
|
||||
import { Check, X } from "lucide-react"
|
||||
|
||||
export default function Positions() {
|
||||
const t = useTranslations("MePage.items.positions")
|
||||
|
||||
const [positions, setPositions] = useState<Position[] | null>(null)
|
||||
const [isAdmin, setIsAdmin] = useState<boolean | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
const [code, setCode] = useState("")
|
||||
const [name, setName] = useState("")
|
||||
const [description, setDescription] = useState("")
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [formError, setFormError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const load = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const [own, all] = await Promise.all([getMyPositions(), listAllPositions()])
|
||||
setPositions(all ?? own)
|
||||
setIsAdmin(all !== null)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
void load()
|
||||
}, [])
|
||||
|
||||
const handleSave = async () => {
|
||||
setFormError(null)
|
||||
if (!code || !name) {
|
||||
setFormError(t("form.required"))
|
||||
return
|
||||
}
|
||||
|
||||
setSaving(true)
|
||||
try {
|
||||
const result = await upsertPosition(code, name, description || undefined)
|
||||
if (!result.success) {
|
||||
toast(t("form.saveError"), { icon: <X size={16} /> })
|
||||
return
|
||||
}
|
||||
|
||||
const updated = await listAllPositions()
|
||||
if (updated) setPositions(updated)
|
||||
|
||||
toast(t("form.saved"), { icon: <Check size={16} /> })
|
||||
setCode("")
|
||||
setName("")
|
||||
setDescription("")
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full lg:max-w-md flex flex-col gap-4">
|
||||
<h1 className="text-xl">{t("title")}</h1>
|
||||
|
||||
{loading && (
|
||||
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
||||
<Spinner />
|
||||
<span>{t("loading")}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!loading && (
|
||||
<>
|
||||
<div className="space-y-4">
|
||||
{positions?.map((position) => (
|
||||
<Item key={position.code}>
|
||||
<ItemContent>
|
||||
<ItemTitle>{position.name}</ItemTitle>
|
||||
<ItemDescription>
|
||||
{position.description || t("noDescription")}
|
||||
</ItemDescription>
|
||||
</ItemContent>
|
||||
</Item>
|
||||
))}
|
||||
{(!positions || positions.length === 0) && (
|
||||
<p className="text-sm text-muted-foreground">{t("empty")}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{isAdmin && (
|
||||
<div className="mt-6">
|
||||
<h2 className="text-lg mb-2">{t("form.title")}</h2>
|
||||
<FieldGroup>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="code">{t("form.code")}</FieldLabel>
|
||||
<Input
|
||||
id="code"
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value.toUpperCase())}
|
||||
disabled={saving}
|
||||
/>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="name">{t("form.name")}</FieldLabel>
|
||||
<Input
|
||||
id="name"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
disabled={saving}
|
||||
/>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="description">{t("form.description")}</FieldLabel>
|
||||
<Input
|
||||
id="description"
|
||||
value={description}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
disabled={saving}
|
||||
/>
|
||||
</Field>
|
||||
{formError && <FieldError>{formError}</FieldError>}
|
||||
<Field>
|
||||
<Button type="button" onClick={handleSave} disabled={saving}>
|
||||
{saving && <Spinner />}
|
||||
{t("form.save")}
|
||||
</Button>
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user