This repository was archived by the owner on Jul 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup-form.tsx
More file actions
129 lines (113 loc) · 3.21 KB
/
Copy pathsignup-form.tsx
File metadata and controls
129 lines (113 loc) · 3.21 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { Box, Button, chakra } from '@chakra-ui/react';
import { useReCaptcha } from 'next-recaptcha-v3';
import React, { useEffect } from 'react';
import { useToggles } from '../providers/toggles-provider';
type MessageEvent = {
data: string;
origin: string;
};
type ParsedMessage = {
signup_finished: boolean;
url: string;
};
const collectorUrl =
'https://europe-west1-velt-metrics.cloudfunctions.net/signup-form-collector-7b80f45';
const isValidJSON = (data: string): boolean => {
try {
JSON.parse(data);
} catch {
return false;
}
return true;
};
const Form = chakra('iframe');
const formPath = 'signup/partners-external-iframe-signup';
const baseUrls = {
freshchat: 'https://freshworks.com/live-chat-software',
freshdesk: 'https://freshdesk.com',
freshsales: 'https://freshworks.com/crm',
freshservice: 'https://freshservice.com',
} as const;
type Types = keyof typeof baseUrls;
type SignUpFormProps = {
type: Types;
};
export const SignUpForm: React.FC<SignUpFormProps> = props => {
const { isEnabled } = useToggles();
const url = new URL([baseUrls[props.type], formPath].join('/'));
const { executeRecaptcha } = useReCaptcha();
const sendSignUpToCollector = async (signUpUrl: string) => {
try {
const token = await executeRecaptcha('signup');
fetch(collectorUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
signUpUrl,
recaptchaToken: token,
}),
});
} catch {
// eslint-disable-next-line no-console
console.error('Failed to send data to collector');
}
};
useEffect(() => {
const handleMessage = (event: MessageEvent): void => {
if (isEnabled('dev-signup-form')) {
// eslint-disable-next-line no-console
console.log({ event });
}
if (isValidJSON(event.data)) {
const parsedMessage = JSON.parse(event.data) as ParsedMessage;
sendSignUpToCollector(parsedMessage.url);
if (isEnabled('dev-signup-form')) {
// eslint-disable-next-line no-console
console.log({ parsedMessage });
} else {
if (parsedMessage.signup_finished) {
window.location.href = parsedMessage.url.trim();
}
}
}
};
window.addEventListener('message', handleMessage);
return () => {
window.removeEventListener('message', handleMessage);
};
}, [isEnabled, executeRecaptcha]);
return (
<>
{isEnabled('dev-signup-form') && (
<Button onClick={() => sendSignUpToCollector('https://testdomain.com')}>
Send to collector
</Button>
)}
<Box
sx={{
position: 'relative',
overflow: 'hidden',
height: '90vh',
minHeight: '800px',
}}
>
<Form
// sandbox="allow-same-origin allow-scripts allow-popups allow-forms"
sx={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
border: 0,
}}
className="fw-iframe"
allowFullScreen
src={url.toString()}
/>
</Box>
</>
);
};