import { Component, createEffect, createSignal, For, Show } from 'solid-js' import { useData } from '../context/data' import formStyles from '../styles/Form.module.css' import styles from '../styles/Modal.module.css' import Button from '../ui/Button' import Modal from '../ui/Modal' interface Author { id: number name: string email: string slug: string } interface Community { id: number name: string slug: string } interface Role { id: string name: string description?: string } interface CommunityRolesModalProps { isOpen: boolean author: Author | null community: Community | null onClose: () => void onSave: (authorId: number, communityId: number, roles: string[]) => Promise } const CommunityRolesModal: Component = (props) => { const { queryGraphQL } = useData() const [roles, setRoles] = createSignal([]) const [userRoles, setUserRoles] = createSignal([]) const [loading, setLoading] = createSignal(false) // Загружаем доступные роли при открытии модала createEffect(() => { if (props.isOpen && props.community) { void loadRolesData() } }) const loadRolesData = async () => { setLoading(true) try { // Получаем доступные роли const rolesData = await queryGraphQL( ` query GetRoles($community: Int) { adminGetRoles(community: $community) { id name description } } `, { community: props.community?.id } ) if (rolesData?.adminGetRoles) { setRoles(rolesData.adminGetRoles) } // Получаем текущие роли пользователя if (props.author) { const membersData = await queryGraphQL( ` query GetCommunityMembers($community_id: Int!) { adminGetCommunityMembers(community_id: $community_id, limit: 1000) { members { id roles } } } `, { community_id: props.community?.id } ) const members = membersData?.adminGetCommunityMembers?.members || [] const currentUser = members.find((m: { id: number }) => m.id === props.author?.id) setUserRoles(currentUser?.roles || []) } } catch (error) { console.error('Ошибка загрузки ролей:', error) } finally { setLoading(false) } } const handleRoleToggle = (roleId: string) => { const currentRoles = userRoles() if (currentRoles.includes(roleId)) { setUserRoles(currentRoles.filter((r) => r !== roleId)) } else { setUserRoles([...currentRoles, roleId]) } } const handleSave = async () => { if (!props.author || !props.community) return setLoading(true) try { await props.onSave(props.author.id, props.community.id, userRoles()) props.onClose() } catch (error) { console.error('Ошибка сохранения ролей:', error) } finally { setLoading(false) } } return (
Загрузка ролей...
}>
{(role) => (
handleRoleToggle(role.id)} class={formStyles.checkbox} />
)}
) } export default CommunityRolesModal