Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(unmatch): Create unmatch button for matched profiles #18

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/components/GroupsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { useAuthState } from '@auth/useAuthState';
import ProfileCard from '@components/Profile/ProfileCard';
import StudentCard from '@components/Profile/UserCard';
import useUserProfile from '@data/useUserProfile';
import { resolveMatchRequest, getUserMatches } from '@firestore/matches';
import {
resolveMatchRequest,
getUserMatches,
removeMatch,
getMatchedUserUids,
} from '@firestore/matches';
import { fetchUserProfile } from '@firestore/userProfile';
import { Box, Stack, Typography } from '@mui/material';

Expand Down Expand Up @@ -75,6 +80,32 @@ function GroupsPage() {
}
};

const handleRemoveMatch = async (profile) => {
try {
// Get the matched user IDs for the current user
const matchedUserUids = await getMatchedUserUids(userProfile.uid);

// Check if this profile is one of the matches
const sharedMatchId = profile.currentMatches.find((matchId) =>
matchedUserUids.includes(profile.uid),
);

if (!sharedMatchId) {
console.error('Match not found for the given profile');
return;
}

await removeMatch(sharedMatchId);

// Update the matchProfiles state by filtering out the removed match
setMatchProfiles((prevProfiles) =>
prevProfiles.filter((matchProfile) => matchProfile.uid !== profile.uid),
);
} catch (error) {
console.error('Error handling match removal:', error);
}
};

const handleOpenProfileModal = (profile) => {
setSelectedProfile(profile);
setOpenProfileModal(true);
Expand Down Expand Up @@ -114,6 +145,12 @@ function GroupsPage() {
label: 'View Profile',
onClick: () => handleOpenProfileModal(profile),
},
{
label: 'Unmatch',
onClick: () => handleRemoveMatch(profile),
variant: 'outlined',
color: 'secondary',
},
];
return <StudentCard key={index} studentUserProfile={profile} actions={actions} />;
})
Expand Down
43 changes: 43 additions & 0 deletions src/utils/firestore/matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,49 @@ export const createMatch = async (users, location, description = '') => {
}
};

// Remove an existing match
export const removeMatch = async (matchId) => {
if (!matchId) {
throw new Error('Missing match ID');
}

const matchRef = doc(db, 'matches', matchId);

try {
await runTransaction(db, async (transaction) => {
const matchDoc = await transaction.get(matchRef);

if (!matchDoc.exists()) {
throw new Error('Match not found');
}

const matchData = matchDoc.data();
const user0 = matchData.users[0];
const user1 = matchData.users[1];

// Fetch the user profiles from Firestore
const user0Ref = doc(db, 'users', user0.uid);
const user1Ref = doc(db, 'users', user1.uid);

// Remove the match reference from both users
transaction.update(user0Ref, {
currentMatches: arrayRemove(matchId),
});

transaction.update(user1Ref, {
currentMatches: arrayRemove(matchId),
});

// Finally, delete the match document
transaction.delete(matchRef);
});

console.log('Match removed with ID: ', matchId);
} catch (error) {
console.error('Error removing match:', error);
}
};

// Get all user matches
export const getUserMatches = async (uid) => {
try {
Expand Down
Loading