implemented add data using spreadsheet functionality
This commit is contained in:
parent
645a89aa43
commit
7dc6aace7a
@ -37,6 +37,7 @@ const AddCampaign = () => {
|
||||
language: "",
|
||||
campaignType: "",
|
||||
video: null,
|
||||
spreadSheet: null,
|
||||
recipients: [{ name: "", phoneNumber: "" }],
|
||||
});
|
||||
|
||||
|
@ -2,17 +2,56 @@ import React from "react";
|
||||
import Button from "@material-ui/core/Button";
|
||||
import { useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { CFormInput, CFormLabel, CCol, CRow } from "@coreui/react";
|
||||
|
||||
const ContactDetails = ({ props }) => {
|
||||
const { data, setData, handleView } = props;
|
||||
const [dataEntryMethod, setDataEntryMethod] = useState("manual");
|
||||
const [csvData, setCsvData] = useState([]);
|
||||
// const [recipients, setRecipients] = useState([{ name: "", phoneNumber: "" }]);
|
||||
|
||||
// console.log("data", data);
|
||||
const addRecord = () => {
|
||||
setData((prevData) => ({
|
||||
...prevData,
|
||||
recipients: [...prevData.recipients, { name: "", phoneNumber: "" }],
|
||||
recipients: [
|
||||
...prevData.recipients,
|
||||
{ name: "", phoneNumber: "", email: "" },
|
||||
],
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSpreadSheet = (e) => {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = (event) => {
|
||||
const csvData = event.target.result;
|
||||
const rows = csvData.split("\n");
|
||||
const extractedData = [];
|
||||
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
const row = rows[i].split(",");
|
||||
if (row.length >= 2) {
|
||||
const name = row[0].trim();
|
||||
const email = row[1].trim();
|
||||
if (name && email) {
|
||||
extractedData.push({ name, email });
|
||||
}
|
||||
}
|
||||
}
|
||||
setCsvData(extractedData);
|
||||
// console.log(csvData);
|
||||
setData((prevData) => ({
|
||||
...prevData,
|
||||
recipients: extractedData,
|
||||
spreadSheet: file.name,
|
||||
}));
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
};
|
||||
|
||||
const deleteRecipient = (index) => {
|
||||
const updatedRecipients = [...data.recipients];
|
||||
updatedRecipients.splice(index, 1);
|
||||
@ -46,25 +85,52 @@ const ContactDetails = ({ props }) => {
|
||||
}));
|
||||
};
|
||||
|
||||
const recipientEmailChange = (e, index) => {
|
||||
const updatedRecipients = [...data.recipients];
|
||||
updatedRecipients[index] = {
|
||||
...updatedRecipients[index],
|
||||
email: e.target.value,
|
||||
};
|
||||
setData((prevData) => ({
|
||||
...prevData,
|
||||
recipients: updatedRecipients,
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (
|
||||
data?.recipients.every(
|
||||
(recipient) =>
|
||||
recipient.name !== "" &&
|
||||
(data?.campaignType !== "email"
|
||||
? recipient.phoneNumber !== ""
|
||||
: recipient.email !== "")
|
||||
)
|
||||
) {
|
||||
handleView(3);
|
||||
} else {
|
||||
toast.error("Fill all contact details");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
<div>
|
||||
<div className="row">
|
||||
<div className="col-12">
|
||||
<div
|
||||
className="
|
||||
page-title-box
|
||||
d-flex
|
||||
align-items-center
|
||||
justify-content-between
|
||||
"
|
||||
<div className="page-title-box d-flex align-items-center justify-content-between">
|
||||
<div>
|
||||
<label>
|
||||
Data Entry Method:
|
||||
<select
|
||||
value={dataEntryMethod}
|
||||
onChange={(e) => setDataEntryMethod(e.target.value)}
|
||||
>
|
||||
<div style={{ fontSize: "22px" }} className="fw-bold">
|
||||
Contact Details
|
||||
<option value="manual">Manually</option>
|
||||
<option value="spreadsheet">Using Spreadsheet</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: "1rem" }}>
|
||||
<h4 className="mb-0"></h4>
|
||||
</div>
|
||||
|
||||
<div className="page-title-right">
|
||||
<Button
|
||||
variant="contained"
|
||||
@ -87,18 +153,7 @@ const ContactDetails = ({ props }) => {
|
||||
marginBottom: "1rem",
|
||||
textTransform: "capitalize",
|
||||
}}
|
||||
onClick={() => {
|
||||
if (
|
||||
data?.recipients.every(
|
||||
(recipient) =>
|
||||
recipient.name !== "" && recipient.phoneNumber !== ""
|
||||
)
|
||||
) {
|
||||
handleView(3);
|
||||
} else {
|
||||
toast.error("Fill all contact details");
|
||||
}
|
||||
}}
|
||||
onClick={handleSubmit}
|
||||
>
|
||||
Next
|
||||
</Button>
|
||||
@ -106,30 +161,19 @@ const ContactDetails = ({ props }) => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div>
|
||||
{dataEntryMethod === "manual" && (
|
||||
<div className="row">
|
||||
<div className="col-sm-12 col-md-12 col-lg-12 my-1">
|
||||
<div className="col-md-12 my-1">
|
||||
<div className="card h-100">
|
||||
<div className="card-body px-5">
|
||||
{data?.recipients.map((recipient, index) => {
|
||||
return (
|
||||
<div className="mb-3 border p-3 rounded">
|
||||
<label
|
||||
htmlFor="title"
|
||||
className="form-label d-flex justify-content-between"
|
||||
>
|
||||
<p>Name</p>
|
||||
|
||||
{index === 0 ? null : (
|
||||
<button
|
||||
onClick={() => {
|
||||
deleteRecipient(index);
|
||||
}}
|
||||
className="btn btn-danger btn-smn rounded-5 fw-bold"
|
||||
>
|
||||
delete
|
||||
</button>
|
||||
)}
|
||||
<div className="row mb-3 border p-3 rounded">
|
||||
<div className="col-md-6 d-flex align-items-center">
|
||||
<label htmlFor="title" className="form-label me-2">
|
||||
Name
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
@ -139,27 +183,55 @@ const ContactDetails = ({ props }) => {
|
||||
onChange={(e) => recipientNameChange(e, index)}
|
||||
maxLength="50"
|
||||
/>
|
||||
|
||||
<label htmlFor="title" className="form-label mt-2">
|
||||
</div>
|
||||
<div className="col-md-6 d-flex align-items-center">
|
||||
<label htmlFor="title" className="form-label me-2">
|
||||
{data?.campaignType === "rcs" ||
|
||||
data?.campaignType === "whatsapp" ? (
|
||||
<p>Phone Number</p>
|
||||
) : (
|
||||
<p>Email</p>
|
||||
)}
|
||||
data?.campaignType === "whatsapp"
|
||||
? "Phone Number"
|
||||
: "Email"}
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
type={
|
||||
data?.campaignType === "rcs" ||
|
||||
data?.campaignType === "whatsapp"
|
||||
? "number"
|
||||
: "email"
|
||||
}
|
||||
className="form-control"
|
||||
id="recipients-phone-number"
|
||||
id={`recipients-phone-number-${index}`}
|
||||
maxLength="50"
|
||||
name={`toPhoneNumber-${index}`}
|
||||
value={recipient?.phoneNumber}
|
||||
onChange={(e) => recipientNumberChange(e, index)}
|
||||
value={
|
||||
data?.campaignType === "rcs" ||
|
||||
data?.campaignType === "whatsapp"
|
||||
? recipient?.phoneNumber
|
||||
: recipient?.email
|
||||
}
|
||||
onChange={(e) =>
|
||||
data?.campaignType === "rcs" ||
|
||||
data?.campaignType === "whatsapp"
|
||||
? recipientNumberChange(e, index)
|
||||
: recipientEmailChange(e, index)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{index !== 0 && (
|
||||
<div className="col-12">
|
||||
<button
|
||||
onClick={() => {
|
||||
deleteRecipient(index);
|
||||
}}
|
||||
className="btn btn-danger btn-sm rounded-5 fw-bold mt-2"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
<div className="col-md-12">
|
||||
<button
|
||||
onClick={() => {
|
||||
addRecord();
|
||||
@ -173,6 +245,35 @@ const ContactDetails = ({ props }) => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{dataEntryMethod === "spreadsheet" && (
|
||||
<div className="row">
|
||||
<div className="col-md-12 my-1">
|
||||
{/* Spreadsheet data entry form */}
|
||||
<div className="card h-100">
|
||||
<div className="card-body px-5">
|
||||
<div className="row mb-3 border p-3 rounded">
|
||||
<div className="mb-3">
|
||||
<label htmlFor="title" className="form-label">
|
||||
Upload Spreadsheet
|
||||
</label>
|
||||
<input
|
||||
type="file"
|
||||
className="form-control"
|
||||
id="spreadsheet"
|
||||
accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
|
||||
onChange={(e) => handleSpreadSheet(e)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -68,8 +68,11 @@ const Preview = ({ props }) => {
|
||||
<td>{data?.campaignType}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="col"> Video</th>
|
||||
<th scope="col">
|
||||
{data?.campaignType === "email" ? "Video" : "Spreadsheet"}
|
||||
</th>
|
||||
<td>
|
||||
{data?.campaignType === "email" ? (
|
||||
<video
|
||||
className="rounded"
|
||||
autoPlay={true}
|
||||
@ -77,6 +80,9 @@ const Preview = ({ props }) => {
|
||||
width={250}
|
||||
src={data?.video}
|
||||
></video>
|
||||
) : (
|
||||
<td>{data?.spreadSheet}</td>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
Loading…
Reference in New Issue
Block a user