worked on preview files

This commit is contained in:
Pratish Ninawe 2023-10-03 10:13:34 +05:30
parent 7794ada908
commit 2238dc074d
3 changed files with 81 additions and 67 deletions

View File

@ -3,8 +3,11 @@ import Button from "@material-ui/core/Button";
import { useState } from "react"; import { useState } from "react";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { CFormInput, CFormLabel, CCol, CRow } from "@coreui/react"; import { CFormInput, CFormLabel, CCol, CRow } from "@coreui/react";
import axios from "axios";
import { isAutheticated } from "src/auth";
const ContactDetails = ({ props }) => { const ContactDetails = ({ props }) => {
const token = isAutheticated();
const { data, setData, handleView } = props; const { data, setData, handleView } = props;
const [dataEntryMethod, setDataEntryMethod] = useState("manual"); const [dataEntryMethod, setDataEntryMethod] = useState("manual");
const [csvData, setCsvData] = useState([]); const [csvData, setCsvData] = useState([]);
@ -12,10 +15,7 @@ const ContactDetails = ({ props }) => {
const addRecord = () => { const addRecord = () => {
setData((prevData) => ({ setData((prevData) => ({
...prevData, ...prevData,
recipients: [ recipients: [...prevData.recipients, { name: "", contact: "" }],
...prevData.recipients,
{ name: "", contact: "" }, // Initialize contact as an empty string
],
})); }));
}; };
@ -83,16 +83,59 @@ const ContactDetails = ({ props }) => {
})); }));
}; };
const handleSubmit = () => { const handleSubmit = (e) => {
if ( e.preventDefault();
data?.recipients.every(
(recipient) => recipient.name !== "" && recipient.contact !== "" const hasEmptyRecipients = data.recipients.some((recipient) => {
) return !recipient.name || !recipient.contact;
) { });
handleView(4);
} else { if (hasEmptyRecipients) {
toast.error("Fill all contact details"); toast.error("Please fill Conatct details");
return;
} }
const formattedRecipients = data.recipients.map((recipient) => ({
name: recipient.name,
contact: recipient.contact,
}));
const campaignData = {
campaignType: data.campaignType,
campaignName: data.campaignName,
language: data.language,
recipients: formattedRecipients,
};
axios
.post(`/api/campaign/create`, campaignData, {
headers: {
Authorization: `Bearer ${token}`,
"Access-Control-Allow-Origin": "*",
},
})
.then((res) => {
// console.log(res);
handleView(4);
toast.success("Campaign added successfully!");
setLoading(false);
})
.catch((err) => {
setLoading(false);
const message = err.response?.data?.message || "Something went wrong!";
// console.log(message);
toast.error(message);
});
// if (
// data?.recipients.every(
// (recipient) => recipient.name !== "" && recipient.contact !== ""
// )
// ) {
// handleView(4);
// } else {
// toast.error("Fill all contact details");
// }
}; };
return ( return (

View File

@ -1,4 +1,4 @@
import React, { useState } from "react"; import React, { useEffect, useState } from "react";
import Button from "@material-ui/core/Button"; import Button from "@material-ui/core/Button";
import axios from "axios"; import axios from "axios";
import { isAutheticated } from "src/auth"; import { isAutheticated } from "src/auth";
@ -7,71 +7,44 @@ const Preview = ({ props }) => {
const token = isAutheticated(); const token = isAutheticated();
const { data, handleView, setData } = props; const { data, handleView, setData } = props;
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [campaignData, setCampaignData] = useState([]);
const handleSubmit = async (e) => { const handleSubmit = async (e) => {
e.preventDefault(); e.preventDefault();
const hasEmptyRecipients = data.recipients.some((recipient) => { if (
return !recipient.name || !recipient.contact; data?.recipients.every(
}); (recipient) => recipient.name !== "" && recipient.contact !== ""
)
if (hasEmptyRecipients) { ) {
swal({ handleView(4);
title: "Validation Error", } else {
text: "Please fill Conatct details", toast.error("Fill all contact details");
icon: "error",
button: "Close",
});
return;
} }
};
const formattedRecipients = data.recipients.map((recipient) => ({ const getCampaign = () => {
name: recipient.name,
contact: recipient.contact,
}));
const campaignData = {
campaignType: data.campaignType,
campaignName: data.campaignName,
language: data.language,
recipients: formattedRecipients,
};
axios axios
.post(`/api/campaign/create`, campaignData, { .get(`/api/campaign/getAll`, {
headers: { headers: {
Authorization: `Bearer ${token}`,
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
}, },
}) })
.then((res) => { .then((res) => {
// console.log(res); setCampaignData(res.data?.campaigns);
swal({
title: "Added",
text: res?.data?.message
? res?.data?.message
: "Campaign added successfully!",
icon: "success",
button: "Close",
});
setLoading(false); setLoading(false);
handleView(5);
}) })
.catch((err) => { .catch((err) => {
console.log(err);
setLoading(false); setLoading(false);
const message = err.response?.data?.message || "Something went wrong!";
// console.log(message);
swal({
title: "Warning",
text: message,
icon: "error",
button: "Close",
dangerMode: true,
});
}); });
}; };
// console.log(data); useEffect(() => {
getCampaign();
}, []);
return ( return (
<React.Fragment> <React.Fragment>
<div className="col-12"> <div className="col-12">
@ -115,16 +88,16 @@ const Preview = ({ props }) => {
<tbody> <tbody>
<tr> <tr>
<th scope="col">Campaign Name</th> <th scope="col">Campaign Name</th>
<td>{data?.campaignName}</td> <td>{campaignData.campaignName}</td>
</tr> </tr>
<tr> <tr>
<th scope="col">Language</th> <th scope="col">Language</th>
<td>{data?.language}</td> <td>{campaignData.language}</td>
</tr> </tr>
<tr> <tr>
<th scope="col">Campaign Type</th> <th scope="col">Campaign Type</th>
<td>{data?.campaignType}</td> <td>{campaignData.campaignType}</td>
</tr> </tr>
<tr> <tr>
<th scope="col">Video</th> <th scope="col">Video</th>
@ -140,7 +113,7 @@ const Preview = ({ props }) => {
</tr> </tr>
<tr> <tr>
<th scope="col">Recipients</th> <th scope="col">Recipients</th>
<td>{data?.recipients?.length}</td> <td>{campaignData.recipients}</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -23,7 +23,6 @@ const Video = ({ props }) => {
const deleteRecord = (index) => { const deleteRecord = (index) => {
if (index >= 2) { if (index >= 2) {
// Only allow deletion for videos starting from the third one
setData((prev) => ({ setData((prev) => ({
...prev, ...prev,
videos: prev.videos.filter((_, i) => i !== index), videos: prev.videos.filter((_, i) => i !== index),
@ -31,7 +30,6 @@ const Video = ({ props }) => {
} }
}; };
console.log(data);
return ( return (
<div className="container"> <div className="container">
<div className="row"> <div className="row">
@ -106,7 +104,7 @@ const Video = ({ props }) => {
id={`videoTitle${index + 1}`} id={`videoTitle${index + 1}`}
onChange={(e) => handleVideoUpload(e, index)} onChange={(e) => handleVideoUpload(e, index)}
/> />
{index >= 2 && ( // Render delete button for videos starting from the third one {index >= 2 && (
<div className="col-12"> <div className="col-12">
<button <button
onClick={() => { onClick={() => {