feat(reviews): auto-fetch Google auth credentials in review modal with 1-click continue

This commit is contained in:
iFixKart Dev 2026-11-17 14:47:18 +05:30
parent a1c21fead2
commit 86b881441b
7 changed files with 133 additions and 10 deletions

View file

@ -188,3 +188,4 @@ export function ProductReviewsSection({
</div> </div>
); );
} }

View file

@ -121,3 +121,4 @@ export function ReviewCard({ review }: { review: ProductReviewItem }) {
</div> </div>
); );
} }

View file

@ -4,6 +4,7 @@ import React, { useState, useEffect } from 'react';
import { Star, X, Upload, CheckCircle2, AlertCircle, Loader2 } from 'lucide-react'; import { Star, X, Upload, CheckCircle2, AlertCircle, Loader2 } from 'lucide-react';
import { useAuthStore } from '@/store/authStore'; import { useAuthStore } from '@/store/authStore';
import { reviewService, ProductReviewItem } from '@/services/api/reviewService'; import { reviewService, ProductReviewItem } from '@/services/api/reviewService';
import GoogleAuthModal from '@/components/auth/GoogleAuthModal';
interface ReviewSubmissionModalProps { interface ReviewSubmissionModalProps {
productId: string; productId: string;
@ -21,6 +22,29 @@ const RATING_DESCRIPTIONS: Record<number, string> = {
5: 'Excellent - Highly recommended!', 5: 'Excellent - Highly recommended!',
}; };
function GoogleIcon({ className = 'w-4 h-4' }: { className?: string }) {
return (
<svg className={className} viewBox="0 0 24 24" aria-hidden>
<path
fill="#4285F4"
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
/>
<path
fill="#34A853"
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
/>
<path
fill="#FBBC05"
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.06H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.94l2.85-2.22.81-.63z"
/>
<path
fill="#EA4335"
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.06l3.66 2.84c.87-2.6 3.3-4.52 6.16-4.52z"
/>
</svg>
);
}
export function ReviewSubmissionModal({ export function ReviewSubmissionModal({
productId, productId,
productName, productName,
@ -29,6 +53,7 @@ export function ReviewSubmissionModal({
onReviewSubmitted, onReviewSubmitted,
}: ReviewSubmissionModalProps) { }: ReviewSubmissionModalProps) {
const customer = useAuthStore((s) => s.customer); const customer = useAuthStore((s) => s.customer);
const bootstrapAuth = useAuthStore((s) => s.bootstrapAuth);
const [rating, setRating] = useState<number>(5); const [rating, setRating] = useState<number>(5);
const [hoverRating, setHoverRating] = useState<number | null>(null); const [hoverRating, setHoverRating] = useState<number | null>(null);
@ -41,14 +66,34 @@ export function ReviewSubmissionModal({
const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false);
const [errorMsg, setErrorMsg] = useState<string | null>(null); const [errorMsg, setErrorMsg] = useState<string | null>(null);
const [isSuccess, setIsSuccess] = useState(false); const [isSuccess, setIsSuccess] = useState(false);
const [showGoogleAuthModal, setShowGoogleAuthModal] = useState(false);
const [isGoogleAutofilled, setIsGoogleAutofilled] = useState(false);
// Restore session when modal opens
useEffect(() => {
if (isOpen) {
void bootstrapAuth();
}
}, [isOpen, bootstrapAuth]);
// Autofill name & email from logged in customer
useEffect(() => { useEffect(() => {
if (customer) { if (customer) {
const fullName = [customer.first_name, customer.last_name].filter(Boolean).join(' '); const resolvedName =
if (fullName) setAuthorName(fullName); (customer as any).name ||
if (customer.email) setAuthorEmail(customer.email); (customer as any).full_name ||
[customer.first_name, customer.last_name].filter(Boolean).join(' ') ||
customer.first_name ||
'';
if (resolvedName) {
setAuthorName((prev) => (prev ? prev : resolvedName));
} }
}, [customer]); if (customer.email) {
setAuthorEmail((prev) => (prev ? prev : customer.email));
}
setIsGoogleAutofilled(true);
}
}, [customer, isOpen]);
if (!isOpen) return null; if (!isOpen) return null;
@ -226,12 +271,57 @@ export function ReviewSubmissionModal({
/> />
</div> </div>
{/* Google Autofill Banner / Action */}
{customer?.email ? (
<div className="flex items-center justify-between p-2.5 rounded-xl bg-blue-50/80 border border-blue-200/80 text-xs">
<div className="flex items-center gap-2 min-w-0">
<div className="w-6 h-6 rounded-full bg-white shadow-xs border border-blue-200 flex items-center justify-center shrink-0">
<GoogleIcon className="w-3.5 h-3.5" />
</div>
<span className="truncate text-blue-950">
Auto-filled from Google: <strong className="font-semibold text-blue-900">{customer.email}</strong>
</span>
</div>
<span className="inline-flex items-center gap-1 text-[10px] font-bold uppercase tracking-wider text-emerald-700 bg-emerald-50 px-2 py-0.5 rounded-full border border-emerald-200 shrink-0">
<CheckCircle2 size={11} />
Verified Sync
</span>
</div>
) : (
<div className="flex items-center justify-between p-2.5 sm:p-3 rounded-xl bg-gradient-to-r from-gray-50 to-blue-50/40 border border-gray-200 hover:border-blue-300 transition-colors">
<div className="flex items-center gap-2.5 min-w-0">
<div className="w-7 h-7 rounded-full bg-white shadow-xs border border-gray-200 flex items-center justify-center shrink-0">
<GoogleIcon className="w-4 h-4" />
</div>
<div className="text-left">
<p className="text-xs font-semibold text-gray-800">Auto-fill with Google</p>
<p className="text-[11px] text-gray-500 hidden sm:block">1-click fetch name & verified buyer check</p>
</div>
</div>
<button
type="button"
onClick={() => setShowGoogleAuthModal(true)}
className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-white hover:bg-gray-50 text-gray-700 hover:text-gray-900 border border-gray-300 rounded-lg text-xs font-bold shadow-2xs transition-all cursor-pointer hover:border-gray-400 shrink-0"
>
<GoogleIcon className="w-3.5 h-3.5" />
<span>Continue with Google</span>
</button>
</div>
)}
{/* Name and Email */} {/* Name and Email */}
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3"> <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
<div> <div>
<label className="block text-xs font-semibold text-gray-700 mb-1"> <div className="flex items-center justify-between mb-1">
<label className="block text-xs font-semibold text-gray-700">
Your Name * Your Name *
</label> </label>
{isGoogleAutofilled && authorName && (
<span className="text-[10px] text-blue-600 font-medium inline-flex items-center gap-1">
<GoogleIcon className="w-2.5 h-2.5" /> Auto-filled
</span>
)}
</div>
<input <input
type="text" type="text"
value={authorName} value={authorName}
@ -242,9 +332,16 @@ export function ReviewSubmissionModal({
/> />
</div> </div>
<div> <div>
<label className="block text-xs font-semibold text-gray-700 mb-1"> <div className="flex items-center justify-between mb-1">
<label className="block text-xs font-semibold text-gray-700 truncate">
Email (Used for Verified Buyer check) Email (Used for Verified Buyer check)
</label> </label>
{isGoogleAutofilled && authorEmail && (
<span className="text-[10px] text-emerald-600 font-medium inline-flex items-center gap-1">
<CheckCircle2 size={10} /> Verified
</span>
)}
</div>
<input <input
type="email" type="email"
value={authorEmail} value={authorEmail}
@ -320,6 +417,25 @@ export function ReviewSubmissionModal({
</form> </form>
)} )}
</div> </div>
{/* Google Auth Modal for 1-Click Auto-fill */}
<GoogleAuthModal
isOpen={showGoogleAuthModal}
onClose={() => setShowGoogleAuthModal(false)}
onSuccess={(data) => {
const resolvedName =
[data.first_name, data.last_name].filter(Boolean).join(' ') ||
data.first_name ||
data.name ||
data.email?.split('@')[0] ||
'';
if (resolvedName) setAuthorName(resolvedName);
if (data.email) setAuthorEmail(data.email);
setIsGoogleAutofilled(true);
setShowGoogleAuthModal(false);
}}
/>
</div> </div>
); );
} }

View file

@ -102,3 +102,4 @@ export function ReviewSummaryBreakdown({
</div> </div>
); );
} }

View file

@ -155,6 +155,7 @@ export interface RestoredSession {
customer_id: string; customer_id: string;
email: string; email: string;
first_name: string; first_name: string;
last_name?: string;
} }
/** /**
@ -177,6 +178,7 @@ export async function restoreCustomerSession(): Promise<RestoredSession | null>
customer_id: data.customer_id, customer_id: data.customer_id,
email: data.email, email: data.email,
first_name: data.first_name, first_name: data.first_name,
last_name: data.last_name,
}; };
} catch { } catch {
return null; return null;

View file

@ -100,3 +100,4 @@ export const reviewService = {
return data.file_url || data.url || ''; return data.file_url || data.url || '';
}, },
}; };

View file

@ -111,6 +111,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
customer_id: restored.customer_id, customer_id: restored.customer_id,
email: restored.email, email: restored.email,
first_name: restored.first_name, first_name: restored.first_name,
last_name: restored.last_name,
}; };
set({ set({
accessToken: restored.access_token, accessToken: restored.access_token,