Skip to content

Commit 871502a

Browse files
committed
refactor implementation to move switch to ValidateEvent/index.tsx
1 parent 68f9df5 commit 871502a

3 files changed

Lines changed: 146 additions & 130 deletions

File tree

src/components/ga4/EventBuilder/ValidateEvent/index.tsx

Lines changed: 112 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ import clsx from "classnames"
2121
import useValidateEvent from "./useValidateEvent"
2222
import Loadable from "@/components/Loadable"
2323
import Typography from "@mui/material/Typography"
24+
import Grid from "@mui/material/Grid"
25+
import Switch from "@mui/material/Switch"
2426
import { PAB, PlainButton } from "@/components/Buttons"
2527
import { Check, Warning, Error as ErrorIcon } from "@mui/icons-material"
2628
import PrettyJson from "@/components/PrettyJson"
2729
import usePayload from "./usePayload"
2830
import { ValidationMessage } from "../types"
2931
import Spinner from "@/components/Spinner"
30-
import { EventCtx, Label, UseEuEndpointCtx } from ".."
32+
import { EventCtx, Label } from ".."
3133
import { Card } from "@mui/material"
3234
import { green, red } from "@mui/material/colors"
35+
import WithHelpText from "@/components/WithHelpText"
3336

3437
const PREFIX = 'ValidateEvent';
3538

@@ -47,6 +50,7 @@ interface TemplateProps {
4750
sent?: boolean
4851
payloadErrors?: string | undefined
4952
useTextBox?: boolean
53+
useEuEndpoint: boolean
5054
}
5155

5256
export interface ValidateEventProps {
@@ -68,7 +72,8 @@ const classes = {
6872
heading: `${PREFIX}-heading`,
6973
payload: `${PREFIX}-payload`,
7074
form: `${PREFIX}-form`,
71-
buttonRow: `${PREFIX}-buttonRow`
75+
buttonRow: `${PREFIX}-buttonRow`,
76+
endpointSwitch: `${PREFIX}-endpointSwitch`,
7277
};
7378

7479
const Root = styled('div')((
@@ -80,6 +85,10 @@ const Root = styled('div')((
8085
padding: theme.spacing(2),
8186
},
8287

88+
[`& .${classes.endpointSwitch}`]: {
89+
marginBottom: theme.spacing(2),
90+
},
91+
8392
[`& .${classes.payloadTitle}`]: {
8493
margin: theme.spacing(1, 0),
8594
},
@@ -166,11 +175,11 @@ const Template: React.FC<TemplateProps> = ({
166175
error,
167176
valid,
168177
payloadErrors,
169-
useTextBox
178+
useTextBox,
179+
useEuEndpoint,
170180
}) => {
171181

172182
const { instanceId, api_secret } = useContext(EventCtx)!
173-
const useEuEndpoint = useContext(UseEuEndpointCtx)
174183
const payload = usePayload()
175184
return (
176185
<Card
@@ -271,82 +280,118 @@ const Template: React.FC<TemplateProps> = ({
271280
}
272281

273282
const ValidateEvent: React.FC<ValidateEventProps> = ({formatPayload, payloadErrors, useTextBox}) => {
274-
const request = useValidateEvent()
283+
const [useEuEndpoint, setUseEuEndpoint] = React.useState(false)
284+
const request = useValidateEvent(useEuEndpoint)
275285

276286
return (
277-
<Loadable
278-
request={request}
279-
renderNotStarted={({ validateEvent }) => (
280-
<Template
281-
heading="This event has not been validated"
282-
headingIcon={<Warning />}
283-
body={
284-
<Root>
285-
<Typography>
286-
Update the event using the controls above.
287-
</Typography>
288-
<Typography>
289-
When you're done editing the event, click "Validate Event" to
290-
check if the event is valid.
291-
</Typography>
292-
</Root>
293-
}
294-
validateEvent={ () => {
287+
<div className={classes.form}>
288+
<WithHelpText
289+
notched
290+
shrink
291+
label="server endpoint"
292+
className={classes.endpointSwitch}
293+
helpText="Collect data in the European Union. If enabled, the https://region1.google-analytics.com endpoint will be used to validate and send events."
294+
>
295+
<Grid component="label" container alignItems="center" spacing={1}>
296+
<Grid item>Default</Grid>
297+
<Grid item>
298+
<Switch
299+
data-testid="use-eu-endpoint"
300+
checked={useEuEndpoint}
301+
onChange={e => {
302+
setUseEuEndpoint(e.target.checked)
303+
}}
304+
name="use-eu-endpoint"
305+
color="primary"
306+
/>
307+
</Grid>
308+
<Grid item>EU</Grid>
309+
</Grid>
310+
</WithHelpText>
311+
<Loadable
312+
request={request}
313+
renderNotStarted={({ validateEvent }) => (
314+
<Template
315+
useEuEndpoint={useEuEndpoint}
316+
heading="This event has not been validated"
317+
headingIcon={<Warning />}
318+
body={
319+
<Root>
320+
<Typography>
321+
Update the event using the controls above.
322+
</Typography>
323+
<Typography>
324+
When you're done editing the event, click "Validate Event" to
325+
check if the event is valid.
326+
</Typography>
327+
</Root>
328+
}
329+
validateEvent={() => {
295330
if (formatPayload) {
296331
formatPayload()
297332
}
298333

299334
validateEvent()
300-
}
301-
}
302-
/>
303-
)}
304-
renderInProgress={() => (
305-
<Template heading="Validating" body={<Spinner ellipses />} />
306-
)}
307-
renderFailed={({ validationMessages, validateEvent}) => (
308-
<Template
309-
error
310-
headingIcon={<ErrorIcon />}
311-
heading="Event is invalid"
312-
body=""
313-
validateEvent={ () => {
335+
}}
336+
/>
337+
)}
338+
renderInProgress={() => (
339+
<Template
340+
useEuEndpoint={useEuEndpoint}
341+
heading="Validating"
342+
body={<Spinner ellipses />}
343+
/>
344+
)}
345+
renderFailed={({ validationMessages, validateEvent }) => (
346+
<Template
347+
useEuEndpoint={useEuEndpoint}
348+
error
349+
headingIcon={<ErrorIcon />}
350+
heading="Event is invalid"
351+
body=""
352+
validateEvent={() => {
314353
if (formatPayload) {
315354
formatPayload()
316355
}
317356

318357
validateEvent()
358+
}}
359+
validationMessages={validationMessages}
360+
payloadErrors={payloadErrors}
361+
useTextBox={useTextBox}
362+
/>
363+
)}
364+
renderSuccessful={({
365+
sendToGA,
366+
copyPayload,
367+
copySharableLink,
368+
sent,
369+
}) => (
370+
<Template
371+
useEuEndpoint={useEuEndpoint}
372+
sent={sent}
373+
valid
374+
heading="Event is valid"
375+
headingIcon={<Check />}
376+
sendToGA={sendToGA}
377+
copyPayload={copyPayload}
378+
copySharableLink={copySharableLink}
379+
body={
380+
<>
381+
<Typography>
382+
Use the controls below to copy the event payload or share it
383+
with coworkers.
384+
</Typography>
385+
<Typography>
386+
You can also send the event to Google Analytics and watch it in
387+
action in the Real Time view.
388+
</Typography>
389+
</>
319390
}
320-
}
321-
validationMessages={validationMessages}
322-
payloadErrors={payloadErrors}
323-
useTextBox={useTextBox}
324-
/>
325-
)}
326-
renderSuccessful={({ sendToGA, copyPayload, copySharableLink, sent}) => (
327-
<Template
328-
sent={sent}
329-
valid
330-
heading="Event is valid"
331-
headingIcon={<Check />}
332-
sendToGA={sendToGA}
333-
copyPayload={copyPayload}
334-
copySharableLink={copySharableLink}
335-
body={
336-
<>
337-
<Typography>
338-
Use the controls below to copy the event payload or share it
339-
with coworkers.
340-
</Typography>
341-
<Typography>
342-
You can also send the event to Google Analytics and watch it in
343-
action in the Real Time view.
344-
</Typography>
345-
</>
346-
}
347-
/>
348-
)}
349-
/>
391+
/>
392+
)}
393+
/>
394+
</div>
350395
);
351396
}
352397

src/components/ga4/EventBuilder/ValidateEvent/useValidateEvent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
useEffect,
1212
useState,
1313
} from "react"
14-
import { EventCtx, UseEuEndpointCtx, UseFirebaseCtx } from ".."
14+
import { EventCtx, UseFirebaseCtx } from ".."
1515
import { InstanceId, ValidationMessage } from "../types"
1616
import usePayload from "./usePayload"
1717
import useInputs from "../useInputs"
@@ -91,14 +91,13 @@ export const ValidationRequestCtx = createContext<
9191
ReturnType<typeof useValidateEvent> | undefined
9292
>(undefined)
9393

94-
const useValidateEvent = (): Requestable<
94+
const useValidateEvent = (useEuEndpoint: boolean): Requestable<
9595
ValidationSuccessful,
9696
ValidationNotStarted,
9797
ValidationInProgress,
9898
ValidationFailed
9999
> => {
100100
const useFirebase = useContext(UseFirebaseCtx)
101-
const useEuEndpoint = useContext(UseEuEndpointCtx)
102101
const { useTextBox } = useContext(EventCtx)!
103102
const [status, setStatus] = useState(RequestStatus.NotStarted)
104103
const [validationMessages, setValidationMessages] = useState<

src/components/ga4/EventBuilder/index.tsx

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,8 @@ export const EventCtx = React.createContext<
158158
>(undefined)
159159
export const ShowAdvancedCtx = React.createContext(false)
160160
export const UseFirebaseCtx = React.createContext(false)
161-
export const UseEuEndpointCtx = React.createContext(false)
162161

163162
const EventBuilder: React.FC = () => {
164-
const [useEuEndpoint, setUseEuEndpoint] = React.useState(false)
165-
166163
const [showAdvanced, setShowAdvanced] = React.useState(false)
167164
const {
168165
userProperties,
@@ -294,29 +291,6 @@ const EventBuilder: React.FC = () => {
294291
After choosing a client, fill out the inputs below.
295292
</Typography>
296293

297-
<WithHelpText
298-
notched
299-
shrink
300-
label="server endpoint"
301-
className={classes.clientSwitch}
302-
>
303-
<Grid component="label" container alignItems="center" spacing={1}>
304-
<Grid item>Default</Grid>
305-
<Grid item>
306-
<Switch
307-
data-testid="use-eu-endpoint"
308-
checked={useEuEndpoint}
309-
onChange={e => {
310-
setUseEuEndpoint(e.target.checked)
311-
}}
312-
name="use-eu-endpoint"
313-
color="primary"
314-
/>
315-
</Grid>
316-
<Grid item>EU</Grid>
317-
</Grid>
318-
</WithHelpText>
319-
320294
<LinkedTextField
321295
required
322296
href="https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference#api_secret"
@@ -632,40 +606,38 @@ const EventBuilder: React.FC = () => {
632606
<Typography variant="h3" className={classes.validateHeading}>
633607
Validate & Send event
634608
</Typography>
635-
<UseEuEndpointCtx.Provider value={useEuEndpoint}>
636-
<UseFirebaseCtx.Provider value={useFirebase}>
637-
<EventCtx.Provider
638-
value={{
639-
type,
640-
clientIds: useFirebase
641-
? { app_instance_id, user_id }
642-
: { client_id, user_id },
643-
items,
644-
parameters,
645-
eventName,
646-
userProperties,
647-
timestamp_micros,
648-
non_personalized_ads,
649-
useTextBox,
650-
payloadObj,
651-
instanceId: useFirebase ? { firebase_app_id } : { measurement_id },
652-
api_secret: api_secret!,
653-
}}
654-
>
655-
<ValidateEvent
656-
client_id={client_id || ""}
657-
user_id={user_id || ""}
658-
api_secret={api_secret || ""}
659-
measurement_id={measurement_id || ""}
660-
app_instance_id={app_instance_id || ""}
661-
firebase_app_id={firebase_app_id || ""}
662-
formatPayload={formatPayload}
663-
payloadErrors={payloadErrors}
664-
useTextBox={useTextBox}
665-
/>
666-
</EventCtx.Provider>
667-
</UseFirebaseCtx.Provider>
668-
</UseEuEndpointCtx.Provider>
609+
<UseFirebaseCtx.Provider value={useFirebase}>
610+
<EventCtx.Provider
611+
value={{
612+
type,
613+
clientIds: useFirebase
614+
? { app_instance_id, user_id }
615+
: { client_id, user_id },
616+
items,
617+
parameters,
618+
eventName,
619+
userProperties,
620+
timestamp_micros,
621+
non_personalized_ads,
622+
useTextBox,
623+
payloadObj,
624+
instanceId: useFirebase ? { firebase_app_id } : { measurement_id },
625+
api_secret: api_secret!,
626+
}}
627+
>
628+
<ValidateEvent
629+
client_id={client_id || ""}
630+
user_id={user_id || ""}
631+
api_secret={api_secret || ""}
632+
measurement_id={measurement_id || ""}
633+
app_instance_id={app_instance_id || ""}
634+
firebase_app_id={firebase_app_id || ""}
635+
formatPayload={formatPayload}
636+
payloadErrors={payloadErrors}
637+
useTextBox={useTextBox}
638+
/>
639+
</EventCtx.Provider>
640+
</UseFirebaseCtx.Provider>
669641
</Root>
670642
);
671643
}

0 commit comments

Comments
 (0)