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

EcrituresNumeriques / stylo / 13141758304

04 Feb 2025 05:55PM UTC coverage: 26.581% (+0.8%) from 25.809%
13141758304

push

github

web-flow
Merge pull request #1188 from ggrossetie/fix-1171-hooks-usegraphql

342 of 546 branches covered (62.64%)

Branch coverage included in aggregate %.

54 of 303 new or added lines in 10 files covered. (17.82%)

2 existing lines in 2 files now uncovered.

3567 of 14160 relevant lines covered (25.19%)

1.66 hits per line

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

40.76
/front/src/hooks/workspace.js
1
import { useSelector } from 'react-redux'
1✔
2
import { useSWRConfig } from 'swr'
1✔
3
import {
1✔
4
  getWorkspaceMembers,
5
  getWorkspaces,
6
  inviteMember as inviteMemberMutation,
7
  removeMember as removeMemberMutation,
8
  create as createMutation,
9
  leave as leaveMutation,
10
} from '../components/workspace/Workspaces.graphql'
11
import { runQuery } 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!
NEW
42
        if (w._id === workspaceId) {
×
NEW
43
          return {
×
NEW
44
            ...w,
×
NEW
45
            stats: {
×
NEW
46
              ...w.stats,
×
NEW
47
              membersCount: result.workspace.members.length,
×
NEW
48
            },
×
NEW
49
          }
×
NEW
50
        } else {
×
NEW
51
          return w
×
NEW
52
        }
×
53
      }),
1✔
54
    }))
1✔
55
  }
1✔
56

57
  const removeMember = async (user) => {
2✔
NEW
58
    const { _id: userId } = user
×
NEW
59
    await mutation({
×
NEW
60
      query: removeMemberMutation,
×
NEW
61
      variables: { workspaceId, userId },
×
NEW
62
    })
×
NEW
63
    const result = await mutate(
×
NEW
64
      async (data) => {
×
NEW
65
        return {
×
NEW
66
          workspace: {
×
NEW
67
            members: data.workspace.members.filter((m) => m._id !== userId),
×
NEW
68
          },
×
NEW
69
        }
×
NEW
70
      },
×
NEW
71
      { revalidate: false }
×
NEW
72
    )
×
NEW
73
    await updateMembersCount(result)
×
NEW
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✔
NEW
109
  const { mutate } = useSWRConfig()
×
NEW
110
  const key = useSWRKey()({ query: getWorkspaces })
×
NEW
111
  const sessionToken = useSelector((state) => state.sessionToken)
×
NEW
112
  const addWorkspace = async (workspace) => {
×
NEW
113
    await runQuery(
×
NEW
114
      { sessionToken },
×
NEW
115
      {
×
NEW
116
        query: createMutation,
×
NEW
117
        variables: {
×
NEW
118
          data: {
×
NEW
119
            color: workspace.color,
×
NEW
120
            description: workspace.description,
×
NEW
121
            name: workspace.name,
×
NEW
122
          },
×
NEW
123
        },
×
NEW
124
      }
×
NEW
125
    )
×
NEW
126
    await mutate(key, async (data) => ({
×
NEW
127
      workspaces: [workspace, ...data.workspaces],
×
NEW
128
    }))
×
NEW
129
  }
×
130

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

NEW
150
  return {
×
NEW
151
    addWorkspace,
×
NEW
152
    leaveWorkspace,
×
NEW
153
  }
×
NEW
154
}
×
155

156
export function useWorkspaces() {
1✔
NEW
157
  const { data, error, isLoading } = useGraphQL({
×
NEW
158
    query: getWorkspaces,
×
NEW
159
  })
×
160

NEW
161
  return {
×
NEW
162
    workspaces: data?.workspaces,
×
NEW
163
    error,
×
NEW
164
    isLoading,
×
NEW
165
  }
×
NEW
166
}
×
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