1
+ import React from 'react'
2
+ import { updateOdpProfileData , getOdpProfileData } from "../utils/odpConnector" ;
3
+ import Cookies from 'js-cookie' ;
4
+ import { useRouter } from 'next/router'
5
+ import styles from '../styles/PersonalizedForm.module.css' ;
6
+ import InputFields from './InputFields'
7
+
8
+ const fetchOdpData = async ( email ) => {
9
+ return await getOdpProfileData ( email ) ;
10
+ } ;
11
+
12
+ async function submitForm ( event , router ) {
13
+
14
+ event . preventDefault ( ) ;
15
+
16
+ const email = event ?. target ?. username ?. value ;
17
+ const location = event ?. target ?. location ?. value ;
18
+
19
+ if ( email ) {
20
+ const response = await updateOdpProfileData ( email , location ) ;
21
+ console . log ( 'submitForm' , response , email ) ;
22
+
23
+ const profileData = await fetchOdpData ( email ) . catch ( console . error ) ;
24
+ const requestedSalesPack = profileData ?. requested_sales_pack ;
25
+ const state = profileData ?. state ;
26
+
27
+ if ( state ) {
28
+ Cookies . set ( 'state' , state ) ;
29
+ }
30
+ if ( requestedSalesPack ) {
31
+ Cookies . set ( 'requestedSalesPack' , true ) ;
32
+ }
33
+
34
+ Cookies . set ( 'email' , email ) ;
35
+
36
+ router . reload ( window . location . pathname )
37
+ }
38
+
39
+ return ;
40
+ }
41
+
42
+ function resetCookies ( router ) {
43
+ Cookies . remove ( 'state' ) ;
44
+ Cookies . remove ( 'email' ) ;
45
+
46
+ router . reload ( window . location . pathname )
47
+ }
48
+
49
+ const PersonalizedForm = ( { email, state} ) => {
50
+
51
+ const router = useRouter ( ) ;
52
+ const displayLocation = ( state == null || state ?. length === 0 ) &&
53
+ ( email != null || email ?. length > 0 )
54
+
55
+ return (
56
+ < >
57
+ < form className = { styles . formContainer } onSubmit = { ( e ) => submitForm ( e , router ) } >
58
+
59
+ < InputFields componentId = "username" placeholder = "Add email" defaultValue = { email } display = { true } />
60
+
61
+ < InputFields componentId = "location" placeholder = "Add location" display = { displayLocation } />
62
+
63
+ < div className = { `${ styles . contentArea } ` } >
64
+ Optimizely will store and process your personal data as described in our Privacy Policy. You can opt out at any time.
65
+ </ div >
66
+
67
+ < button type = "submit" className = { `${ styles . signupBtn } ` } >
68
+ Sign Up
69
+ </ button >
70
+
71
+ </ form >
72
+
73
+
74
+ < button onClick = { ( ) => resetCookies ( router ) } >
75
+ Reset cookies
76
+ </ button >
77
+ </ >
78
+ )
79
+ }
80
+
81
+ export default PersonalizedForm ;
0 commit comments