From c077da728c77e449040d162e4e5f2b4f903d6496 Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Wed, 30 Oct 2024 17:15:56 +0800 Subject: [PATCH] [#126] Add search farmer --- frontend/src/app/broadcast-message/page.js | 82 ++++++++++++++-------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/frontend/src/app/broadcast-message/page.js b/frontend/src/app/broadcast-message/page.js index 43c229b0..1a8c5ebf 100644 --- a/frontend/src/app/broadcast-message/page.js +++ b/frontend/src/app/broadcast-message/page.js @@ -5,7 +5,7 @@ import { BackIcon } from "@/utils/icons"; import { ChatHeader, Notification } from "@/components"; import { useUserContext } from "@/context/UserContextProvider"; import { ButtonLoadingIcon } from "@/utils/icons"; -import { useState } from "react"; +import { useState, useMemo, useEffect } from "react"; import { api } from "@/lib"; const MAX_CHARACTERS = 1600; @@ -19,28 +19,46 @@ const BroadcastMessage = () => { const [disabled, setDisabled] = useState(false); const [notificationContent, setNotificationContent] = useState(""); const [showNotification, setShowNotification] = useState(false); + const [searchClient, setSearchClient] = useState(""); + + useEffect(() => { + setSelectAll(selectedClients.length === clients.length); + }, [clients, selectedClients]); const handleOnClickBack = () => { router.replace("/chats"); }; - const handleClientChange = (event) => { - const value = Array.from(event.target.options) - .filter((option) => option.selected) - .map((option) => option.value); - setSelectedClients(value); - // Update selectAll state - setSelectAll(value.length === clients.length); + const handleClientChange = (clientId) => { + setSelectedClients((prevSelectedClients) => { + if (prevSelectedClients.includes(clientId)) { + // If client is already selected, remove it + return prevSelectedClients.filter((id) => id !== clientId); + } else { + // Otherwise, add the client to selectedClients + return [...prevSelectedClients, clientId]; + } + }); + setSearchClient(""); + }; + + const handleSearchClient = (event) => { + setSearchClient(event.target.value); }; + const filteredClients = useMemo(() => { + if (!searchClient) return clients; + return clients.filter( + (client) => + client.name.toLowerCase().includes(searchClient.toLowerCase()) || + client.phone_number.includes(searchClient) + ); + }, [searchClient, clients]); + const handleSelectAllChange = (event) => { const isChecked = event.target.checked; setSelectAll(isChecked); - if (isChecked) { - setSelectedClients(clients.map((client) => String(client.id))); - } else { - setSelectedClients([]); - } + setSelectedClients(isChecked ? clients.map((client) => client.id) : []); }; const handleMessageChange = (e) => { @@ -80,15 +98,10 @@ const BroadcastMessage = () => { } const requestData = { - message: message, + message, contacts: clients - .map((c) => { - if (selectedClients.includes(String(c.id))) { - return c.phone_number; - } - return false; - }) - .filter((x) => x), + .filter((c) => selectedClients.includes(c.id)) + .map((c) => c.phone_number), }; try { @@ -102,7 +115,7 @@ const BroadcastMessage = () => { setTimeout(() => { setDisabled(false); handleClearForm(); - }); + }, 1000); } else { setNotificationContent("Error, please try again later."); handleShowNotification(); @@ -128,10 +141,11 @@ const BroadcastMessage = () => { } />
-
handleSubmit(e)} className="p-10 w-full"> +

Send Broadcast Message

+ {/* Client List */}
@@ -145,20 +159,32 @@ const BroadcastMessage = () => { />
+
+