implemented functionality to upload files via spreadsheet also

This commit is contained in:
Pratish Ninawe 2023-09-22 18:46:04 +05:30
parent 645a89aa43
commit 5c3af38ebc
4 changed files with 235 additions and 127 deletions

View File

@ -138,7 +138,7 @@ const AddCampaign = () => {
// });
// };
console.log(data);
// console.log(data);
return (
<CContainer>
@ -153,7 +153,7 @@ const AddCampaign = () => {
"
>
<div style={{ fontSize: "22px" }} className="fw-bold">
Add Campaign
Contact Details
</div>
<div className="page-title-right">
<div className="page-title-right">

View File

@ -2,6 +2,7 @@ import React from "react";
import { useState } from "react";
import Button from "@material-ui/core/Button";
import toast from "react-hot-toast";
const BasicDetaiils = ({ props }) => {
const { data, setData, handleView } = props;

View File

@ -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,131 +85,193 @@ 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 className="row">
<div className="col-12">
<div
className="
page-title-box
d-flex
align-items-center
justify-content-between
"
>
<div style={{ fontSize: "22px" }} className="fw-bold">
Contact Details
</div>
<div style={{ display: "flex", gap: "1rem" }}>
<h4 className="mb-0"></h4>
</div>
<div className="page-title-right">
<Button
variant="contained"
color="secondary"
style={{
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
marginRight: "5px",
}}
onClick={() => handleView(1)}
>
Prev
</Button>
<Button
variant="contained"
color="secondary"
style={{
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
}}
onClick={() => {
if (
data?.recipients.every(
(recipient) =>
recipient.name !== "" && recipient.phoneNumber !== ""
)
) {
handleView(3);
} else {
toast.error("Fill all contact details");
}
}}
>
Next
</Button>
<div>
<div className="row">
<div className="col-12">
<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)}
>
<option value="manual">Manually</option>
<option value="spreadsheet">Using Spreadsheet</option>
</select>
</label>
</div>
<div className="page-title-right">
<Button
variant="contained"
color="secondary"
style={{
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
marginRight: "5px",
}}
onClick={() => handleView(1)}
>
Prev
</Button>
<Button
variant="contained"
color="secondary"
style={{
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
}}
onClick={handleSubmit}
>
Next
</Button>
</div>
</div>
</div>
</div>
</div>
<div className="row">
<div className="col-sm-12 col-md-12 col-lg-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"
<div>
{dataEntryMethod === "manual" && (
<div className="row">
<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="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"
className="form-control"
name={`name-${index}`}
value={recipient?.name}
onChange={(e) => recipientNameChange(e, index)}
maxLength="50"
/>
</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"
? "Phone Number"
: "Email"}
</label>
<input
type={
data?.campaignType === "rcs" ||
data?.campaignType === "whatsapp"
? "number"
: "email"
}
className="form-control"
id={`recipients-phone-number-${index}`}
maxLength="50"
name={`toPhoneNumber-${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();
}}
className="btn btn-secondary"
>
<p>Name</p>
{index === 0 ? null : (
<button
onClick={() => {
deleteRecipient(index);
}}
className="btn btn-danger btn-smn rounded-5 fw-bold"
>
delete
</button>
)}
</label>
<input
type="text"
className="form-control"
name={`name-${index}`}
value={recipient?.name}
onChange={(e) => recipientNameChange(e, index)}
maxLength="50"
/>
<label htmlFor="title" className="form-label mt-2">
{data?.campaignType === "rcs" ||
data?.campaignType === "whatsapp" ? (
<p>Phone Number</p>
) : (
<p>Email</p>
)}
</label>
<input
type="number"
className="form-control"
id="recipients-phone-number"
maxLength="50"
name={`toPhoneNumber-${index}`}
value={recipient?.phoneNumber}
onChange={(e) => recipientNumberChange(e, index)}
/>
Add another record
</button>
</div>
);
})}
<button
onClick={() => {
addRecord();
}}
className="btn btn-secondary"
>
Add another record
</button>
</div>
</div>
</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>
);

View File

@ -68,15 +68,21 @@ const Preview = ({ props }) => {
<td>{data?.campaignType}</td>
</tr>
<tr>
<th scope="col"> Video</th>
<th scope="col">
{data?.campaignType === "email" ? "Video" : "Spreadsheet"}
</th>
<td>
<video
className="rounded"
autoPlay={true}
height={300}
width={250}
src={data?.video}
></video>
{data?.campaignType === "email" ? (
<video
className="rounded"
autoPlay={true}
height={300}
width={250}
src={data?.video}
></video>
) : (
<div>{data?.spreadSheet}</div>
)}
</td>
</tr>
<tr>