From c078610c4df391d2687841bb164dda26f3fe26ed Mon Sep 17 00:00:00 2001 From: philipredstone Date: Tue, 15 Apr 2025 14:32:54 +0200 Subject: [PATCH] public networks now display correctly --- frontend/src/api/network.ts | 7 +- .../src/components/networks/NetworkList.tsx | 143 ++++++++++++++---- src/controllers/network.controller.ts | 16 +- 3 files changed, 129 insertions(+), 37 deletions(-) diff --git a/frontend/src/api/network.ts b/frontend/src/api/network.ts index 0fe4de8..73605df 100644 --- a/frontend/src/api/network.ts +++ b/frontend/src/api/network.ts @@ -7,11 +7,16 @@ const port = window.location.port; const API_URL = protocol + '//' + hostname + (port ? ':' + port : '') + '/api'; // Types +export interface NetworkOwner { + _id: string; + username: string; +} + export interface Network { _id: string; name: string; description?: string; - owner: string; + owner: string | NetworkOwner; // Can be either string ID or populated object isPublic: boolean; createdAt: string; updatedAt: string; diff --git a/frontend/src/components/networks/NetworkList.tsx b/frontend/src/components/networks/NetworkList.tsx index 8e18dd0..4e5c4e1 100644 --- a/frontend/src/components/networks/NetworkList.tsx +++ b/frontend/src/components/networks/NetworkList.tsx @@ -1,9 +1,11 @@ import React, { useState } from 'react'; import { useNetworks } from '../../context/NetworkContext'; +import { useAuth } from '../../context/AuthContext'; import { useNavigate } from 'react-router-dom'; const NetworkList: React.FC = () => { const { networks, loading, error, createNetwork, deleteNetwork } = useNetworks(); + const { user } = useAuth(); const [showCreateForm, setShowCreateForm] = useState(false); const [newNetworkName, setNewNetworkName] = useState(''); const [newNetworkDescription, setNewNetworkDescription] = useState(''); @@ -153,40 +155,117 @@ const NetworkList: React.FC = () => { )} ) : ( -
- {networks.map((network) => ( -
-
-

{network.name}

- {network.description && ( -

{network.description}

- )} -
- - {network.isPublic ? 'Public' : 'Private'} - - - Created: {new Date(network.createdAt).toLocaleDateString()} - -
-
- - -
+ <> + {/* My Networks Section */} +
+

My Networks

+
+ {networks + .filter(network => { + if (!user) return false; + const ownerId = typeof network.owner === 'string' + ? network.owner + : network.owner._id; + return ownerId === user.id; + }) + .map((network) => ( +
+
+

{network.name}

+ {network.description && ( +

{network.description}

+ )} +
+ + {network.isPublic ? 'Public' : 'Private'} + + + Created: {new Date(network.createdAt).toLocaleDateString()} + +
+
+ + +
+
+
+ ))} +
+ {networks.filter(network => { + if (!user) return false; + const ownerId = typeof network.owner === 'string' + ? network.owner + : network.owner._id; + return ownerId === user.id; + }).length === 0 && ( +

You haven't created any networks yet.

+ )} +
+ + {/* Public Networks Section */} + {networks.some(network => { + if (!user) return false; + const ownerId = typeof network.owner === 'string' + ? network.owner + : network.owner._id; + return ownerId !== user.id; + }) && ( +
+

Public Networks From Others

+
+ {networks + .filter(network => { + if (!user) return false; + const ownerId = typeof network.owner === 'string' + ? network.owner + : network.owner._id; + return ownerId !== user.id; + }) + .map((network) => ( +
+
+

{network.name}

+ {network.description && ( +

{network.description}

+ )} +
+ + Public + + + Created: {new Date(network.createdAt).toLocaleDateString()} + + + By: {typeof network.owner === 'string' + ? 'Unknown' + : network.owner.username} + +
+
+ +
+
+
+ ))}
- ))} -
+ )} + )}
); diff --git a/src/controllers/network.controller.ts b/src/controllers/network.controller.ts index 7b63746..ef825af 100644 --- a/src/controllers/network.controller.ts +++ b/src/controllers/network.controller.ts @@ -3,7 +3,7 @@ import Network from '../models/network.model'; import { UserRequest } from '../types/express'; import { validationResult } from 'express-validator'; -// Get all networks for current user +// Get all networks for current user and all public networks export const getUserNetworks = async (req: UserRequest, res: Response): Promise => { try { if (!req.user) { @@ -11,7 +11,15 @@ export const getUserNetworks = async (req: UserRequest, res: Response): Promise< return; } - const networks = await Network.find({ owner: req.user._id }); + // Find networks that either: + // 1. Belong to the current user, OR + // 2. Are public networks (created by any user) + const networks = await Network.find({ + $or: [ + { owner: req.user._id }, + { isPublic: true } + ] + }).populate('owner', 'username _id'); // Populate owner field with username res.json({ success: true, data: networks }); } catch (error) { @@ -63,7 +71,7 @@ export const getNetwork = async (req: UserRequest, res: Response): Promise return; } - const network = await Network.findById(networkId); + const network = await Network.findById(networkId).populate('owner', 'username _id'); if (!network) { res.status(404).json({ message: 'Network not found' }); @@ -71,7 +79,7 @@ export const getNetwork = async (req: UserRequest, res: Response): Promise } // Check if user is owner or network is public - if (network.owner.toString() !== req.user._id.toString() && !network.isPublic) { + if (network.owner._id.toString() !== req.user._id.toString() && !network.isPublic) { res.status(403).json({ message: 'You do not have permission to access this network' }); return; }