This commit is contained in:
omkieit 2024-09-16 16:25:17 +05:30
parent 4448c7560f
commit 4b228bc5b5
9 changed files with 91 additions and 27 deletions

View File

@ -31,3 +31,12 @@ export const createProperty = async (req, res) => {
}; };
export const getUserProperties = async (req, res) => {
try {
const userId = req.params.userId;
const properties = await PropertyModal.find({ userId: userId });
res.status(200).json(properties);
} catch (error) {
res.status(500).json({ message: "Error retrieving properties", error });
}
};

View File

@ -33,7 +33,8 @@ app.use(
); );
app.use("/users", userRouter); app.use("/users", userRouter);
app.use("/property", propertyRouter); app.use("/properties", propertyRouter);
app.get("/", (req, res) => { app.get("/", (req, res) => {
res.send("Welcome to EF-API"); res.send("Welcome to EF-API");

View File

@ -1,9 +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 } from '../controllers/property.js'; import { createProperty, getUserProperties } from '../controllers/property.js';
router.post('/', auth, createProperty); router.post('/', auth, createProperty);
router.get('/user/:userId', getUserProperties);

File diff suppressed because one or more lines are too long

View File

@ -42,7 +42,7 @@
<!-- <script src="https://stackpath.bootstrapcdn.com/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script> --> <!-- <script src="https://stackpath.bootstrapcdn.com/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script> -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js"></script>
<script type="module" crossorigin src="/assets/index-0-EQa57V.js"></script> <script type="module" crossorigin src="/assets/index-CzVE7_Kd.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BrT2kTNU.css"> <link rel="stylesheet" crossorigin href="/assets/index-BrT2kTNU.css">
</head> </head>

View File

@ -46,7 +46,7 @@ const App = () => {
element={<ResetPassword />} element={<ResetPassword />}
/> />
<Route path="/addproperty" element={ <PrivateRoute><Addproperty /></PrivateRoute>}></Route> {/* <Route path="/addproperty" element={ <PrivateRoute><Addproperty /></PrivateRoute>}></Route> */}

View File

@ -1,21 +1,42 @@
import { useState } from "react"; import { useState,useEffect } from "react";
import Footer from "./Footer"; import Footer from "./Footer";
import Navbar from "./Navbar"; import Navbar from "./Navbar";
import { useSelector } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
import profilepic from "../img/samplepic.jpg"; import profilepic from "../img/samplepic.jpg";
import Addproperty from "./Addproperty"; import Addproperty from "./Addproperty";
import { fetchUserProperties } from "../redux/features/propertySlice";
import "../dashboard.css"; import "../dashboard.css";
const Dashboard = () => { const Dashboard = () => {
const dispatch = useDispatch();
const { user } = useSelector((state) => ({ ...state.auth })); const { user } = useSelector((state) => ({ ...state.auth }));
const { userProperties} = useSelector((state) => state.property);
const [activeTab, setActiveTab] = useState("dashboard"); const [activeTab, setActiveTab] = useState("dashboard");
// Fetch user properties when "Active Properties" tab is selected
useEffect(() => {
if (activeTab === "activeProperties") {
dispatch(fetchUserProperties(user?.result?.userId));
}
}, [activeTab, dispatch, user?.result?.userId]);
const renderTabContent = () => { const renderTabContent = () => {
switch (activeTab) { switch (activeTab) {
case "addProperty": case "addProperty":
return <Addproperty />; return <Addproperty />;
case "activeProperties": case "activeProperties":
return <p>Here are your active properties.</p>; return <div>
<h3>Active Properties</h3>
{userProperties.length > 0 ? (
<ul>
{userProperties.map((property) => (
<li key={property._id}>{property.title}</li>
))}
</ul>
) : (
<p>No active properties found.</p>
)}
</div>;
case "closedProperties": case "closedProperties":
return <p>These are your closed properties.</p>; return <p>These are your closed properties.</p>;
default: default:

View File

@ -19,5 +19,10 @@ API.interceptors.request.use((req) => {
export const signUp = (formData) => API.post("/users/signup", formData); export const signUp = (formData) => API.post("/users/signup", formData);
export const signIn = (formData) => API.post("/users/signin", formData); export const signIn = (formData) => API.post("/users/signin", formData);
export const verifyEmail = (id, token, data) => API.get(`/users/${id}/verify/${token}`, data); export const verifyEmail = (id, token, data) => API.get(`/users/${id}/verify/${token}`, data);
export const submitProperty = (propertyData) => API.post("/property", propertyData); export const submitProperty = (propertyData) => API.post("/properties", propertyData);
export const fetchUserProperties = (userId) => API.get(`/properties/user/${userId}`, userId);

View File

@ -1,5 +1,5 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
// import axios from 'axios'; import axios from 'axios';
import * as api from "../api"; import * as api from "../api";
export const submitProperty = createAsyncThunk( export const submitProperty = createAsyncThunk(
@ -14,12 +14,26 @@ export const submitProperty = createAsyncThunk(
} }
); );
export const fetchUserProperties = createAsyncThunk(
'property/fetchUserProperties',
async (userId, { rejectWithValue }) => {
try {
const response = await api.fetchUserProperties(userId);
return response.data;
} catch (error) {
return rejectWithValue(error.response.data);
}
}
);
const propertySlice = createSlice({ const propertySlice = createSlice({
name: 'property', name: 'property',
initialState: { initialState: {
property: {}, property: {},
status: 'idle', status: 'idle',
error: null, error: null,
userProperties: [],
}, },
reducers: {}, reducers: {},
extraReducers: (builder) => { extraReducers: (builder) => {
@ -34,6 +48,18 @@ const propertySlice = createSlice({
.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) => {
state.status = 'loading';
})
.addCase(fetchUserProperties.fulfilled, (state, action) => {
state.status = 'succeeded';
state.userProperties = action.payload;
})
.addCase(fetchUserProperties.rejected, (state, action) => {
state.status = 'failed';
state.error = action.payload;
}) ; }) ;
}, },
}); });