import throttle from "lodash/throttle";
import { useCallback, useEffect, useState } from "react";
import { TouchableOpacity, View } from "react-native";
import { StyledImage, StyledText } from "..";
import { sendAsAnalyticsEvent } from "../../../App/common/hooks/useAnalyticsEvent";
import {
Cell as CellData,
CellProps,
Column,
ColumnsProps,
Row,
RowsProps,
TableWidgetProps,
} from "../../../App/common/interface/widgets/widgetData";
import { styles } from "./TableWidgetStyles";
const Cell = ({
widgetData,
cell,
index,
handleClick,
handleActions,
}: CellProps) => {
useEffect(() => {
cell?.analyticEvents?.onViewEvent &&
sendAsAnalyticsEvent(cell?.analyticEvents?.onViewEvent);
}, []);
const throttledHandleActions = useCallback(
throttle(() => handleActions(null, cell?.action), 700, {
leading: true,
trailing: false,
}),
[widgetData],
);
const cellAction = () => {
throttledHandleActions();
};
const handleTableCellClick = () => {
if (cell?.cta) {
handleClick && handleClick(cell?.cta);
} else if (cell?.action) {
cellAction();
}
};
return (
{cell?.title?.text && (
)}
{cell?.image?.url && (
)}
);
};
const Rows = ({
widgetData,
rowsToDisplay,
handleClick,
handleActions,
}: RowsProps) => {
return (
<>
{widgetData?.rows &&
widgetData?.rows
?.slice(0, rowsToDisplay)
?.map((row: Row, index: number) => {
return (
{row?.cells?.map((cell: CellData, index: number) => (
|
))}
);
})}
>
);
};
const Columns = ({ widgetData }: ColumnsProps) => {
return (
{widgetData?.columns &&
widgetData?.columns?.map((column: Column, index: number) => {
return (
{column?.image?.url && (
)}
{column?.subImage?.url && (
)}
{column?.title?.text && (
)}
{column?.subtitle?.text && (
)}
);
})}
);
};
const Table = ({
widgetData,
handleClick,
handleActions,
}: TableWidgetProps) => {
const [rowsToDisplay, setRowsToDisplay] = useState(
widgetData?.rowsToDisplay || widgetData?.rows?.length,
);
const handleSelect = () => {
setRowsToDisplay(widgetData?.rows?.length);
widgetData?.viewButton?.cta?.analyticsEventProperties &&
sendAsAnalyticsEvent(
widgetData?.viewButton?.cta?.analyticsEventProperties,
);
};
return (
<>
{rowsToDisplay !== widgetData?.rows?.length &&
widgetData.viewButton?.text && (
)}
>
);
};
export default Table;