@@ -9,18 +9,17 @@ import { selectUserData } from 'App/slice';
99import { ROUTES } from 'routes' ;
1010import { useGetProjectQuery , useAddProjectMemberMutation , useRemoveProjectMemberMutation } from 'services/project' ;
1111import { getProjectRoleByUserName } from '../utils' ;
12+ import { useProjectMemberActions } from '../hooks/useProjectMemberActions' ;
1213
1314export const ProjectDetails : React . FC = ( ) => {
1415 const { t } = useTranslation ( ) ;
1516 const params = useParams ( ) ;
1617 const navigate = useNavigate ( ) ;
1718 const paramProjectName = params . projectName ?? '' ;
18- const [ pushNotification ] = useNotifications ( ) ;
1919 const userData = useAppSelector ( selectUserData ) ;
20+ const { handleJoinProject, handleLeaveProject, isMemberActionLoading } = useProjectMemberActions ( ) ;
2021
2122 const { data : project } = useGetProjectQuery ( { name : paramProjectName } ) ;
22- const [ addMember , { isLoading : isAdding } ] = useAddProjectMemberMutation ( ) ;
23- const [ removeMember , { isLoading : isRemoving } ] = useRemoveProjectMemberMutation ( ) ;
2423
2524 const currentUserRole = useMemo ( ( ) => {
2625 if ( ! userData ?. username || ! project ) return null ;
@@ -30,55 +29,6 @@ export const ProjectDetails: React.FC = () => {
3029 const isProjectOwner = userData ?. username === project ?. owner . username ;
3130
3231 const isMember = currentUserRole !== null ;
33- const isMemberActionLoading = isAdding || isRemoving ;
34-
35- const handleJoinProject = async ( ) => {
36- if ( ! userData ?. username || ! project ) return ;
37-
38- try {
39- await addMember ( {
40- project_name : project . project_name ,
41- username : userData . username ,
42- project_role : 'user' ,
43- } ) . unwrap ( ) ;
44-
45- pushNotification ( {
46- type : 'success' ,
47- content : t ( 'projects.join_success' ) ,
48- } ) ;
49- } catch ( error ) {
50- console . error ( 'Failed to join project:' , error ) ;
51- pushNotification ( {
52- type : 'error' ,
53- content : t ( 'projects.join_error' ) ,
54- } ) ;
55- }
56- } ;
57-
58- const handleLeaveProject = async ( ) => {
59- if ( ! userData ?. username || ! project ) return ;
60-
61- try {
62- await removeMember ( {
63- project_name : project . project_name ,
64- username : userData . username ,
65- } ) . unwrap ( ) ;
66-
67- pushNotification ( {
68- type : 'success' ,
69- content : t ( 'projects.leave_success' ) ,
70- } ) ;
71-
72- // Redirect to project list after successfully leaving
73- navigate ( ROUTES . PROJECT . LIST ) ;
74- } catch ( error ) {
75- console . error ( 'Failed to leave project:' , error ) ;
76- pushNotification ( {
77- type : 'error' ,
78- content : t ( 'projects.leave_error' ) ,
79- } ) ;
80- }
81- } ;
8232
8333 const renderJoinLeaveButton = ( ) => {
8434 // Only show button if user is authenticated and project is loaded
@@ -87,7 +37,7 @@ export const ProjectDetails: React.FC = () => {
8737 if ( ! isMember ) {
8838 return (
8939 < Button
90- onClick = { handleJoinProject }
40+ onClick = { ( ) => handleJoinProject ( project . project_name , userData . username ! ) }
9141 disabled = { isMemberActionLoading }
9242 variant = "primary"
9343 >
@@ -100,7 +50,7 @@ export const ProjectDetails: React.FC = () => {
10050
10151 return (
10252 < Button
103- onClick = { handleLeaveProject }
53+ onClick = { ( ) => handleLeaveProject ( project . project_name , userData . username ! ) }
10454 disabled = { isMemberActionLoading || ! canLeave }
10555 variant = "normal"
10656 >
0 commit comments