From 2238dc074d5ba814b3df985188800cad05bbc7c8 Mon Sep 17 00:00:00 2001 From: Pratish Ninawe Date: Tue, 3 Oct 2023 10:13:34 +0530 Subject: [PATCH] worked on preview files --- src/views/Campaigns/ContactDetails.js | 69 +++++++++++++++++++----- src/views/Campaigns/Preview.js | 75 +++++++++------------------ src/views/Campaigns/Video.js | 4 +- 3 files changed, 81 insertions(+), 67 deletions(-) diff --git a/src/views/Campaigns/ContactDetails.js b/src/views/Campaigns/ContactDetails.js index 99c899c..3844e12 100644 --- a/src/views/Campaigns/ContactDetails.js +++ b/src/views/Campaigns/ContactDetails.js @@ -3,8 +3,11 @@ 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"; +import axios from "axios"; +import { isAutheticated } from "src/auth"; const ContactDetails = ({ props }) => { + const token = isAutheticated(); const { data, setData, handleView } = props; const [dataEntryMethod, setDataEntryMethod] = useState("manual"); const [csvData, setCsvData] = useState([]); @@ -12,10 +15,7 @@ const ContactDetails = ({ props }) => { const addRecord = () => { setData((prevData) => ({ ...prevData, - recipients: [ - ...prevData.recipients, - { name: "", contact: "" }, // Initialize contact as an empty string - ], + recipients: [...prevData.recipients, { name: "", contact: "" }], })); }; @@ -83,16 +83,59 @@ const ContactDetails = ({ props }) => { })); }; - const handleSubmit = () => { - if ( - data?.recipients.every( - (recipient) => recipient.name !== "" && recipient.contact !== "" - ) - ) { - handleView(4); - } else { - toast.error("Fill all contact details"); + const handleSubmit = (e) => { + e.preventDefault(); + + const hasEmptyRecipients = data.recipients.some((recipient) => { + return !recipient.name || !recipient.contact; + }); + + if (hasEmptyRecipients) { + 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 ( diff --git a/src/views/Campaigns/Preview.js b/src/views/Campaigns/Preview.js index b29c5c2..bde6986 100644 --- a/src/views/Campaigns/Preview.js +++ b/src/views/Campaigns/Preview.js @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import Button from "@material-ui/core/Button"; import axios from "axios"; import { isAutheticated } from "src/auth"; @@ -7,71 +7,44 @@ const Preview = ({ props }) => { const token = isAutheticated(); const { data, handleView, setData } = props; const [loading, setLoading] = useState(false); + const [campaignData, setCampaignData] = useState([]); const handleSubmit = async (e) => { e.preventDefault(); - const hasEmptyRecipients = data.recipients.some((recipient) => { - return !recipient.name || !recipient.contact; - }); - - if (hasEmptyRecipients) { - swal({ - title: "Validation Error", - text: "Please fill Conatct details", - icon: "error", - button: "Close", - }); - return; + if ( + data?.recipients.every( + (recipient) => recipient.name !== "" && recipient.contact !== "" + ) + ) { + handleView(4); + } else { + toast.error("Fill all contact details"); } + }; - 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, - }; - + const getCampaign = () => { axios - .post(`/api/campaign/create`, campaignData, { + .get(`/api/campaign/getAll`, { headers: { - Authorization: `Bearer ${token}`, "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, }, }) .then((res) => { - // console.log(res); - swal({ - title: "Added", - text: res?.data?.message - ? res?.data?.message - : "Campaign added successfully!", - icon: "success", - button: "Close", - }); + setCampaignData(res.data?.campaigns); setLoading(false); - handleView(5); }) .catch((err) => { + console.log(err); 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 (
@@ -115,16 +88,16 @@ const Preview = ({ props }) => { Campaign Name - {data?.campaignName} + {campaignData.campaignName} Language - {data?.language} + {campaignData.language} Campaign Type - {data?.campaignType} + {campaignData.campaignType} Video @@ -140,7 +113,7 @@ const Preview = ({ props }) => { Recipients - {data?.recipients?.length} + {campaignData.recipients} diff --git a/src/views/Campaigns/Video.js b/src/views/Campaigns/Video.js index 6edb1e6..3a54c59 100644 --- a/src/views/Campaigns/Video.js +++ b/src/views/Campaigns/Video.js @@ -23,7 +23,6 @@ const Video = ({ props }) => { const deleteRecord = (index) => { if (index >= 2) { - // Only allow deletion for videos starting from the third one setData((prev) => ({ ...prev, videos: prev.videos.filter((_, i) => i !== index), @@ -31,7 +30,6 @@ const Video = ({ props }) => { } }; - console.log(data); return (
@@ -106,7 +104,7 @@ const Video = ({ props }) => { id={`videoTitle${index + 1}`} onChange={(e) => handleVideoUpload(e, index)} /> - {index >= 2 && ( // Render delete button for videos starting from the third one + {index >= 2 && (