import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { CopyToClipboard } from 'react-copy-to-clipboard'; const Template = () => { const BASE_URL = process.env.REACT_APP_BASE_URL; const API_KEY = process.env.REACT_APP_API_KEY; const [ownership, setOwnership] = useState([]); const [buttonStates, setButtonStates] = useState([]); const [copiedIndex, setCopiedIndex] = useState(null); const [searchTerm, setSearchTerm] = useState(''); // state for search term const [debouncedSearchTerm, setDebouncedSearchTerm] = useState(''); // state for debounced search term const [errorMessage, setErrorMessage] = useState(null); // state for error message // Debounce search term input useEffect(() => { const timer = setTimeout(() => { setDebouncedSearchTerm(searchTerm); }, 500); // Delay of 500ms before search term is sent return () => clearTimeout(timer); // Clear timeout when searchTerm changes }, [searchTerm]); // Fetch applications based on the search term useEffect(() => { let isMounted = true; const fetchApplications = async () => { if (debouncedSearchTerm) { // Fetch by search term try { const response = await fetch(`${BASE_URL}/application/get-by-name/${debouncedSearchTerm}`, { method: 'GET', headers: { 'accept': 'application/json', 'x-api-key': API_KEY, }, }); const result = await response.json(); if (response.ok) { if (result.details && result.details.data) { if (isMounted) { setOwnership([result.details.data]); setButtonStates([{ isHovered: false, isActive: false }]); setErrorMessage(null); // Reset error if data found } } else { if (isMounted) { setOwnership([]); setErrorMessage('Data is not found.'); } } } else { if (isMounted) { setOwnership([]); setErrorMessage('Data is not found.'); } } } catch (error) { console.error('Error fetching applications:', error); if (isMounted) { setOwnership([]); setErrorMessage('Data is not found.'); } } } else { // Fetch all applications when there's no search term const fetchAllApplications = async () => { try { const response = await fetch(`${BASE_URL}/application/list`, { method: 'GET', headers: { 'x-api-key': API_KEY, }, }); const result = await response.json(); if (response.ok && result.status_code === 200) { if (isMounted) { setOwnership(result.details.data); setButtonStates(result.details.data.map(() => ({ isHovered: false, isActive: false }))); setErrorMessage(null); // Reset error if data found } } else { if (isMounted) { setOwnership([]); setErrorMessage('Error fetching data. Please try again.'); } } } catch (error) { console.error('Error fetching all applications:', error); if (isMounted) { setOwnership([]); setErrorMessage('Error fetching data. Please try again.'); } } }; fetchAllApplications(); } }; fetchApplications(); return () => { isMounted = false; // Set the flag to false on cleanup }; }, [debouncedSearchTerm]); // Trigger the effect when `debouncedSearchTerm` changes const handleMouseEnter = (index) => { setButtonStates((prevStates) => { const newStates = [...prevStates]; newStates[index].isHovered = true; return newStates; }); }; const handleMouseLeave = (index) => { setButtonStates((prevStates) => { const newStates = [...prevStates]; newStates[index].isHovered = false; return newStates; }); }; const handleChange = (event) => { setSearchTerm(event.target.value); }; return (
{/* Welcome Message */}

Create WhatsApp Business Templates

Design pre-approved message templates tailored for various business needs, such as customer support, appointment reminders, order updates, and more. Streamline communication and ensure consistency with ready-to-use templates.

{/* Filtering Options */}
{/* Search Bar */}
{/* Application Cards */}
{/* Show message if no data or search result */} {ownership.length === 0 && !errorMessage &&

Data is not found.

} {ownership.map((item, index) => (
{item.name}

WABA Ownership:

{item.name}

{/* Copy to Clipboard Button */} { setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); // Hide "Copied" status after 2 seconds }} >
))}
{/* Show error message */} {errorMessage &&

{errorMessage}

}
); }; const styles = { container: { margin: '20px auto', maxWidth: '1200px', }, welcomeCard: { backgroundColor: '#F1F3F6', borderColor: '#1e2b34', borderRadius: '5px', marginBottom: '20px', }, createButton: { backgroundColor: '#1d3f68', color: '#fff', borderRadius: '5px', padding: '12px 20px', width: '150px', display: 'flex', justifyContent: 'center', alignItems: 'center', }, wrapperCard: { padding: '20px', }, searchInputWrapper: { display: 'flex', alignItems: 'center', marginTop: '20px', marginBottom: '20px', }, searchInput: { padding: '10px', border: '1px solid #ccc', borderRadius: '5px', width: '300px', marginRight: '10px', }, searchButton: { backgroundColor: '#1d3f68', border: 'none', color: 'white', padding: '10px 15px', cursor: 'pointer', }, searchIcon: { fontSize: '16px', }, cardContainer: { display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(300px, 1fr))', gap: '20px', }, card: { backgroundColor: '#fff', border: '1px solid #ddd', borderRadius: '10px', padding: '20px', boxShadow: '0 2px 4px rgba(0, 0, 0, 0.1)', }, cardHeader: { marginBottom: '10px', }, cardTitle: { fontSize: '18px', fontWeight: 'bold', }, cardContent: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', }, applicationIdWrapper: { display: 'flex', flexDirection: 'column', }, noDataMessage: { color: 'gray', }, errorMessage: { color: 'red', } }; export default Template;