Skip to content

Commit

Permalink
Merge pull request #62 from jeevan-aj/update
Browse files Browse the repository at this point in the history
sort filter done for vehicleVaiant page 🚀 ,
  • Loading branch information
jeevan-aj authored May 9, 2024
2 parents b19593b + e42377d commit e44ef4e
Show file tree
Hide file tree
Showing 17 changed files with 872 additions and 1,129 deletions.
2 changes: 2 additions & 0 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import CheckoutPage from "./pages/user/CheckoutPage";
import Razorpay from "./pages/user/Razorpay";
import AllVehiclesofSameModel from "./pages/user/AllVehiclesofSameModel";
import AddProductModal from "./pages/admin/components/AddProductModal";
import VendorAddProductModal from "./pages/vendor/Components/VendorAddVehilceModal";


function App() {
Expand Down Expand Up @@ -76,6 +77,7 @@ function App() {
element={<VendorEditProductComponent />}
/>
<Route path="/vendorDashboard/vendorDeleteVehicleModal" element={<VendorDeleteVehicleModal/>} />
<Route path="vendorDashboard/vendorAddProduct" element={<VendorAddProductModal />} />
</Route>

{/* admin private routes */}
Expand Down
24 changes: 18 additions & 6 deletions client/src/components/Filter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import { GoPlus } from "react-icons/go";

import { Controller, useForm } from "react-hook-form";
import { useDispatch, useSelector } from "react-redux";
// import { setFilter, setfilterData } from "../redux/user/sortfilterSlice";
import { setFilteredData } from "../redux/user/sortfilterSlice";

const Filter = () => {
const { control, handleSubmit } = useForm();
const { userAllVehicles } = useSelector((state) => state.userListVehicles);
const { userAllVehicles, allVariants } = useSelector(
(state) => state.userListVehicles
);
const { variantMode } = useSelector((state) => state.sortfilterSlice);

const dispatch = useDispatch();
let transformedData = [];

const handleData = async (data) => {
console.log(data)
const typeMapping = {
suv: "car_type",
sedan: "car_type",
Expand All @@ -31,7 +32,7 @@ const Filter = () => {
.filter(([key, value]) => value == true)
.map(([key, value]) => ({ [key]: value, type: typeMapping[key] }));

if (transformedData && transformedData.length<=0 ) {
if (transformedData && transformedData.length <= 0 && !variantMode) {
dispatch(setFilteredData(userAllVehicles));
} else if (transformedData && transformedData.length > 0) {
try {
Expand All @@ -46,6 +47,19 @@ const Filter = () => {
if (res.ok) {
const data = await res.json();
const filtData = data.data.filteredVehicles;

//from filtData filtering vehicles that are available allVariants currently
//this is done when we have allVariants which means we are searching for available vehicles in Homepage and is redirected
if (allVariants) {
const filteredData = filtData.filter((data) =>
allVariants.some((variant) => variant._id === data._id)
);
dispatch(setFilteredData(filteredData));
return;
}

//this is in the other case when we are filtering from AllVehicles
//when we use filter from Vehicles in Navbar
dispatch(setFilteredData(filtData));
}
} catch (error) {
Expand Down Expand Up @@ -108,9 +122,7 @@ const Filter = () => {
{...field}
checked={field["value"] ?? false}
/>

)}

/>
}
label="Suv"
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/Sort.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useEffect } from "react";
import { setData, setFilteredData, setPriceHightoLow, setPriceLowtoHigh, setYearAscending, setYearDecending } from "../redux/user/sortfilterSlice";

const Sort = () => {
const {userAllVehicles} = useSelector(state => state.userListVehicles)
const {userAllVehicles,allVariants} = useSelector(state => state.userListVehicles)
const dispatch = useDispatch();
const { handleSubmit, control } = useForm();

Expand All @@ -31,7 +31,9 @@ const Sort = () => {
};

useEffect(()=> {
dispatch(setFilteredData(userAllVehicles))
if(!allVariants){
dispatch(setFilteredData(userAllVehicles))
}
},[])

return (
Expand Down
25 changes: 13 additions & 12 deletions client/src/pages/admin/components/AddProductModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { IoMdClose } from "react-icons/io";
import { LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import { setLoading, setadminAddVehicleSuccess, setadminCrudError } from "../../../redux/adminSlices/adminDashboardSlice/StatusSlice";

export const fetchModelData = async (dispatch) => {
try {
Expand Down Expand Up @@ -74,21 +75,22 @@ export const fetchModelData = async (dispatch) => {
};

const AddProductModal = () => {
const { register, handleSubmit, control } = useForm();
const { register, handleSubmit, control , reset } = useForm();
const navigate = useNavigate();
const dispatch = useDispatch();
const { isAddVehicleClicked } = useSelector((state) => state.addVehicle);
const { modelData, companyData, locationData, districtData } = useSelector(
(state) => state.modelDataSlice
);
const {loading} = useSelector(state => state.statusSlice)

useEffect(() => {
fetchModelData(dispatch);
dispatch(addVehicleClicked(true))
}, []);

const onSubmit = async (addData) => {
console.log(addData.Registeration_end_date.$d)

try {
const img = [];
for (let i = 0; i < addData.image.length; i++) {
Expand Down Expand Up @@ -117,16 +119,12 @@ const AddProductModal = () => {
formData.append("location", addData.vehicleLocation);
formData.append("district", addData.vehicleDistrict
);





console.log(formData)


let tostID;
if (formData) {
tostID = toast.loading("saving...", { position: "bottom-center" });
dispatch(setLoading(true))
}
const res = await fetch("/api/admin/addProduct", {
method: "POST",
Expand All @@ -136,14 +134,17 @@ const AddProductModal = () => {
if (!res.ok) {
toast.error("error");
toast.dismiss(tostID);
dispatch(setLoading(false))
}
if (res.ok) {
toast.success("added");
toast.dismiss(tostID);
dispatch(setadminAddVehicleSuccess(true));
toast.dismiss(tostID)
dispatch(setLoading(false))
}

// reset();
reset();
} catch (error) {
dispatch(setadminCrudError(true))
console.log(error);
}
dispatch(addVehicleClicked(false));
Expand All @@ -156,7 +157,7 @@ const AddProductModal = () => {

return (
<>
<Toaster />
{loading ? <Toaster/> : null }
{isAddVehicleClicked && (
<div>
<button onClick={handleClose} className="relative left-10 top-5">
Expand Down
Loading

0 comments on commit e44ef4e

Please sign in to comment.