diff --git a/src/pages/admin/newportal/manage/access/index.tsx b/src/pages/admin/newportal/manage/access/index.tsx index f915f958b..db114a15a 100644 --- a/src/pages/admin/newportal/manage/access/index.tsx +++ b/src/pages/admin/newportal/manage/access/index.tsx @@ -1,28 +1,361 @@ import Link from 'next/link'; -import React from 'react'; +import { useSnackbar } from 'notistack'; +import React, { useState, useMemo } from 'react'; +import { useQueryClient } from 'react-query'; -import { UserContext } from 'src/contexts/userContext'; +import { + Button, + Table, + TableBody, + TableCell, + TableHead, + TableRow, + TextField, + Radio, + RadioGroup, + FormControlLabel, + Typography, + Grid, + Paper, + Box, + TableContainer, + InputLabel, + Select, + MenuItem, + FormControl, +} from '@mui/material'; +import type { SelectChangeEvent } from '@mui/material/Select'; + +import { useFeatureFlags } from 'src/api/featureFlag/featureFlag.get'; +import { useUsers } from 'src/api/user/user.list'; import BackArrow from 'src/svg/back-arrow.svg'; -import { UserType } from 'types/user.type'; +import { axiosRequest } from 'src/utils/axiosRequest'; +import { FEATURE_FLAGS_NAMES } from 'types/featureFlag.constant'; +import type { User, UserType } from 'types/user.type'; +import { userTypeNames } from 'types/user.type'; + +const FeatureFlagsTest: React.FC = () => { + const { enqueueSnackbar } = useSnackbar(); + const queryClient = useQueryClient(); + const { data: featureFlags } = useFeatureFlags(); + const { data: users } = useUsers(); + const [newFeatureFlag, setNewFeatureFlag] = useState({ name: '', isEnabled: false }); + const [userFilter, setUserFilter] = useState(''); + + const [addedUsers, setAddedUsers] = useState([]); + const [currentPage, setCurrentPage] = useState(1); + const [usersPerPage] = useState(10); + const [userTypeFilter, setUserTypeFilter] = useState(''); + const [countryCodeFilter, setCountryCodeFilter] = useState(''); + + const filteredUsers = useMemo(() => { + const usersWithoutAccess = (users || []).filter((user) => !addedUsers?.find((addedUser) => addedUser.id === user.id)); + + let filtered = usersWithoutAccess; + + if (userFilter) { + filtered = filtered.filter((user) => user.email.toLowerCase().includes(userFilter.toLowerCase())); + } + + if (userTypeFilter !== '') { + filtered = filtered.filter((user) => user.type === userTypeFilter); + } + + if (countryCodeFilter) { + filtered = filtered.filter((user) => user.country?.isoCode === countryCodeFilter); + } + + return filtered; + }, [users, userFilter, addedUsers, userTypeFilter, countryCodeFilter]); + + const filteredAddedUsers = useMemo(() => { + let filtered = addedUsers; + + if (userFilter) { + filtered = filtered.filter((user) => user.email.toLowerCase().includes(userFilter.toLowerCase())); + } + + if (userTypeFilter !== '') { + filtered = filtered.filter((user) => user.type === userTypeFilter); + } + + if (countryCodeFilter) { + filtered = filtered.filter((user) => user.country?.isoCode === countryCodeFilter); + } + + return filtered; + }, [addedUsers, userFilter, userTypeFilter, countryCodeFilter]); + + const indexOfLastUser = currentPage * usersPerPage; + const indexOfFirstUser = indexOfLastUser - usersPerPage; + const currentUsers = filteredUsers.slice(indexOfFirstUser, indexOfLastUser); + const totalPages = Math.ceil(filteredUsers.length / usersPerPage); + + const handleNewFeatureFlagChange = async (event: SelectChangeEvent) => { + const featureFlagName = event.target.value; -const Access = () => { - const { user } = React.useContext(UserContext); - const hasAccess = user?.type === UserType.SUPER_ADMIN; + // Find the feature flag object from the featureFlags state using the selected feature flag name + const selectedFeatureFlag = (featureFlags || []).find((flag) => flag.name === featureFlagName); + + if (selectedFeatureFlag) { + // Update the newFeatureFlag and addedUsers states with the selected feature flag data + setNewFeatureFlag({ + name: selectedFeatureFlag.name, + isEnabled: selectedFeatureFlag.isEnabled, + }); + setAddedUsers(selectedFeatureFlag.users || []); + } else { + // Reset the newFeatureFlag and addedUsers states if the selected feature flag is not found + setNewFeatureFlag({ name: '', isEnabled: false }); + setAddedUsers([]); + } + }; + + const handleCheckboxChange = (event: React.ChangeEvent) => { + setNewFeatureFlag({ ...newFeatureFlag, isEnabled: event.target.value === 'true' }); + }; + + const handleUserFilterChange = (event: React.ChangeEvent) => { + setUserFilter(event.target.value); + }; + + const handlePageChange = (newPage: number) => { + setCurrentPage(newPage); + }; + + const handleSubmit = async (event: React.FormEvent) => { + event.preventDefault(); + + if (!newFeatureFlag.name) { + alert('Please select a feature flag.'); + return; + } + + const addedUserIds = addedUsers.map((user) => user.id); + + const response = await axiosRequest({ + method: 'POST', + url: '/featureFlags', + data: { + name: newFeatureFlag.name, + isEnabled: newFeatureFlag.isEnabled, + users: addedUserIds, + }, + }); + if (response.error) { + enqueueSnackbar('Erreur, impossible de mettre à jour le feature flag...', { + variant: 'error', + }); + const selectedFeatureFlag = (featureFlags || []).find((flag) => flag.name === newFeatureFlag.name); + if (selectedFeatureFlag) { + setNewFeatureFlag({ + name: selectedFeatureFlag.name, + isEnabled: selectedFeatureFlag.isEnabled, + }); + setAddedUsers(selectedFeatureFlag.users || []); + } + } else { + queryClient.invalidateQueries('feature-flags'); + enqueueSnackbar('Feature flag mis à jour !', { + variant: 'success', + }); + } + }; + + const handleAddUser = (userId: number) => { + const userToAdd = filteredUsers.find((user) => user.id === userId); + if (userToAdd) { + setAddedUsers((prevAddedUsers) => [...(prevAddedUsers || []), userToAdd]); + } + }; + + const handleRemoveUser = (userId: number) => { + setAddedUsers((prevAddedUsers) => prevAddedUsers.filter((user) => user.id !== userId)); + }; - if (!hasAccess) { - return

Vous n'avez pas accès à cette page, vous devez être super admin.

; - } return ( -
+ -
+

Droits d'accès

-

Il y a ici la liste complète des droits d'accès sur 1Village.

-
+ Gestion des feature flags (restrictions d'accès) + + Choisir une restriction + + +
+
+ + Feature Flag + + + +
+ + {newFeatureFlag.name && ( + <> + + État + + + } label="Actif pour tout le monde" /> + } label="Actif pour certains utilisateurs" /> + + + )} + + {newFeatureFlag.name && !newFeatureFlag.isEnabled && ( + + + + Utilisateurs + + + + + + + + Type d'utilisateur + + + + + ) => setCountryCodeFilter(event.target.value)} + /> + + + )} +
+ + {newFeatureFlag.name && !newFeatureFlag.isEnabled && ( + + + + Utilisateurs sans accès + + + + + + ID + Pseudo + Email + Country Code + Type + Action + + + + {currentUsers?.map((user) => ( + + {user.id} + {user.pseudo} + {user.email} + {user.country?.isoCode} + {user.type} + + + + + ))} + +
+
+ + + + + Page {currentPage} of {totalPages} + + + +
+ + + Utilisateurs avec accès{' '} + + + + + + ID + Pseudo + Email + Country Code + Action + + + + {filteredAddedUsers?.map((user) => ( + + {user.id} + {user.pseudo} + {user.email} + {user.country?.isoCode} + + + + + ))} + +
+
+
+
+ )} +
); }; -export default Access; +export default FeatureFlagsTest;