done
This commit is contained in:
parent
6f8ca5ce4a
commit
dd8f1e6463
|
@ -53,4 +53,29 @@ export const getPropertyById = async (req, res) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Update property by ID..
|
||||||
|
export const updatePropertyById = async (req, res) => {
|
||||||
|
const { id } = req.params; // This should be the propertyId
|
||||||
|
const updateData = req.body;
|
||||||
|
|
||||||
|
console.log("Received propertyId:", id); // Log the received propertyId
|
||||||
|
console.log("updateData", updateData);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Find the property by propertyId instead of _id
|
||||||
|
const updatedProperty = await PropertyModal.findOneAndUpdate(
|
||||||
|
{ propertyId: id }, // Search by propertyId
|
||||||
|
updateData,
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!updatedProperty) {
|
||||||
|
return res.status(404).json({ message: "Property not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json(updatedProperty);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating property:", error.message);
|
||||||
|
res.status(500).json({ message: "Failed to update property", error: error.message });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
const propertySchema = mongoose.Schema({
|
const propertySchema = mongoose.Schema({
|
||||||
|
_id: { type: String, required: true }, // Allow string IDs
|
||||||
propertyType: {type: String, required: true },
|
propertyType: {type: String, required: true },
|
||||||
title: {type: String, required: true },
|
title: {type: String, required: true },
|
||||||
yearBuild: {type: String, required: true },
|
yearBuild: {type: String, required: true },
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
import auth from '../middleware/auth.js';
|
import auth from '../middleware/auth.js';
|
||||||
import { createProperty, getUserProperties, getPropertyById} from '../controllers/property.js';
|
import { createProperty, getUserProperties, getPropertyById, updatePropertyById} from '../controllers/property.js';
|
||||||
|
|
||||||
router.post('/', auth, createProperty);
|
router.post('/', auth, createProperty);
|
||||||
router.get('/user/:userId', getUserProperties);
|
router.get('/user/:userId', getUserProperties);
|
||||||
router.get('/:propertyId', getPropertyById);
|
router.get('/:propertyId', getPropertyById);
|
||||||
|
router.put("/:id", updatePropertyById);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -45,7 +45,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="module" crossorigin src="/assets/index-TX5YeaKV.js"></script>
|
<script type="module" crossorigin src="/assets/index-Bn1lHNVo.js"></script>
|
||||||
<link rel="stylesheet" crossorigin href="/assets/index-BtU0nZSp.css">
|
<link rel="stylesheet" crossorigin href="/assets/index-BtU0nZSp.css">
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useDispatch, useSelector } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { fetchPropertyById } from "../redux/features/propertySlice";
|
import { fetchPropertyById } from "../redux/features/propertySlice";
|
||||||
|
import { updateProperty } from "../redux/features/propertySlice";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
|
||||||
const EditProperty = () => {
|
const EditProperty = () => {
|
||||||
|
@ -34,10 +35,16 @@ const EditProperty = () => {
|
||||||
setFormData({ ...formData, [e.target.name]: e.target.value });
|
setFormData({ ...formData, [e.target.name]: e.target.value });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleSubmit = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
dispatch(updateProperty({ id, propertyData: formData }));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="edit-property-form">
|
<div className="edit-property-form">
|
||||||
<h2>Edit Property</h2>
|
<h2>Edit Property</h2>
|
||||||
<form>
|
<form onSubmit={handleSubmit}>
|
||||||
<div>
|
<div>
|
||||||
<select
|
<select
|
||||||
className="form-floating mb-3 form-control"
|
className="form-floating mb-3 form-control"
|
||||||
|
|
|
@ -22,6 +22,8 @@ export const verifyEmail = (id, token, data) => API.get(`/users/${id}/verify/${t
|
||||||
export const submitProperty = (propertyData) => API.post("/properties", propertyData);
|
export const submitProperty = (propertyData) => API.post("/properties", propertyData);
|
||||||
export const fetchUserProperties = (userId) => API.get(`/properties/user/${userId}`, userId);
|
export const fetchUserProperties = (userId) => API.get(`/properties/user/${userId}`, userId);
|
||||||
export const fetchPropertyById = (id) => API.get(`/properties/${id}`, id);
|
export const fetchPropertyById = (id) => API.get(`/properties/${id}`, id);
|
||||||
|
export const updateProperty = (id, propertyData) => API.put(`/properties/${id}`, propertyData);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
|
||||||
import * as api from "../api";
|
import * as api from "../api";
|
||||||
import axios from "axios";
|
|
||||||
|
|
||||||
export const submitProperty = createAsyncThunk(
|
export const submitProperty = createAsyncThunk(
|
||||||
'property/submitProperty',
|
"property/submitProperty",
|
||||||
async (propertyData, { rejectWithValue }) => {
|
async (propertyData, { rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const response = await api.submitProperty(propertyData);
|
const response = await api.submitProperty(propertyData);
|
||||||
|
@ -14,9 +13,8 @@ export const submitProperty = createAsyncThunk(
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
export const fetchUserProperties = createAsyncThunk(
|
export const fetchUserProperties = createAsyncThunk(
|
||||||
'property/fetchUserProperties',
|
"property/fetchUserProperties",
|
||||||
async (userId, { rejectWithValue }) => {
|
async (userId, { rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const response = await api.fetchUserProperties(userId);
|
const response = await api.fetchUserProperties(userId);
|
||||||
|
@ -39,12 +37,11 @@ export const fetchPropertyById = createAsyncThunk(
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update property thunk
|
|
||||||
export const updateProperty = createAsyncThunk(
|
export const updateProperty = createAsyncThunk(
|
||||||
'property/updateProperty',
|
"property/updateProperty",
|
||||||
async ({ id, updatedData }, { rejectWithValue }) => {
|
async ({ id, propertyData }, { rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.patch(`http://localhost:3002/properties/${id}`, updatedData);
|
const response = await api.updateProperty(id, propertyData);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return rejectWithValue(error.response.data);
|
return rejectWithValue(error.response.data);
|
||||||
|
@ -53,46 +50,41 @@ export const updateProperty = createAsyncThunk(
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const propertySlice = createSlice({
|
const propertySlice = createSlice({
|
||||||
name: 'property',
|
name: "property",
|
||||||
initialState: {
|
initialState: {
|
||||||
property: {},
|
property: {},
|
||||||
status: 'idle',
|
status: "idle",
|
||||||
error: null,
|
error: null,
|
||||||
userProperties: [],
|
userProperties: [],
|
||||||
selectedProperty: null,
|
selectedProperty: null,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
reducers: {},
|
reducers: {},
|
||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
builder
|
builder
|
||||||
.addCase(submitProperty.pending, (state) => {
|
.addCase(submitProperty.pending, (state) => {
|
||||||
state.status = 'loading';
|
state.status = "loading";
|
||||||
})
|
})
|
||||||
.addCase(submitProperty.fulfilled, (state, action) => {
|
.addCase(submitProperty.fulfilled, (state, action) => {
|
||||||
state.status = 'succeeded';
|
state.status = "succeeded";
|
||||||
state.property = action.payload;
|
state.property = action.payload;
|
||||||
})
|
})
|
||||||
.addCase(submitProperty.rejected, (state, action) => {
|
.addCase(submitProperty.rejected, (state, action) => {
|
||||||
state.status = 'failed';
|
state.status = "failed";
|
||||||
state.error = action.payload;
|
state.error = action.payload;
|
||||||
})
|
})
|
||||||
|
|
||||||
.addCase(fetchUserProperties.pending, (state) => {
|
.addCase(fetchUserProperties.pending, (state) => {
|
||||||
state.status = 'loading';
|
state.status = "loading";
|
||||||
})
|
})
|
||||||
.addCase(fetchUserProperties.fulfilled, (state, action) => {
|
.addCase(fetchUserProperties.fulfilled, (state, action) => {
|
||||||
state.status = 'succeeded';
|
state.status = "succeeded";
|
||||||
state.userProperties = action.payload;
|
state.userProperties = action.payload;
|
||||||
})
|
})
|
||||||
.addCase(fetchUserProperties.rejected, (state, action) => {
|
.addCase(fetchUserProperties.rejected, (state, action) => {
|
||||||
state.status = 'failed';
|
state.status = "failed";
|
||||||
state.error = action.payload;
|
state.error = action.payload;
|
||||||
})
|
})
|
||||||
|
|
||||||
.addCase(fetchPropertyById.pending, (state) => {
|
.addCase(fetchPropertyById.pending, (state) => {
|
||||||
state.status = "loading";
|
state.status = "loading";
|
||||||
|
@ -105,15 +97,18 @@ const propertySlice = createSlice({
|
||||||
state.status = "failed";
|
state.status = "failed";
|
||||||
state.error = action.payload;
|
state.error = action.payload;
|
||||||
})
|
})
|
||||||
|
.addCase(updateProperty.pending, (state) => {
|
||||||
|
state.status = "loading";
|
||||||
;
|
})
|
||||||
|
.addCase(updateProperty.fulfilled, (state, action) => {
|
||||||
|
state.status = "succeeded";
|
||||||
|
state.selectedProperty = action.payload;
|
||||||
|
})
|
||||||
|
.addCase(updateProperty.rejected, (state, action) => {
|
||||||
|
state.status = "failed";
|
||||||
|
state.error = action.payload;
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
export default propertySlice.reducer;
|
export default propertySlice.reducer;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue