/* Copyright (C) 2024 Manuel Bustillo*/ 'use client'; import { Affinities } from '@/app/lib/affinities'; import { Group } from '@/app/lib/group'; import { getCsrfToken, getSlug } from '@/app/lib/utils'; import { classNames } from '@/app/ui/components/button'; import { Dialog } from 'primereact/dialog'; import { useEffect, useState } from 'react'; import AffinitySlider from './form/affinitySlider'; export default function AffinitiesFormDialog({ groups, group, visible, onHide }: { groups: Group[], group?: Group, visible: boolean, onHide: () => void, }) { const [affinities, setAffinities] = useState({}); const [isLoadingAffinities, setIsLoadingAffinities] = useState(true); useEffect(() => { setIsLoadingAffinities(true); if (group?.id === undefined) { setAffinities({}); setIsLoadingAffinities(false) } else { fetch(`/api/${getSlug()}/groups/${group?.id}/affinities`) .then((response) => response.json()) .then((data) => { setAffinities(data); setIsLoadingAffinities(false) }); } }, [group]); function submitAffinities() { const formattedAffinities = Object.entries(affinities).map(([groupId, affinity]) => ({ group_id: groupId, affinity: affinity })); fetch(`/api/${getSlug()}/groups/${group?.id}/affinities/bulk_update`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': getCsrfToken(), }, body: JSON.stringify({ affinities: formattedAffinities }) }).then(() => { onHide(); }); } function resetAffinities() { fetch(`/api/${getSlug()}/groups/${group?.id}/affinities/default`, { method: 'GET', headers: { 'Accept': 'application/json', } }).then((response) => response.json()) .then(setAffinities); } return ( {!isLoadingAffinities &&
Describe the affinity with the rest of the groups { groups.filter((currentGroup) => currentGroup.id !== group?.id).map((group) =>
{group.name} setAffinities({ ...affinities, [group.id || "default"]: value })} />
) }
}
); }