• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EcrituresNumeriques / stylo / 13547287365

26 Feb 2025 03:20PM UTC coverage: 12.024% (+0.3%) from 11.684%
13547287365

push

github

web-flow
Merge pull request #1255 from ggrossetie/fix-1171-hooks-renaming

157 of 305 branches covered (51.48%)

Branch coverage included in aggregate %.

67 of 264 new or added lines in 25 files covered. (25.38%)

10 existing lines in 4 files now uncovered.

1144 of 10515 relevant lines covered (10.88%)

1.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.83
/front/src/hooks/workspace.js
1
import { useSelector } from 'react-redux'
1✔
2
import { useSWRConfig } from 'swr'
1✔
3
import {
1✔
4
  create as createMutation,
5
  getWorkspaceMembers,
6
  getWorkspaces,
7
  inviteMember as inviteMemberMutation,
8
  leave as leaveMutation,
9
  removeMember as removeMemberMutation,
10
} from '../components/workspace/Workspaces.graphql'
11
import { executeQuery } from '../helpers/graphQL.js'
1✔
12
import useGraphQL, { useMutation, useSWRKey } from './graphql.js'
1✔
13

14
export function useActiveWorkspace() {
1✔
15
  return useSelector((state) => {
×
16
    const activeUser = state.activeUser
×
17
    if (activeUser === undefined) {
×
18
      return undefined
×
19
    }
×
20
    const activeWorkspaceId = activeUser.activeWorkspaceId
×
21
    return activeUser.workspaces?.find(
×
22
      (workspace) => workspace._id === activeWorkspaceId
×
23
    )
×
24
  })
×
25
}
×
26

27
export function useWorkspaceMembersActions(workspaceId) {
1✔
28
  const { mutate: workspacesMutate } = useSWRConfig()
2✔
29
  const key = useSWRKey()({ query: getWorkspaces })
2✔
30
  const mutation = useMutation()
2✔
31
  const { data, mutate, error, isLoading } = useGraphQL(
2✔
32
    { query: getWorkspaceMembers, variables: { workspaceId } },
2✔
33
    {
2✔
34
      revalidateOnFocus: false,
2✔
35
      revalidateOnReconnect: false,
2✔
36
    }
2✔
37
  )
2✔
38

39
  const updateMembersCount = async (result) => {
2✔
40
    await workspacesMutate(key, async (data) => ({
1✔
41
      workspaces: data?.workspaces?.map((w) => {
1!
42
        if (w._id === workspaceId) {
×
43
          return {
×
44
            ...w,
×
45
            stats: {
×
46
              ...w.stats,
×
47
              membersCount: result.workspace.members.length,
×
48
            },
×
49
          }
×
50
        } else {
×
51
          return w
×
52
        }
×
53
      }),
1✔
54
    }))
1✔
55
  }
1✔
56

57
  const removeMember = async (user) => {
2✔
58
    const { _id: userId } = user
×
59
    await mutation({
×
60
      query: removeMemberMutation,
×
61
      variables: { workspaceId, userId },
×
62
    })
×
63
    const result = await mutate(
×
64
      async (data) => {
×
65
        return {
×
66
          workspace: {
×
67
            members: data.workspace.members.filter((m) => m._id !== userId),
×
68
          },
×
69
        }
×
70
      },
×
71
      { revalidate: false }
×
72
    )
×
73
    await updateMembersCount(result)
×
74
  }
×
75

76
  const inviteMember = async (user) => {
2✔
77
    const { _id: userId } = user
1✔
78
    await mutation({
1✔
79
      query: inviteMemberMutation,
1✔
80
      variables: { workspaceId, userId, role: '' },
1✔
81
    })
1✔
82
    const result = await mutate(
1✔
83
      async (data) => {
1✔
84
        return {
1✔
85
          workspace: {
1✔
86
            members: [user, ...data.workspace.members],
1✔
87
          },
1✔
88
        }
1✔
89
      },
1✔
90
      { revalidate: false }
1✔
91
    )
1✔
92
    await updateMembersCount(result)
1✔
93
  }
1✔
94

95
  return {
2✔
96
    error,
2✔
97
    isLoading,
2✔
98
    members:
2✔
99
      data?.workspace?.members?.map((member) => ({
2✔
100
        ...member,
1✔
101
        selected: true,
1✔
102
      })) || [],
2✔
103
    inviteMember,
2✔
104
    removeMember,
2✔
105
  }
2✔
106
}
2✔
107

108
export function useWorkspaceActions() {
1✔
109
  const { mutate } = useSWRConfig()
×
110
  const key = useSWRKey()({ query: getWorkspaces })
×
111
  const sessionToken = useSelector((state) => state.sessionToken)
×
112
  const addWorkspace = async (workspace) => {
×
NEW
113
    await executeQuery({
×
NEW
114
      sessionToken,
×
NEW
115
      query: createMutation,
×
NEW
116
      variables: {
×
NEW
117
        data: {
×
NEW
118
          color: workspace.color,
×
NEW
119
          description: workspace.description,
×
NEW
120
          name: workspace.name,
×
121
        },
×
NEW
122
      },
×
NEW
123
    })
×
124
    await mutate(key, async (data) => ({
×
125
      workspaces: [workspace, ...data.workspaces],
×
126
    }))
×
127
  }
×
128

129
  const leaveWorkspace = async (workspaceId) => {
×
NEW
130
    await executeQuery({
×
NEW
131
      sessionToken,
×
NEW
132
      query: leaveMutation,
×
NEW
133
      variables: {
×
NEW
134
        workspaceId,
×
NEW
135
      },
×
NEW
136
    })
×
137
    await mutate(
×
138
      key,
×
139
      async (data) => ({
×
140
        workspaces: data.workspaces.filter((w) => w._id !== workspaceId),
×
141
      }),
×
142
      { revalidate: false }
×
143
    )
×
144
  }
×
145

146
  return {
×
147
    addWorkspace,
×
148
    leaveWorkspace,
×
149
  }
×
150
}
×
151

152
export function useWorkspaces() {
1✔
153
  const { data, error, isLoading } = useGraphQL({
×
154
    query: getWorkspaces,
×
155
  })
×
156

157
  return {
×
158
    workspaces: data?.workspaces,
×
159
    error,
×
160
    isLoading,
×
161
  }
×
162
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc