|
| 1 | +import contextlib |
| 2 | +import os |
| 3 | +from datetime import datetime |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import httpx |
| 7 | +import reflex as rx |
| 8 | +from email_validator import EmailNotValidError, ValidatedEmail, validate_email |
| 9 | +from sqlmodel import Field |
| 10 | + |
| 11 | +from shared.constants import ( |
| 12 | + API_BASE_URL_LOOPS, |
| 13 | + REFLEX_DEV_WEB_NEWSLETTER_FORM_WEBHOOK_URL, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class Waitlist(rx.Model, table=True): |
| 18 | + email: str |
| 19 | + date_created: datetime = Field(default_factory=datetime.utcnow, nullable=False) |
| 20 | + |
| 21 | + |
| 22 | +class IndexState(rx.State): |
| 23 | + """Hold the state for the home page.""" |
| 24 | + |
| 25 | + # Whether the user signed up for the newsletter. |
| 26 | + signed_up: bool = False |
| 27 | + |
| 28 | + # Whether to show the confetti. |
| 29 | + show_confetti: bool = False |
| 30 | + |
| 31 | + @rx.event(background=True) |
| 32 | + async def send_contact_to_webhook( |
| 33 | + self, |
| 34 | + email: str | None, |
| 35 | + ) -> None: |
| 36 | + with contextlib.suppress(httpx.HTTPError): |
| 37 | + async with httpx.AsyncClient() as client: |
| 38 | + response = await client.post( |
| 39 | + REFLEX_DEV_WEB_NEWSLETTER_FORM_WEBHOOK_URL, |
| 40 | + json={ |
| 41 | + "email": email, |
| 42 | + }, |
| 43 | + ) |
| 44 | + response.raise_for_status() |
| 45 | + |
| 46 | + @rx.event(background=True) |
| 47 | + async def add_contact_to_loops( |
| 48 | + self, |
| 49 | + email: str | None, |
| 50 | + ): |
| 51 | + url: str = f"{API_BASE_URL_LOOPS}/contacts/create" |
| 52 | + loops_api_key: str | None = os.getenv("LOOPS_API_KEY") |
| 53 | + if loops_api_key is None: |
| 54 | + print("Loops API key does not exist") |
| 55 | + return |
| 56 | + |
| 57 | + headers = { |
| 58 | + "Accept": "application/json", |
| 59 | + "Authorization": f"Bearer {loops_api_key}", |
| 60 | + } |
| 61 | + try: |
| 62 | + async with httpx.AsyncClient() as client: |
| 63 | + response = await client.post( |
| 64 | + url, |
| 65 | + headers=headers, |
| 66 | + json={ |
| 67 | + "email": email, |
| 68 | + }, |
| 69 | + ) |
| 70 | + response.raise_for_status() # Raise an exception for HTTP errors (4xx and 5xx) |
| 71 | + |
| 72 | + except httpx.HTTPError as e: |
| 73 | + print(f"An error occurred: {e}") |
| 74 | + |
| 75 | + @rx.event |
| 76 | + def signup_for_another_user(self): |
| 77 | + self.signed_up = False |
| 78 | + |
| 79 | + @rx.event(background=True) |
| 80 | + async def signup( |
| 81 | + self, |
| 82 | + form_data: dict[str, Any], |
| 83 | + ): |
| 84 | + """Sign the user up for the newsletter.""" |
| 85 | + email: str | None = None |
| 86 | + if email_to_validate := form_data.get("input_email"): |
| 87 | + try: |
| 88 | + validated_email: ValidatedEmail = validate_email( |
| 89 | + email_to_validate, |
| 90 | + check_deliverability=True, |
| 91 | + ) |
| 92 | + email = validated_email.normalized |
| 93 | + |
| 94 | + except EmailNotValidError as e: |
| 95 | + # Alert the error message. |
| 96 | + yield rx.toast.warning( |
| 97 | + str(e), |
| 98 | + style={ |
| 99 | + "border": "1px solid #3C3646", |
| 100 | + "background": "linear-gradient(218deg, #1D1B23 -35.66%, #131217 100.84%)", |
| 101 | + }, |
| 102 | + ) |
| 103 | + return |
| 104 | + yield IndexState.send_contact_to_webhook(email) |
| 105 | + yield IndexState.add_contact_to_loops(email) |
| 106 | + async with self: |
| 107 | + self.signed_up = True |
| 108 | + yield |
| 109 | + yield [ |
| 110 | + rx.toast.success("Thanks for signing up to the Newsletter!"), |
| 111 | + ] |
0 commit comments