This commit is contained in:
omkieit 2024-09-28 14:21:29 +05:30
parent d54bcdb9c5
commit 8b49882f18
6 changed files with 86 additions and 62 deletions

View File

@ -113,13 +113,40 @@ export const updatePropertyById = async (req, res) => {
// }; // };
// controllers/propertyController.js // export const getProperties = async (req, res) => {
// try {
// const { page = 1, limit = 10 } = req.query;
// const skip = (page - 1) * limit;
// const totalProperties = await PropertyModal.countDocuments();
// const properties = await PropertyModal.find()
// .sort({ createdAt: -1 })
// .skip(skip)
// .limit(parseInt(limit));
// res.status(200).json({
// success: true,
// data: properties,
// totalPages: Math.ceil(totalProperties / limit),
// currentPage: parseInt(page), // Include the currentPage in the response
// });
// } catch (err) {
// res.status(500).json({ success: false, message: 'Server error' });
// }
// };
export const getProperties = async (req, res) => { export const getProperties = async (req, res) => {
try { try {
const { page = 1, limit = 10 } = req.query; const { page = 1, limit = 10, keyword = "" } = req.query;
const skip = (page - 1) * limit; const skip = (page - 1) * limit;
const totalProperties = await PropertyModal.countDocuments();
const properties = await PropertyModal.find() // Use a filter to match the keyword if provided
const keywordFilter = keyword
? { address: { $regex: keyword, $options: "i" } }
: {};
const totalProperties = await PropertyModal.countDocuments(keywordFilter);
const properties = await PropertyModal.find(keywordFilter)
.sort({ createdAt: -1 }) .sort({ createdAt: -1 })
.skip(skip) .skip(skip)
.limit(parseInt(limit)); .limit(parseInt(limit));
@ -128,7 +155,7 @@ export const getProperties = async (req, res) => {
success: true, success: true,
data: properties, data: properties,
totalPages: Math.ceil(totalProperties / limit), totalPages: Math.ceil(totalProperties / limit),
currentPage: parseInt(page), // Include the currentPage in the response currentPage: parseInt(page),
}); });
} catch (err) { } catch (err) {
res.status(500).json({ success: false, message: 'Server error' }); res.status(500).json({ success: false, message: 'Server error' });
@ -137,3 +164,4 @@ export const getProperties = async (req, res) => {

File diff suppressed because one or more lines are too long

View File

@ -45,7 +45,7 @@
<script type="module" crossorigin src="/assets/index-DJmr6ji6.js"></script> <script type="module" crossorigin src="/assets/index-BsVOnNCb.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-DepkKhoc.css"> <link rel="stylesheet" crossorigin href="/assets/index-DepkKhoc.css">
</head> </head>

View File

@ -11,7 +11,6 @@ const EditProperty = () => {
const [formData, setFormData] = useState({ const [formData, setFormData] = useState({
propertyType: "", propertyType: "",
title: "",
yearBuild: "", yearBuild: "",
totalSqft: "", totalSqft: "",
}); });
@ -24,7 +23,6 @@ const EditProperty = () => {
if (selectedProperty) { if (selectedProperty) {
setFormData({ setFormData({
propertyType: selectedProperty.propertyType, propertyType: selectedProperty.propertyType,
title: selectedProperty.title,
yearBuild: selectedProperty.yearBuild, yearBuild: selectedProperty.yearBuild,
totalSqft: selectedProperty.totalSqft, totalSqft: selectedProperty.totalSqft,
}); });
@ -62,23 +60,6 @@ const EditProperty = () => {
</select> </select>
</div> </div>
<div className="col-12">
<div className="form-floating mb-3">
<input
type="text"
className="form-control"
name="title"
value={formData.title}
onChange={handleChange}
placeholder="Property title"
required
/>
<label htmlFor="title" className="form-label">
Property Title
</label>
</div>
</div>
<div className="col-12"> <div className="col-12">
<div className="form-floating mb-3"> <div className="form-floating mb-3">
<input <input

View File

@ -20,9 +20,15 @@ const SearchProperties = () => {
const limit = 10; // Number of results per page const limit = 10; // Number of results per page
// Dispatch getProperties to load properties on page change // Dispatch getProperties to load properties on page change
// useEffect(() => {
// dispatch(getProperties({ page, limit }));
// }, [dispatch, page]);
// Dispatch getProperties to load properties on page or keyword change
useEffect(() => { useEffect(() => {
dispatch(getProperties({ page, limit })); dispatch(getProperties({ page, limit, keyword }));
}, [dispatch, page]); }, [dispatch, page, keyword]);
// Filter properties based on the keyword entered // Filter properties based on the keyword entered
useEffect(() => { useEffect(() => {
@ -45,10 +51,16 @@ const SearchProperties = () => {
e.preventDefault(); e.preventDefault();
}; };
// const handlePageChange = (newPage) => {
// setPage(newPage); // Update the current page when user clicks pagination
// };
const handlePageChange = (newPage) => { const handlePageChange = (newPage) => {
setPage(newPage); // Update the current page when user clicks pagination setPage(newPage);
dispatch(getProperties({ page: newPage, limit, keyword })); // Pass the keyword when changing page
}; };
return ( return (
<> <>
<Navbar /> <Navbar />
@ -109,7 +121,6 @@ const SearchProperties = () => {
filteredProperties.map((property) => ( filteredProperties.map((property) => (
<tr key={property.id}> <tr key={property.id}>
<td> <td>
{property.images && property.images[0] ? ( {property.images && property.images[0] ? (
<img <img
src={property.images[0].file || profilepic} src={property.images[0].file || profilepic}
@ -123,11 +134,6 @@ const SearchProperties = () => {
style={{ width: "100px", height: "auto" }} style={{ width: "100px", height: "auto" }}
/> />
)} )}
</td> </td>
<td> <td>
<div className="widget-26-job-title"> <div className="widget-26-job-title">

View File

@ -69,6 +69,14 @@ export const updateProperty = createAsyncThunk(
// return response.data; // return response.data;
// }); // });
// export const getProperties = createAsyncThunk(
// 'properties/getProperties',
// async ({ page, limit, keyword = "" }) => {
// const response = await axios.get(`${import.meta.env.VITE_REACT_APP_SECRET}/properties?page=${page}&limit=${limit}&keyword=${keyword}`);
// return response.data;
// }
// );
export const getProperties = createAsyncThunk( export const getProperties = createAsyncThunk(
'properties/getProperties', 'properties/getProperties',
async ({ page, limit, keyword = "" }) => { async ({ page, limit, keyword = "" }) => {
@ -77,6 +85,7 @@ export const getProperties = createAsyncThunk(
} }
); );
const propertySlice = createSlice({ const propertySlice = createSlice({
name: "property", name: "property",
initialState: { initialState: {