Skip to content

Commit 4256f71

Browse files
committed
Refactor collaborator add/remove to use API endpoints
Updated the logic for adding and removing project collaborators to use REST API endpoints instead of direct database calls. This simplifies the frontend code, improves error handling, and ensures collaborator lists are refreshed using API responses.
1 parent 43eac40 commit 4256f71

1 file changed

Lines changed: 51 additions & 57 deletions

File tree

app/dashboard/projects/page.tsx

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -243,63 +243,50 @@ export default function ProjectsPage() {
243243
setLoading(true)
244244
setError(null)
245245

246-
console.log('🔍 Searching for user with email:', collaboratorEmail)
247-
248-
// Find user by email (case-insensitive, trimmed)
249-
const userProfile = await getProfileByEmail(collaboratorEmail)
250-
251-
console.log('👤 User profile found:', userProfile)
252-
253-
if (!userProfile) {
254-
setError(`User not found with email: ${collaboratorEmail.trim()}. Please make sure they have signed up on the platform.`)
255-
alert(`User not found: ${collaboratorEmail.trim()}\n\nPlease make sure:\n1. The email is correct\n2. The user has signed up on the platform\n3. Try searching by name using the dropdown`)
256-
return
257-
}
246+
console.log('🔍 Adding collaborator with email:', collaboratorEmail)
247+
248+
// Use API endpoint to add collaborator
249+
const response = await fetch(`/api/projects/${selectedProject.id}/collaborators`, {
250+
method: 'POST',
251+
headers: {
252+
'Content-Type': 'application/json',
253+
},
254+
body: JSON.stringify({
255+
email: collaboratorEmail.trim(),
256+
role: 'viewer'
257+
})
258+
})
258259

259-
const currentUser = getCurrentUser()
260-
if (collaboratorEmail.toLowerCase().trim() === currentUser?.email.toLowerCase().trim()) {
261-
alert("You cannot add yourself as a collaborator.")
262-
return
263-
}
260+
const data = await response.json()
264261

265-
// Check if already a collaborator
266-
const collaborators = await getProjectCollaborators(selectedProject.id)
267-
if (collaborators.some(c => c.user_id === userProfile.id)) {
268-
alert("This user is already a collaborator.")
262+
if (!response.ok) {
263+
setError(data.error || "Failed to add collaborator")
264+
alert(data.error || "Failed to add collaborator. Please try again.")
269265
return
270266
}
271267

272-
console.log('✅ Adding collaborator:', userProfile.email)
273-
274-
// Add collaborator
275-
await addProjectCollaborator(
276-
selectedProject.id,
277-
userProfile.id,
278-
'viewer',
279-
currentUser?.id
280-
)
268+
console.log('✅ Collaborator added successfully')
281269

282270
setCollaboratorEmail("")
283271
setUserSearchResults([])
284272
setShowUserSearch(false)
285-
alert(`✅ Successfully added ${userProfile.name || userProfile.email} as a collaborator!`)
273+
alert(`✅ Successfully added ${data.collaborator.profile.name || data.collaborator.profile.email} as a collaborator!`)
286274

275+
// Reload projects to show new collaborator
287276
await loadProjects()
288277

289-
// Reload selected project to show new collaborator
290-
const updatedProjects = await getProjects(currentUser!.id)
291-
const updated = updatedProjects.find((p: any) => p.id === selectedProject.id)
292-
if (updated) {
293-
const collaborators = await getProjectCollaborators(updated.id)
294-
const collaboratorEmails = await Promise.all(
295-
collaborators.map(async (c) => {
296-
const profile = await getProfileByEmail(c.user_id)
297-
return profile?.email || ''
298-
})
299-
)
278+
// Update selected project
279+
const collaboratorsResponse = await fetch(`/api/projects/${selectedProject.id}/collaborators`)
280+
const collaboratorsData = await collaboratorsResponse.json()
281+
282+
if (collaboratorsResponse.ok) {
283+
const collaboratorEmails = collaboratorsData.collaborators
284+
.map((c: any) => c.profiles?.email)
285+
.filter((email: string | undefined): email is string => Boolean(email))
286+
300287
setSelectedProject({
301288
...selectedProject,
302-
collaborators: collaboratorEmails.filter(e => e)
289+
collaborators: collaboratorEmails
303290
})
304291
}
305292
} catch (err) {
@@ -324,26 +311,33 @@ export default function ProjectsPage() {
324311
return
325312
}
326313

327-
// Remove collaborator
328-
await removeProjectCollaborator(selectedProject.id, userProfile.id)
314+
// Use API endpoint to remove collaborator
315+
const response = await fetch(`/api/projects/${selectedProject.id}/collaborators?userId=${userProfile.id}`, {
316+
method: 'DELETE'
317+
})
318+
319+
const data = await response.json()
320+
321+
if (!response.ok) {
322+
alert(data.error || "Failed to remove collaborator")
323+
return
324+
}
329325

326+
// Reload projects
330327
await loadProjects()
331328

332329
// Update selected project
333-
const currentUser = getCurrentUser()
334-
const updatedProjects = await getProjects(currentUser!.id)
335-
const updated = updatedProjects.find((p: any) => p.id === selectedProject.id)
336-
if (updated) {
337-
const collaborators = await getProjectCollaborators(updated.id)
338-
const collaboratorEmails = await Promise.all(
339-
collaborators.map(async (c) => {
340-
const profile = await getProfileByEmail(c.user_id)
341-
return profile?.email || ''
342-
})
343-
)
330+
const collaboratorsResponse = await fetch(`/api/projects/${selectedProject.id}/collaborators`)
331+
const collaboratorsData = await collaboratorsResponse.json()
332+
333+
if (collaboratorsResponse.ok) {
334+
const collaboratorEmails = collaboratorsData.collaborators
335+
.map((c: any) => c.profiles?.email)
336+
.filter((email: string | undefined): email is string => Boolean(email))
337+
344338
setSelectedProject({
345339
...selectedProject,
346-
collaborators: collaboratorEmails.filter(e => e)
340+
collaborators: collaboratorEmails
347341
})
348342
}
349343
} catch (err) {

0 commit comments

Comments
 (0)