Skip to content

Commit

Permalink
add match toast notification
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsamaaa committed Apr 20, 2024
1 parent f7ac2e1 commit 8cf21bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
30 changes: 30 additions & 0 deletions app/browse/match-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useAppDispatch, useAppSelector } from '@/redux/hooks';
import { ProfilePassionMatchList } from '@/models/profile-passion-match-list';
import {
matchReset,
profilePassionMatchListReset,
getProfilePassionMatchList,
createMatchRecord,
} from '@/redux/features/matchSlice';
Expand All @@ -20,6 +21,8 @@ import {
} from '@/redux/features/profileSlice';
import moment from 'moment';
import { useSession } from 'next-auth/react';
import { Match } from '@/models/match';
import { toast, Toaster } from 'sonner';

type ButtonType = 'like' | 'skip';

Expand All @@ -41,6 +44,17 @@ const MatchCard = () => {
const profile: Profile = useAppSelector(
(state) => state.profileReducer.profile
);
const match: Match = useAppSelector((state) => state.matchReducer.match);

const isEmpty = (obj: any) => {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}

return true;
};

// Step 1: Fetch the logged in user's profile
// ** NOW FETCHED FROM HOME PAGE
Expand Down Expand Up @@ -91,8 +105,10 @@ const MatchCard = () => {
) {
setIsEmptyMatchList(true);
dispatch(profileReset());
dispatch(profilePassionMatchListReset());
} else {
setIsEmptyMatchList(true);
dispatch(profileReset());
}
}, [profilePassionMatchList, counter]);

Expand All @@ -101,6 +117,18 @@ const MatchCard = () => {
console.log(profile);
}, [profile]);

// Step 5: Check if it's mutual like
useEffect(() => {
if (!isEmpty(match)) {
if (match.like_1 && match.like_2) {
toast.success('You got a match! 🎉', {
duration: 2000,
});
dispatch(matchReset());
}
}
}, [match]);

const [clickedSkip, setClickedSkip] = useState<boolean>(false);
const [clickedLike, setClickedLike] = useState<boolean>(false);

Expand Down Expand Up @@ -262,6 +290,8 @@ const MatchCard = () => {
</div>
)}
</div>

<Toaster />
</>
);
};
Expand Down
11 changes: 7 additions & 4 deletions redux/features/matchSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ export const matchSlice = createSlice({
name: 'match',
initialState,
reducers: {
matchReset() {
return initialState;
matchReset(state) {
state.match = {} as Match;
},
profilePassionMatchListReset(state) {
state.profilePassionMatchList = {} as ProfilePassionMatchList;
},
},
extraReducers(builder) {
Expand Down Expand Up @@ -160,13 +163,13 @@ export const matchSlice = createSlice({
state.errorMessage = 'Failed to get create/update match record.';
})
.addMatcher(
isPending(getProfilePassionMatchList, createMatchRecord),
isPending(getProfilePassionMatchList, createMatchRecord, getAllMatches),
(state) => {
state.loading = true;
}
);
},
});

export const { matchReset } = matchSlice.actions;
export const { matchReset, profilePassionMatchListReset } = matchSlice.actions;
export default matchSlice.reducer;

0 comments on commit 8cf21bc

Please sign in to comment.