Firm-Collective / One-Accord

https://one-accord.vercel.app
4 stars 4 forks source link

Activity Icon Button #67

Open timbo84 opened 2 months ago

timbo84 commented 2 months ago

for this function we would like to make it that on the activity you can push the icon to "Join" the activity and it will make a call into the backend and tally up how many times it got pushed. Here is a snippet of how it could look like:

import { useState } from 'react'; import axios from 'axios'; // Make sure to install axios or use any other method to make HTTP requests

const ActivityJoinButton = ({ activityId, activityIcon }) => { const [participants, setParticipants] = useState([]);

// Function to handle the join button click const handleJoinClick = async (userId) => { try { // Send a POST request to your backend to add the user to the participants list const response = await axios.post('/api/join', { activityId, userId }); if (response.status === 200) { // Update the local state with the new list of participants setParticipants(response.data.newParticipantsList); } } catch (error) { console.error('Error joining the activity:', error); } };

return (

Participants: {participants.length}

{/* Display participant icons who joined the activity */}
{participants.map((userId) => ( User Icon ))}

); };

export default ActivityJoinButton;

timbo84 commented 2 months ago

activityId is a prop that uniquely identifies the activity. activityIcon is a prop that provides the URL to the activity’s icon image. participants is a state array that holds the list of users who have joined the activity. handleJoinClick is a function that sends a POST request to your backend when the join button is clicked. It updates the list of participants. The button is styled to display the activity’s icon as its background, making the button itself the icon. When clicked, it will add the user to the list of participants for that activity.

On the backend, you would need an endpoint (e.g., /api/join) that handles the POST request, adds the user to the participants list in the database, and returns the updated list of participants.

Please replace 'USER_ID' with the actual user ID and /path/to/user/${userId}/icon with the actual path to the user’s icon. Also, ensure that your backend properly handles the join functionality, including adding users to the activity and managing the list of participants.

This is a basic implementation, and you may need to add more features such as authentication, validation to prevent the same user from joining multiple times, and real-time updates to all clients when a new user joins an activity.