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";
|
||||
|
||||
const propertySchema = mongoose.Schema({
|
||||
_id: { type: String, required: true }, // Allow string IDs
|
||||
propertyType: {type: String, required: true },
|
||||
title: {type: String, required: true },
|
||||
yearBuild: {type: String, required: true },
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import express from 'express';
|
||||
const router = express.Router();
|
||||
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.get('/user/:userId', getUserProperties);
|
||||
router.get('/:propertyId', getPropertyById);
|
||||
router.put("/:id", updatePropertyById);
|
||||
|
||||
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">
|
||||
</head>
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { useState, useEffect } from "react";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { fetchPropertyById } from "../redux/features/propertySlice";
|
||||
import { updateProperty } from "../redux/features/propertySlice";
|
||||
import { useParams } from "react-router-dom";
|
||||
|
||||
const EditProperty = () => {
|
||||
|
@ -34,10 +35,16 @@ const EditProperty = () => {
|
|||
setFormData({ ...formData, [e.target.name]: e.target.value });
|
||||
};
|
||||
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
dispatch(updateProperty({ id, propertyData: formData }));
|
||||
};
|
||||
|
||||
|
||||
return (
|
||||
<div className="edit-property-form">
|
||||
<h2>Edit Property</h2>
|
||||
<form>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div>
|
||||
<select
|
||||
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 fetchUserProperties = (userId) => API.get(`/properties/user/${userId}`, userId);
|
||||
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 axios from "axios";
|
||||
|
||||
export const submitProperty = createAsyncThunk(
|
||||
'property/submitProperty',
|
||||
"property/submitProperty",
|
||||
async (propertyData, { rejectWithValue }) => {
|
||||
try {
|
||||
const response = await api.submitProperty(propertyData);
|
||||
|
@ -14,9 +13,8 @@ export const submitProperty = createAsyncThunk(
|
|||
}
|
||||
);
|
||||
|
||||
|
||||
export const fetchUserProperties = createAsyncThunk(
|
||||
'property/fetchUserProperties',
|
||||
"property/fetchUserProperties",
|
||||
async (userId, { rejectWithValue }) => {
|
||||
try {
|
||||
const response = await api.fetchUserProperties(userId);
|
||||
|
@ -39,12 +37,11 @@ export const fetchPropertyById = createAsyncThunk(
|
|||
}
|
||||
);
|
||||
|
||||
// Update property thunk
|
||||
export const updateProperty = createAsyncThunk(
|
||||
'property/updateProperty',
|
||||
async ({ id, updatedData }, { rejectWithValue }) => {
|
||||
"property/updateProperty",
|
||||
async ({ id, propertyData }, { rejectWithValue }) => {
|
||||
try {
|
||||
const response = await axios.patch(`http://localhost:3002/properties/${id}`, updatedData);
|
||||
const response = await api.updateProperty(id, propertyData);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return rejectWithValue(error.response.data);
|
||||
|
@ -53,46 +50,41 @@ export const updateProperty = createAsyncThunk(
|
|||
);
|
||||
|
||||
|
||||
|
||||
const propertySlice = createSlice({
|
||||
name: 'property',
|
||||
name: "property",
|
||||
initialState: {
|
||||
property: {},
|
||||
status: 'idle',
|
||||
status: "idle",
|
||||
error: null,
|
||||
userProperties: [],
|
||||
selectedProperty: null,
|
||||
|
||||
|
||||
|
||||
|
||||
},
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
builder
|
||||
.addCase(submitProperty.pending, (state) => {
|
||||
state.status = 'loading';
|
||||
state.status = "loading";
|
||||
})
|
||||
.addCase(submitProperty.fulfilled, (state, action) => {
|
||||
state.status = 'succeeded';
|
||||
state.status = "succeeded";
|
||||
state.property = action.payload;
|
||||
})
|
||||
.addCase(submitProperty.rejected, (state, action) => {
|
||||
state.status = 'failed';
|
||||
state.status = "failed";
|
||||
state.error = action.payload;
|
||||
})
|
||||
|
||||
.addCase(fetchUserProperties.pending, (state) => {
|
||||
state.status = 'loading';
|
||||
state.status = "loading";
|
||||
})
|
||||
.addCase(fetchUserProperties.fulfilled, (state, action) => {
|
||||
state.status = 'succeeded';
|
||||
state.status = "succeeded";
|
||||
state.userProperties = action.payload;
|
||||
})
|
||||
.addCase(fetchUserProperties.rejected, (state, action) => {
|
||||
state.status = 'failed';
|
||||
state.status = "failed";
|
||||
state.error = action.payload;
|
||||
})
|
||||
})
|
||||
|
||||
.addCase(fetchPropertyById.pending, (state) => {
|
||||
state.status = "loading";
|
||||
|
@ -105,15 +97,18 @@ const propertySlice = createSlice({
|
|||
state.status = "failed";
|
||||
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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue