All files / src/components/pages/Contact index.tsx

10.15% Statements 20/197
100% Branches 0/0
0% Functions 0/1
10.15% Lines 20/197

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 1981x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                   1x 1x  
'use client';
 
import React, { useState } from "react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import emailjs from "@emailjs/browser";
import { clientLogger } from '@/lib/logging/clientLogger';
import { Button, Input } from "@/components/ui";
import { FormRow } from "@/components/ui/FormField";
import { useUserProfile } from "@/hooks/useUserProfile";
import toast from "react-hot-toast";
 
// EmailJS configuration (same as holiday-lights project)
const EMAILJS_SERVICE_ID = 'service_vya873h';
const EMAILJS_TEMPLATE_ID = 'template_6ilaky7';
const EMAILJS_PUBLIC_KEY = '1EvPRt_Y_1owrR37p';
 
const Contact = () => {
  const router = useRouter();
  const { data: session, status } = useSession();
  const isAuthenticated = status === "authenticated";
  const { userProfile } = useUserProfile(isAuthenticated);
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // Parse name into first and last name
  const fullName = session?.user?.name || '';
  const nameParts = fullName.split(' ');
  const firstName = nameParts[0] || '';
  const lastName = nameParts.slice(1).join(' ') || '';

  // Custom className to match existing design system
  const inputClassName = "rounded-md border-gray-3 dark:border-gray-600 bg-gray-1 dark:bg-gray-700 placeholder:text-dark-5 dark:placeholder:text-gray-400 dark:text-gray-200 py-2.5 px-5 focus:shadow-input";

  const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    setIsSubmitting(true);
    setError(null);

    const form = e.currentTarget;
    const formData = new FormData(form);

    const fName = formData.get('firstName') as string;
    const lName = formData.get('lastName') as string;
    const email = formData.get('email') as string;
    const phone = formData.get('phone') as string;
    const subject = formData.get('subject') as string;
    const message = formData.get('message') as string;

    // Build message with phone if provided
    const fullMessage = phone
      ? `${message}\n\n---\nPhone: ${phone}`
      : message;

    const templateParams = {
      from_name: `${fName} ${lName}`.trim(),
      from_email: email,
      subject: subject,
      message: fullMessage,
      source: window.location.host,
    };

    try {
      await emailjs.send(
        EMAILJS_SERVICE_ID,
        EMAILJS_TEMPLATE_ID,
        templateParams,
        EMAILJS_PUBLIC_KEY
      );

      toast.success('Message sent successfully!');
      form.reset();
      router.push('/mail-success');
    } catch (err) {
      clientLogger.error('EmailJS error', err instanceof Error ? err : new Error(String(err)), {
        email,
        subject
      });
      const errorMessage = 'Failed to send message. Please try again.';
      setError(errorMessage);
      toast.error(errorMessage);
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <>
      <section className="overflow-hidden pt-[140px] pb-20 bg-gray-2 dark:bg-gray-900">
        <div className="max-w-[770px] w-full mx-auto px-4 sm:px-8 xl:px-0">
          <div className="bg-white dark:bg-gray-800 rounded-xl shadow-1 p-4 sm:p-7.5 xl:p-10">
            <h1 className="text-2xl font-semibold text-dark dark:text-gray-100 mb-6">
              Contact Us
            </h1>

            {error && (
              <div className="mb-6 p-4 bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-300 rounded-lg">
                {error}
              </div>
            )}

            <form onSubmit={handleSubmit}>
              <FormRow gap={8} className="gap-5 sm:gap-8 mb-5">
                <Input
                  label="First Name"
                  type="text"
                  name="firstName"
                  id="firstName"
                  placeholder="John"
                  defaultValue={firstName}
                  required
                  fullWidth
                  className={inputClassName}
                />

                <Input
                  label="Last Name"
                  type="text"
                  name="lastName"
                  id="lastName"
                  placeholder="Doe"
                  defaultValue={lastName}
                  required
                  fullWidth
                  className={inputClassName}
                />
              </FormRow>

              <FormRow gap={8} className="gap-5 sm:gap-8 mb-5">
                <Input
                  label="Email"
                  type="email"
                  name="email"
                  id="email"
                  placeholder="Enter your email"
                  defaultValue={session?.user?.email || ''}
                  required
                  fullWidth
                  className={inputClassName}
                />

                <Input
                  label="Phone"
                  type="text"
                  name="phone"
                  id="phone"
                  placeholder="Enter your phone"
                  defaultValue={userProfile?.phone || ''}
                  fullWidth
                  className={inputClassName}
                />
              </FormRow>

              <div className="mb-5">
                <Input
                  label="Subject"
                  type="text"
                  name="subject"
                  id="subject"
                  placeholder="Type your subject"
                  required
                  fullWidth
                  className={inputClassName}
                />
              </div>

              <div className="mb-7.5">
                <Input
                  as="textarea"
                  label="Message"
                  name="message"
                  id="message"
                  rows={5}
                  placeholder="Type your message"
                  required
                  fullWidth
                  className="rounded-md border-gray-3 dark:border-gray-600 bg-gray-1 dark:bg-gray-700 placeholder:text-dark-5 dark:placeholder:text-gray-400 dark:text-gray-200 p-5 focus:shadow-input"
                />
              </div>

              <Button
                type="submit"
                variant="primary"
                size="md"
                disabled={isSubmitting}
              >
                {isSubmitting ? 'Sending...' : 'Send Message'}
              </Button>
            </form>
          </div>
        </div>
      </section>
    </>
  );
};
 
export default Contact;