forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABTestWrapper.js
More file actions
55 lines (50 loc) · 1.52 KB
/
Copy pathABTestWrapper.js
File metadata and controls
55 lines (50 loc) · 1.52 KB
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
import React from 'react'
import { any, string } from 'prop-types'
import { useUserVar } from '../utilities/userState'
import { IS_USER_SUPER_ADMIN } from '../graphql/queries'
import { useQuery } from '@apollo/client'
const isInsiderUser = ({ isUserSuperAdmin, insideUser }) => {
return insideUser || isUserSuperAdmin
}
export function ABTestVariant({ children }) {
return <>{children}</>
}
export function ABTestingWrapper({ children, insiderVariantName = 'B' }) {
const { data } = useQuery(IS_USER_SUPER_ADMIN)
const { currentUser } = useUserVar()
let childIndex = 0
// only one variant
if (!children.length) {
if (
isInsiderUser({
isUserSuperAdmin: data?.isUserSuperAdmin || false,
insideUser: currentUser?.insideUser || false,
})
) {
if (children.props.name === insiderVariantName) return <>{children}</>
else return <></>
} else {
if (children.props.name !== insiderVariantName) return <>{children}</>
else return <></>
}
}
// A + B variants
if (
isInsiderUser({
isUserSuperAdmin: data?.isUserSuperAdmin || false,
insideUser: currentUser?.insideUser || false,
})
) {
childIndex = children.findIndex((variant) => variant.props.name === insiderVariantName)
} else {
childIndex = children.findIndex((variant) => variant.props.name !== insiderVariantName)
}
return <>{children[childIndex]}</>
}
ABTestVariant.propTypes = {
children: any,
}
ABTestingWrapper.propTypes = {
insiderVariantName: string,
children: any,
}