import React, { useState } from "react";
import { View, ScrollView, TouchableOpacity, Alert } from "react-native";
import {
theme,
AppContainer,
MyText,
tw,
useManualRefresh,
useMarkDataFetchers,
MyTouchableOpacity,
BottomDialog,
} from "common-ui";
import { trpc } from "../../../src/trpc-client";
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
import { LinearGradient } from "expo-linear-gradient";
import dayjs from "dayjs";
import { useRouter } from "expo-router";
interface ProductGroup {
id: number;
groupName: string;
description: string | null;
createdAt: string;
products: any[];
productCount: number;
}
const GroupItem = ({
group,
onEdit,
onDelete,
onViewProducts,
index,
}: {
group: ProductGroup;
onEdit: (group: ProductGroup) => void;
onDelete: (id: number) => void;
onViewProducts: (products: any[]) => void;
index: number;
}) => {
return (
{/* Top Header: Name & Actions */}
Group Name
{group.groupName}
onEdit(group)}
style={tw`p-2 mr-2`}
>
onDelete(group.id)}
style={tw`p-2`}
>
{/* Description */}
{group.description && (
{group.description}
)}
{/* Stats */}
onViewProducts(group.products)}
style={tw`flex-row items-center mr-4`}
>
{group.productCount} Products
{dayjs(group.createdAt).format("MMM DD, YYYY")}
);
};
export default function ProductGroupings() {
const router = useRouter();
const {
data: groupsData,
isLoading,
error,
refetch,
} = trpc.admin.product.getGroups.useQuery();
const deleteGroup = trpc.admin.product.deleteGroup.useMutation();
const groups = groupsData?.groups || [];
const [viewProducts, setViewProducts] = useState(null);
useManualRefresh(refetch);
useMarkDataFetchers(() => {
refetch();
});
const handleCreate = () => {
router.push("/(drawer)/create-product-group");
};
const handleEdit = (group: ProductGroup) => {
router.push(`/(drawer)/edit-product-group/${group.id}`);
};
const handleDelete = (id: number) => {
Alert.alert("Delete Group", "Are you sure you want to delete this group?", [
{ text: "Cancel", style: "cancel" },
{
text: "Delete",
style: "destructive",
onPress: () => {
deleteGroup.mutate(
{ id },
{
onSuccess: () => {
refetch();
},
}
);
},
},
]);
};
if (isLoading) {
return (
Loading product groups...
);
}
if (error) {
return (
Error loading groups
);
}
return (
{groups.length === 0 ? (
No Groups Yet
Start by creating your first product group using the button below.
) : (
groups.map((group, index) => (
{index < groups.length - 1 && (
)}
))
)}
{/* Global Floating Action Button */}
);
}