Testimonials Section Updated

This commit is contained in:
Sibunnayak 2024-03-27 10:44:34 +05:30
parent ee638fb666
commit a88dae7cb2
4 changed files with 730 additions and 264 deletions

View File

@ -114,6 +114,7 @@ import SupportRequest from "./views/CustomerSupport/SupportRequest";
import SupportReply from "./views/CustomerSupport/SupportReply";
import SupportRequestClosed from "./views/CustomerSupport/SupportRequestClosed";
import CloseRequestView from "./views/CustomerSupport/CloseRequestView";
import EditTestimonial from "./views/Testimonials/EditTestimonial";
const routes = [
{ path: "/", exact: true, name: "Home" },
@ -294,8 +295,27 @@ const routes = [
// { path: '/complaint/view/:id', name: 'view Complain', element: ViewComplaint },
//Complaints
{ path: "/testimonials", name: "Testimonials", element: Testimonials },
{ path: "/testimonial/new", name: "AddTestimonial", element: AddTestimonial },
{
path: "/testimonials",
name: "Testimonials",
element: Testimonials
},
{
path: "/testimonial/new",
name: "AddTestimonial",
element: AddTestimonial
},
{
path: "/testimonial/view/:id",
name: "ViewTestimonial",
element: ViewTestimonial,
},
{
path: "/testimonial/edit/:id",
name: "EditTestimonial",
element: EditTestimonial
},
{
path: "/banner",
name: "Banners",
@ -316,11 +336,6 @@ const routes = [
name: "ShopImage",
element: ShopImage,
},
{
path: "/testimonial/view/:id",
name: "ViewTestimonial",
element: ViewTestimonial,
},
//informations
{ path: "/informations", name: "Informations", element: Informations },
{

View File

@ -1,129 +1,111 @@
import React, { useEffect, useState } from 'react'
import Button from '@material-ui/core/Button'
import { Link, useNavigate } from 'react-router-dom'
import swal from 'sweetalert'
import axios from 'axios'
import { isAutheticated } from 'src/auth'
import React, { useEffect, useState } from "react";
import Button from "@material-ui/core/Button";
import { Link, useNavigate } from "react-router-dom";
import swal from "sweetalert";
import axios from "axios";
import { isAutheticated } from "src/auth";
// import { WebsiteURL } from '../WebsiteURL'
const AddTestimonial = () => {
const token = isAutheticated()
const navigate = useNavigate()
const token = isAutheticated();
const navigate = useNavigate();
const [data, setData] = useState({
name: "",
testimonial: "",
company: "",
image: "",
imageURL: "",
});
name: '',
testimonial: '',
company: '',
image: '',
imageURL: '',
})
const [loading, setLoading] = useState(false)
const [loading, setLoading] = useState(false);
const handleChange = (e) => {
if (e.target.id === 'image') {
if (e.target.id === "image") {
if (
e.target.files[0]?.type === 'image/jpeg' ||
e.target.files[0]?.type === 'image/png' ||
e.target.files[0]?.type === 'image/jpg'
e.target.files[0]?.type === "image/jpeg" ||
e.target.files[0]?.type === "image/png" ||
e.target.files[0]?.type === "image/jpg"
) {
setData((prev) => ({
...prev,
imageURL: URL.createObjectURL(e.target.files[0]),
image: e.target.files[0],
}))
return
}));
return;
} else {
swal({
title: 'Warning',
text: 'Upload jpg, jpeg, png only.',
icon: 'error',
button: 'Close',
title: "Warning",
text: "Upload jpg, jpeg, png only.",
icon: "error",
button: "Close",
dangerMode: true,
})
});
setData((prev) => ({
...prev,
imageURL: '',
image: '',
}))
e.target.value = null
return
imageURL: "",
image: "",
}));
e.target.value = null;
return;
}
}
setData((prev) => ({ ...prev, [e.target.id]: e.target.value }))
}
setData((prev) => ({ ...prev, [e.target.id]: e.target.value }));
};
const handleSubmit = () => {
if (
data.name.trim() === '' ||
data.company.trim() === '' ||
data.image === '' ||
data.testimonial.trim() === ''
data.name.trim() === "" ||
data.company.trim() === "" ||
data.image === "" ||
data.testimonial.trim() === ""
) {
swal({
title: 'Warning',
text: 'Fill all mandatory fields',
icon: 'error',
button: 'Close',
title: "Warning",
text: "Fill all mandatory fields",
icon: "error",
button: "Close",
dangerMode: true,
})
return
});
return;
}
setLoading(true)
const formData = new FormData()
formData.set('name', data.name)
formData.set('company', data.company)
formData.set('image', data.image)
formData.set('testimonial', data.testimonial)
setLoading(true);
const formData = new FormData();
formData.set("name", data.name);
formData.set("company", data.company);
formData.set("image", data.image);
formData.set("testimonial", data.testimonial);
axios
.post(`/api/testimonial/new/`, formData, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'multipart/formdata',
'Access-Control-Allow-Origin': '*',
"Content-Type": "multipart/formdata",
"Access-Control-Allow-Origin": "*",
},
})
.then((res) => {
swal({
title: 'Added',
text: 'Testimonial added successfully!',
icon: 'success',
button: 'ok',
})
setLoading(false)
navigate('/testimonials', { replace: true })
title: "Added",
text: "Testimonial added successfully!",
icon: "success",
button: "ok",
});
setLoading(false);
navigate("/testimonials", { replace: true });
})
.catch((err) => {
setLoading(false)
const message = err.response?.data?.message || 'Something went wrong!'
setLoading(false);
const message = err.response?.data?.message || "Something went wrong!";
swal({
title: 'Warning',
title: "Warning",
text: message,
icon: 'error',
button: 'Retry',
icon: "error",
button: "Retry",
dangerMode: true,
})
})
}
});
});
};
return (
<div className="container">
@ -137,10 +119,10 @@ const AddTestimonial = () => {
justify-content-between
"
>
<div style={{ fontSize: '22px' }} className="fw-bold">
<div style={{ fontSize: "22px" }} className="fw-bold">
Testimonial
</div>
<div style={{ display: 'flex', gap: '1rem' }}>
<div style={{ display: "flex", gap: "1rem" }}>
<h4 className="mb-0"></h4>
</div>
@ -149,24 +131,24 @@ const AddTestimonial = () => {
variant="contained"
color="primary"
style={{
fontWeight: 'bold',
marginBottom: '1rem',
textTransform: 'capitalize',
marginRight: '5px',
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
marginRight: "5px",
}}
onClick={() => handleSubmit()}
disabled={loading}
>
{loading ? 'Loading' : 'Save'}
{loading ? "Loading" : "Save"}
</Button>
<Link to="/testimonials">
<Button
variant="contained"
color="secondary"
style={{
fontWeight: 'bold',
marginBottom: '1rem',
textTransform: 'capitalize',
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
}}
>
Back
@ -180,10 +162,6 @@ const AddTestimonial = () => {
<div className="col-lg-12 col-md-12 col-sm-12 my-1">
<div className="card h-100">
<div className="card-body px-5">
<div className="mb-3">
<label htmlFor="title" className="form-label">
Name *
@ -196,12 +174,16 @@ const AddTestimonial = () => {
maxLength={25}
onChange={(e) => handleChange(e)}
/>
{data.name ? <><small className="charLeft mt-4 fst-italic">
{data.name ? (
<>
<small className="charLeft mt-4 fst-italic">
{25 - data.name.length} characters left
</small></> : <></>
} </div>
</small>
</>
) : (
<></>
)}{" "}
</div>
<div className="mb-3">
<label htmlFor="title" className="form-label">
@ -215,11 +197,16 @@ const AddTestimonial = () => {
maxLength={30}
onChange={(e) => handleChange(e)}
/>
{data.company ? <><small className="charLeft mt-4 fst-italic">
{data.company ? (
<>
<small className="charLeft mt-4 fst-italic">
{30 - data.company.length} characters left
</small></> : <></>
} </div>
</small>
</>
) : (
<></>
)}{" "}
</div>
<div className="mb-3">
<label htmlFor="title" className="form-label">
Testimonial *
@ -231,20 +218,24 @@ const AddTestimonial = () => {
rows="10"
cols="80"
value={data.testimonial}
placeholder='your Testimonial...'
placeholder="your Testimonial..."
maxLength="500"
onChange={(e) => handleChange(e)}
>
</textarea>
></textarea>
{data.testimonial ? <><small className="charLeft mt-4 fst-italic">
{data.testimonial ? (
<>
<small className="charLeft mt-4 fst-italic">
{500 - data.testimonial.length} characters left
</small></> : <></>
}
</small>
</>
) : (
<></>
)}
</div>
<div className="mb-3">
<label htmlFor="image" className="form-label">
Photo (optional)*
Photo*
</label>
<input
type="file"
@ -254,15 +245,19 @@ const AddTestimonial = () => {
multiple
onChange={(e) => handleChange(e)}
/>
<p className="pt-1 pl-2 text-secondary">Upload jpg, jpeg and png only*</p>
<p className="pt-1 pl-2 text-secondary">
Upload jpg, jpeg and png only*
</p>
</div>
<div className="mb-3" style={{ height: '200px', maxWdth: '100%' }}>
<div
className="mb-3"
style={{ height: "200px", maxWdth: "100%" }}
>
<img
src={data.imageURL}
alt="Uploaded Image will be shown here"
style={{ maxHeight: '200px', maxWidth: '100%' }}
style={{ maxHeight: "200px", maxWidth: "100%" }}
/>
</div>
{/* <div className="mb-3">
<label htmlFor="title" className="form-label">
@ -276,10 +271,9 @@ const AddTestimonial = () => {
</div>
</div>
</div>
</div>
</div>
)
}
);
};
export default AddTestimonial
export default AddTestimonial;

View File

@ -0,0 +1,415 @@
import React, { useEffect, useState } from "react";
import Button from "@material-ui/core/Button";
import { Link, useNavigate, useParams } from "react-router-dom";
import swal from "sweetalert";
import axios from "axios";
import { isAutheticated } from "src/auth";
import CloudUploadIcon from "@mui/icons-material/CloudUpload";
import DeleteSharpIcon from "@mui/icons-material/DeleteSharp";
import {
Box,
FormControl,
IconButton,
MenuItem,
Select,
TextField,
} from "@mui/material";
// import { WebsiteURL } from '../WebsiteURL'
const EditTestimonial = () => {
const id = useParams()?.id;
const token = isAutheticated();
const navigate = useNavigate();
const [loading, setLoading] = useState(false);
const [name, setName] = useState("");
const [company, setcompany] = useState("");
const [image, setimage] = useState(null);
const [testimonial, settestimonial] = useState("");
const [error, setError] = useState("");
const [newUpdatedImages, setNewUpdatedImages] = useState(null);
const [Img, setImg] = useState(true);
//get testimonialdata
const gettestimonial = async () => {
axios
.get(`/api/testimonial/getOne/${id}`, {
headers: {
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
})
.then((res) => {
// console.log(res?.data?.testimonial);
setName(res?.data?.testimonial?.name);
setcompany(res?.data?.testimonial?.company);
settestimonial(res?.data?.testimonial?.testimonial);
setimage(res?.data?.testimonial?.image);
setImg(false);
})
.catch((err) => {
swal({
title: error,
text: " Can not fetch the product ",
icon: "error",
button: "Retry",
dangerMode: true,
});
});
};
useEffect(() => {
gettestimonial();
}, []);
const handleSubmit = () => {
if (name === "" || testimonial === "" || (image === null && newUpdatedImages === null)) {
swal({
title: "Warning",
text: "Fill all mandatory fields",
icon: "error",
button: "Close",
dangerMode: true,
});
return;
}
setLoading(true);
const formData = new FormData();
formData.append("name", name);
formData.append("testimonial", testimonial);
formData.append("company", company);
// formData.append("image", JSON.stringify(newUpdatedImages));
if (newUpdatedImages !== null) {
formData.append("image", newUpdatedImages);
}
axios
.patch(`/api/testimonial/update/${id}`, formData, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "multipart/form-data",
"Access-Control-Allow-Origin": "*",
},
})
.then((res) => {
swal({
title: "Updated",
text: "Testimonial Updated successfully!",
icon: "success",
button: "ok",
});
setLoading(false);
navigate("/testimonials", { replace: true });
})
.catch((err) => {
setLoading(false);
const message = err.response?.data?.message
? err.response?.data?.message
: "Something went wrong!";
swal({
title: "Warning",
text: message,
icon: "error",
button: "Retry",
dangerMode: true,
});
});
};
const handleFileChange = (e) => {
const files = e.target.files;
// Reset error state
setError("");
// Check if more than one image is selected
if (files.length > 1 || Img === false || newUpdatedImages !== null) {
setError("You can only upload one image.");
return;
}
// Check file types and append to selectedFiles
const allowedTypes = ["image/jpeg", "image/png", "image/jpg"];
const file = files[0]; // Only one file is selected, so we get the first one
if (allowedTypes.includes(file.type)) {
setNewUpdatedImages(file);
} else {
setError("Please upload only PNG, JPEG, or JPG files.");
}
};
const handelDelete = async (public_id) => {
const ary = public_id.split("/");
const res = await axios.delete(
`/api/testimonial/deleteImage/GetSygnal/Testimonial/${ary[2]}`,
{
headers: {
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
}
);
if (res) {
setimage(null);
setImg(true);
}
};
const handellocalDelete = () => {
setNewUpdatedImages(null);
};
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">
Edit Testimonial
</div>
<div style={{ display: "flex", gap: "1rem" }}>
<h4 className="mb-0"></h4>
</div>
<div className="page-title-right">
<Button
variant="contained"
color="primary"
style={{
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
marginRight: "5px",
}}
onClick={() => handleSubmit()}
disabled={loading}
>
{loading ? "Loading" : "Edit"}
</Button>
<Link to="/testimonials">
<Button
variant="contained"
color="secondary"
style={{
fontWeight: "bold",
marginBottom: "1rem",
textTransform: "capitalize",
}}
>
Back
</Button>
</Link>
</div>
</div>
</div>
</div>
<div className="row">
<div className="col-lg-6 col-md-6 col-sm-12 my-1">
<div className="card h-100">
<div className="card-body px-5">
<div className="mb-3">
<label htmlFor="title" className="form-label">
Name*
</label>
<input
type="text"
className="form-control"
id="name"
value={name}
maxLength={25}
onChange={(e) => setName(e.target.value)}
/>
{name ? (
<>
<small className="charLeft mt-4 fst-italic">
{25 - name.length} characters left
</small>
</>
) : (
<></>
)}{" "}
</div>
<div className="mb-3">
<label htmlFor="title" className="form-label">
Company
</label>
<input
type="text"
className="form-control"
id="Company"
value={company}
maxLength="100"
onChange={(e) => setcompany(e.target.value)}
/>
{company ? (
<>
<small className="charLeft mt-4 fst-italic">
{30 - company.length} characters left
</small>
</>
) : (
<></>
)}
</div>
{/* ************************* */}
<Box>
<label htmlFor="upload-Image">
<TextField
style={{
display: "none",
width: "350px",
height: "350px",
borderRadius: "10%",
}}
fullWidth
id="upload-Image"
type="file"
accept=".jpg , .png ,.jpeg"
label="file"
multiple
variant="outlined"
onChange={(e) => handleFileChange(e)}
/>
<Box
style={{ borderRadius: "10%" }}
sx={{
margin: "1rem 0rem",
cursor: "pointer",
width: "100px",
height: "100px",
border: "2px solid grey",
// borderRadius: '50%',
"&:hover": {
background: "rgba(112,112,112,0.5)",
},
}}
>
<CloudUploadIcon
style={{
color: "grey",
margin: "auto",
fontSize: "5rem",
marginLeft: "0.5rem",
marginTop: "0.5rem",
}}
fontSize="large"
/>
</Box>
</label>
</Box>
{error && <p style={{ color: "red" }}>{error}</p>}
<div>
<strong className="fs-6 fst-italic">
*You cannot upload more than 1 image
</strong>
</div>
<Box style={{ display: "flex" }}>
{Img === false ? (
<Box marginRight={"2rem"}>
<img
src={image.url}
alt="profileImage"
style={{
width: 70,
height: 70,
marginBottom: "1rem",
}}
/>
<DeleteSharpIcon
onClick={() => handelDelete(image.public_id)}
fontSize="small"
sx={{
color: "white",
position: "absolute",
cursor: "pointer",
padding: "0.2rem",
background: "black",
borderRadius: "50%",
}}
/>
</Box>
) : null}
{newUpdatedImages !== null && Img && (
<Box marginRight={"2rem"}>
<img
src={
newUpdatedImages
? URL.createObjectURL(newUpdatedImages)
: ""
}
// src={newUpdatedImages?.url}
alt="profileImage"
style={{
width: 70,
height: 70,
marginBottom: "1rem",
}}
/>
<DeleteSharpIcon
onClick={() => handellocalDelete()}
fontSize="small"
sx={{
color: "white",
position: "absolute",
cursor: "pointer",
padding: "0.2rem",
background: "black",
borderRadius: "50%",
}}
/>
{/* </IconButton> */}
</Box>
)}
</Box>
</div>
</div>
</div>
<div className="col-lg-6 col-md-6 col-sm-12 my-1">
<div className="card h-100">
<div className="card-body px-5">
<div className="mb-3 me-3">
<label htmlFor="title" className="form-label">
Testimonial*
</label>
<textarea
className="form-control"
id="testimonial"
value={testimonial}
maxLength={200}
style={{ minHeight: "120px", resize: "vertical" }} // Adjust the height and resize behavior here
onChange={(e) => settestimonial(e.target.value)}
/>
{testimonial ? (
<small className="charLeft mt-4 fst-italic">
{200 - testimonial.length} characters left
</small>
) : (
<></>
)}
</div>
</div>
</div>
</div>
</div>
</div>
);
};
export default EditTestimonial;

View File

@ -61,7 +61,7 @@ const Testimonials = () => {
}).then((value) => {
if (value === true) {
axios
.delete(`/api/item/delete/${id}`, {
.delete(`/api/testimonial/delete/${id}`, {
headers: {
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
@ -222,6 +222,48 @@ const Testimonials = () => {
View
</button>
</Link>
<Link to={`/testimonial/edit/${item._id}`}>
<button
style={{
color: "white",
marginRight: "1rem",
}}
type="button"
className="
btn btn-info btn-sm
waves-effect waves-light
btn-table
mt-1
mx-1
"
>
Edit
</button>
</Link>
<Link
to={"#"}
style={{
marginRight: "1rem",
}}
>
<button
style={{ color: "white" }}
type="button"
className="
btn btn-danger btn-sm
waves-effect waves-light
btn-table
mt-1
mx-1
"
onClick={() => {
handleDelete(item._id);
}}
>
Delete
</button>
</Link>
</td>
</tr>
);