-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthenticatedUserProvider.tsx
103 lines (98 loc) · 2.39 KB
/
AuthenticatedUserProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { useQuery } from '@apollo/client';
import CogSpinner from 'components/CogSpinner';
import gql from 'graphql-tag';
import { useRouter } from 'next/router';
import { ReactNode, useEffect } from 'react';
import { GetMe } from 'types/GetMe';
import { trackUser } from 'utils/analytics';
import { companySectorsPrimarySectorName } from 'utils/companySectors';
import { AuthenticatedUserContext } from './AuthenticatedUserContext';
export const GET_ME = gql`
query GetMe {
me {
id
email
firstName
lastName
expertiseDomain
company {
id
name
location
status
dnbRegion
dnbCountry
dnbCountryIso
dnbPostalCode
dnbAddressLineOne
dnbAddressLineTwo
companySectors {
sectorType
sector {
name
id
}
}
}
roles {
id
name
}
status
canViewUsersAdminDashboard
canViewCompaniesAdminDashboard
canViewSupplyDashboard
canEditSupplyDashboard
canViewCompanyRelationships
canEditCompanyRelationships
canViewEmissionAllocations
canEditEmissionAllocations
canEditCompanySectors
canInviteNewCompanyMembers
canEditCompanyMembers
canRemoveCompanyMembers
canSubmitDataPrivacyInfo
preferences {
suppressTaskListPrompt
}
launchDarklyHash
}
}
`;
interface IProps {
children: ReactNode;
}
export const AuthenticatedUserProvider = ({ children }: IProps) => {
const { data, loading, error } = useQuery<GetMe>(GET_ME);
const router = useRouter();
// identify current user with Segment
useEffect(() => {
if (data?.me) {
trackUser(data.me.id, {
primarySector:
companySectorsPrimarySectorName(data.me.company?.companySectors) ??
'',
location: data.me.company?.location ?? undefined,
companyName: data.me.company?.name,
url: router.pathname,
email: data.me.email,
emailAddress: data.me.email, // 'email' prop can be removed not to trigger a change in HubSpot
});
}
}, [data]);
if (loading) {
return <CogSpinner />;
}
if (error || !data) {
return null;
}
return (
<AuthenticatedUserContext.Provider
value={{
...data.me,
}}
>
{children}
</AuthenticatedUserContext.Provider>
);
};