import React from "react";
import { useSelector } from 'react-redux';
import { Swiper, SwiperSlide } from "swiper/react";
import { Navigation, Pagination } from "swiper/modules";
import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";
import { Edit2,Info } from 'lucide-react';
import {
Card,
CardContent,
CardHeader,
Typography,
Box,
Divider,
IconButton,
Button,
Grid,
Tooltip,
} from '@mui/material';
import { usePreviewDetails } from "../hooks/usePreview";
import { isAuthenticated } from "../utills/auth";
const PreviewScreen = ({ onEdit, onSubmit }) => {
const formData = useSelector((state) => state.registerform);
const isAuth = isAuthenticated();
const { data: previewData, isLoading: apiLoading, isError: apiError } = usePreviewDetails(isAuth);
const isLoading = isAuth && apiLoading;
const isError = isAuth && apiError;
const sections = previewData?.personal_details
? [
{
title: "Personal Details",
step: 1,
data: previewData.personal_details,
},
{
title: "Educational & Professional Details",
step: 2,
data: previewData.educational_details,
},
{
title: "Family Details",
step: 3,
data: previewData.family_details,
},
{
title: "Lifestyle & Habits",
step: 4,
data: previewData.lifestyle_details,
},
{
title: "Partner Preferences",
step: 5,
data: previewData.partner_preferences,
},
]
: [
{
title: 'Personal Details',
step: 1,
data: formData.personalDetails,
},
{
title: 'Educational & Professional Details',
step: 2,
data: formData.educationalDetails,
},
{
title: 'Family Details',
step: 3,
data: formData.familyDetails,
},
{
title: 'Lifestyle & Habits',
step: 4,
data: formData.lifestyleDetails,
},
{
title: 'Partner Preferences',
step: 5,
data: formData.partnerPreferences,
},
];
const renderValue = (key, value) => {
if (key === "profiles" || key === "profile" || key === "profile_images" || key === "images") {
const list = Array.isArray(value) ? value : (value ? [value] : []);
return (
{list.length > 0 ? (
list.map((imgObj, index) => {
const src =
typeof imgObj === "string"
? imgObj
: imgObj?.preview ||
imgObj?.url ||
(imgObj?.file ? URL.createObjectURL(imgObj.file) : null);
if (!src) return null;
return (
);
})
) : (
No photos uploaded
)}
);
}
if (Array.isArray(value)) {
if (value.length === 0) return "-";
if (typeof value[0] === "object") {
return value.map((item, idx) => (
{JSON.stringify(item)}
));
}
return value.join(", ");
}
if (value && typeof value === "object") {
return "-";
}
return value || "-";
};
const renderChartGrid = (getDataForCell, title) => {
const renderCell = (i) => {
const items = getDataForCell(i);
const label = Array.isArray(items) ? items.join(', ') : '';
return (
0 ? 'help' : 'default'
}}
>
{items && items.length > 2 ? (
{items.slice(0, 2).join(', ')}
+{items.length - 2}
) : label}
);
};
return (
{title}
{renderCell(1)} {renderCell(2)} {renderCell(3)} {renderCell(4)}
{renderCell(5)}
{title.toUpperCase()}
{renderCell(6)}
{renderCell(7)} {renderCell(8)}
{renderCell(9)} {renderCell(10)} {renderCell(11)} {renderCell(12)}
);
};
return (
{isLoading && (
Loading preview...
)}
{isError && (
Failed to load preview.
)}
{!isLoading &&
sections.map((section) => (
{section.title}
}
action={
onEdit(section.step)}
size="large"
>
}
sx={{padding:"15px 15px", background:"#f5fbff" }}
/>
{Object.entries(section.data || {}).map(([key, value]) => {
// Filter out ID fields
if (key === 'id' || key.endsWith('_id') || key.endsWith('Id') || key.endsWith('_ids') || key.endsWith('Ids') || key === 'created_at' || key === 'updated_at') {
return null;
}
// Handle Horoscope Chart (Server Data)
if (key === 'horoscope' && value && typeof value === 'object') {
return (
Horoscope Details
{renderChartGrid((i) => {
const val = value[`graha_${i}`];
return val ? val.split(',') : [];
}, 'Rasi')}
{renderChartGrid((i) => {
const val = value[`amsam_${i}`];
return val ? val.split(',') : [];
}, 'Navamsam')}
);
}
// Handle Horoscope Chart (Redux Data)
if (key === 'graha' && value) {
return (
{renderChartGrid((i) => value[i] || [], 'Rasi')}
);
}
if (key === 'amsam' && value) {
return (
{renderChartGrid((i) => value[i] || [], 'Navamsam')}
);
}
// Handle brothers and sisters arrays specifically
if ((key === 'brothers' || key === 'sisters') && Array.isArray(value) && value.length > 0) {
return (
{key === 'brothers' ? 'Brothers Details' : 'Sisters Details'}
{value.map((sibling, idx) => (
{Object.entries(sibling).map(([sKey, sVal]) => {
if (sKey === 'id' || sKey.endsWith('_id') || sKey.endsWith('Id') || sKey === 'created_at' || sKey === 'updated_at') return null;
let displayValue = sVal;
if (sKey === 'haveChildrens' || sKey === 'have_childrens') {
if (sVal === true || sVal === 1 || sVal === '1') {
displayValue = 'Yes';
} else if (sVal === false || sVal === 0 || sVal === '0') {
displayValue = 'No';
}
}
return (
{sKey
.replace(/([A-Z])/g, ' $1')
.replace(/_/g, ' ')
.replace(/\b\w/g, (l) => l.toUpperCase())
.trim()}
{displayValue ?? '-'}
)})}
))}
);
}
// Convert camelCase or camel_Snake_case to readable words
const formattedKey = key
.replace(/([A-Z])/g, ' $1')
.replace(/_/g, ' ')
.replace(/\b\w/g, (l) => l.toUpperCase())
.trim();
const content = renderValue(key, value);
return (
{formattedKey}:
{/* Special Case: Profiles Image Preview */}
{key === "profiles" || key === "profile" || key === "profile_images" || key === "images" ? (
content
) : value ? (
{content}
) : (
No data available
)}
);
})}
))}
);
};
export default PreviewScreen;