fix
This commit is contained in:
@@ -10,6 +10,7 @@ import { useState } from "react";
|
||||
import { Logout } from "@/actions/auth";
|
||||
import Profile from "./items/profile";
|
||||
import Applications from "./items/applications";
|
||||
import Positions from "./items/positions";
|
||||
|
||||
interface Props {
|
||||
userData: User | null;
|
||||
@@ -26,7 +27,7 @@ export default function MeClient({ userData, consentsData }: Props) {
|
||||
const items = [
|
||||
{"name": t("items.profile.title"), "icon": <UserIcon />, "component": <Profile user={user} setUser={setUser} />},
|
||||
{"name": t("items.applications.title"), "icon": <AppWindowMacIcon />, "component": <Applications consents={consents} setConsents={setConsents} />},
|
||||
{"name": t("items.positions.title"), "icon": <BriefcaseBusinessIcon />},
|
||||
{"name": t("items.positions.title"), "icon": <BriefcaseBusinessIcon />, "component": <Positions />},
|
||||
]
|
||||
|
||||
return (
|
||||
|
||||
@@ -2,13 +2,16 @@
|
||||
|
||||
import { useLocale, useTranslations } from "next-intl";
|
||||
import type { Consent } from "@/types";
|
||||
import { Item, ItemActions, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item"
|
||||
import { Item, ItemActions, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { localeMap } from "@/types";
|
||||
import { enUS } from "date-fns/locale";
|
||||
import { format } from "date-fns";
|
||||
import { revokeConsent } from "@/actions/consents";
|
||||
import { useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Check, X } from "lucide-react";
|
||||
|
||||
interface Props {
|
||||
consents: Consent[] | null;
|
||||
@@ -18,16 +21,29 @@ interface Props {
|
||||
export default function Applications({ consents, setConsents }: Props) {
|
||||
const t = useTranslations("MePage.items.applications");
|
||||
const [list, setList] = useState(consents);
|
||||
const [revokingId, setRevokingId] = useState<string | null>(null);
|
||||
|
||||
const locale = useLocale();
|
||||
const dateLocale = localeMap[locale as keyof typeof localeMap] ?? enUS;
|
||||
|
||||
const handleRevoke = async (clientId: string) => {
|
||||
const result = await revokeConsent(clientId);
|
||||
if (result.success) {
|
||||
const updated = list?.filter(c => c.client_id !== clientId) ?? [];
|
||||
setList(updated);
|
||||
setConsents(updated);
|
||||
if (revokingId) return;
|
||||
setRevokingId(clientId);
|
||||
|
||||
try {
|
||||
const result = await revokeConsent(clientId);
|
||||
if (result.success) {
|
||||
const updated = list?.filter((c) => c.client_id !== clientId) ?? [];
|
||||
setList(updated);
|
||||
setConsents(updated);
|
||||
toast(t("successfullyRevoked"), { icon: <Check size={16} /> });
|
||||
} else {
|
||||
toast(t("revocationError"), { icon: <X size={16} /> });
|
||||
}
|
||||
} catch {
|
||||
toast(t("revocationError"), { icon: <X size={16} /> });
|
||||
} finally {
|
||||
setRevokingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -40,18 +56,23 @@ export default function Applications({ consents, setConsents }: Props) {
|
||||
<Item key={consent.client_id}>
|
||||
<ItemContent>
|
||||
<ItemTitle>{consent.client_name}</ItemTitle>
|
||||
<ItemDescription>{t("authorized")} {format(consent.authorized_at, "PPP", { locale: dateLocale })}</ItemDescription>
|
||||
<ItemDescription>
|
||||
{t("authorized")}{" "}
|
||||
{format(consent.authorized_at, "PPP", { locale: dateLocale })}
|
||||
</ItemDescription>
|
||||
</ItemContent>
|
||||
<ItemActions>
|
||||
<Button
|
||||
disabled={!consent.isRevokable}
|
||||
disabled={!consent.isRevokable || revokingId === consent.client_id}
|
||||
onClick={() => handleRevoke(consent.client_id)}
|
||||
>
|
||||
{revokingId === consent.client_id && <Spinner />}
|
||||
{t("revoke")}
|
||||
</Button>
|
||||
</ItemActions>
|
||||
</Item>
|
||||
))}
|
||||
{list == null && <h2 className="text-lg">{t("noConsents")}</h2>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useForm, Controller, SubmitHandler } from "react-hook-form";
|
||||
import type { User } from "@/types";
|
||||
@@ -10,6 +9,8 @@ import { Input } from "@/components/ui/input";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { BirthdatePicker } from "@/components/ui/birthdate-picker";
|
||||
import { UpdateUser } from "@/actions/auth";
|
||||
import { toast } from "sonner";
|
||||
import { Check, X } from "lucide-react";
|
||||
|
||||
interface Props {
|
||||
user: User | null;
|
||||
@@ -24,7 +25,6 @@ interface FormData {
|
||||
|
||||
export default function Profile({ user, setUser }: Props) {
|
||||
const t = useTranslations("MePage.items.profile");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const { register, handleSubmit, control, formState: { errors, isSubmitting } } = useForm<FormData>({
|
||||
defaultValues: {
|
||||
@@ -35,15 +35,16 @@ export default function Profile({ user, setUser }: Props) {
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const response = await UpdateUser(data.first_name, data.second_name, data.birth_date);
|
||||
|
||||
if (response == null) setError(t("form.updateError"))
|
||||
else setUser(response)
|
||||
if (response == null) toast(t("form.updateError"), { icon: <X size={16} /> })
|
||||
else {
|
||||
setUser(response)
|
||||
toast(t("form.successfullyUpdated"), { icon: <Check size={16} /> })
|
||||
}
|
||||
} catch {
|
||||
setError(t("form.updateError"))
|
||||
toast(t("form.updateError"), { icon: <X size={16} /> })
|
||||
}
|
||||
};
|
||||
|
||||
@@ -106,7 +107,6 @@ export default function Profile({ user, setUser }: Props) {
|
||||
{isSubmitting && <Spinner />}
|
||||
{t("form.save")}
|
||||
</Button>
|
||||
{error && <FieldError className="text-center">{error}</FieldError>}
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user