Ignore:
Timestamp:
03/24/26 23:03:39 (3 months ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
a7550ca
Parents:
73b69b2
Message:

Added functional collaboration between users

File:
1 edited

Legend:

Unmodified
Added
Removed
  • chapterx-frontend/src/store/storyStore.ts

    r73b69b2 r7fbb91c  
    7373  fetchStories: () => Promise<void>
    7474  fetchChapters: () => Promise<void>
     75  fetchCollaborations: () => Promise<void>
    7576  fetchReadingLists: () => Promise<void>
    7677  fetchUserReadingLists: (userId: number) => Promise<void>
     
    9899
    99100  // Collaboration actions
    100   addCollaboration: (collab: Collaboration) => void
     101  addCollaboration: (collab: Collaboration) => Promise<void>
    101102  updateCollaborationPermission: (userId: number, storyId: number, level: PermissionLevel) => void
    102   removeCollaboration: (userId: number, storyId: number) => void
     103  removeCollaboration: (userId: number, storyId: number) => Promise<void>
    103104
    104105  // AI Suggestion actions
     
    175176    } catch {
    176177      // keep mock data on failure
     178    }
     179  },
     180
     181  fetchCollaborations: async () => {
     182    try {
     183      const res = await axios.get(`${API}/collaborations`)
     184      const data: any[] = res.data ?? []
     185      const collaborations: Collaboration[] = data.map((c: any) => ({
     186        collab_id: c.id,
     187        story_id: c.storyId,
     188        user_id: c.userId,
     189        username: c.username ?? '',
     190        name: c.name ?? c.username ?? '',
     191        story_title: '',
     192        role: 'editor' as any,
     193        permission_level: 3 as any,
     194        joined_at: c.createdAt,
     195      }))
     196      set({ collaborations })
     197    } catch {
     198      // keep existing
    177199    }
    178200  },
     
    364386    get().likedStories.some(l => l.userId === userId && l.storyId === storyId),
    365387
    366   addCollaboration: (collab) =>
    367     set(state => ({ collaborations: [...state.collaborations, collab] })),
     388  addCollaboration: async (collab) => {
     389    set(state => ({ collaborations: [...state.collaborations, collab] }))
     390    try {
     391      await axios.post(`${API}/collaborations`, {
     392        userId: collab.user_id,
     393        storyId: collab.story_id,
     394        role: collab.role,
     395      }, { headers: getAuthHeaders() })
     396    } catch {
     397      // keep optimistic
     398    }
     399  },
    368400
    369401  updateCollaborationPermission: (userId, storyId, level) =>
     
    376408    })),
    377409
    378   removeCollaboration: (userId, storyId) =>
     410  removeCollaboration: async (userId, storyId) => {
    379411    set(state => ({
    380412      collaborations: state.collaborations.filter(
    381413        c => !(c.user_id === userId && c.story_id === storyId)
    382414      ),
    383     })),
     415    }))
     416    try {
     417      await axios.delete(`${API}/collaborations/user/${userId}/story/${storyId}`, { headers: getAuthHeaders() })
     418    } catch {
     419      // keep optimistic
     420    }
     421  },
    384422
    385423  fetchSuggestions: async () => {
Note: See TracChangeset for help on using the changeset viewer.