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

feat: added button to stop searching for a buddy #208

Merged
merged 3 commits into from
Oct 26, 2022
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
24 changes: 24 additions & 0 deletions client/src/components/BuddyMatcher.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useChat } from 'src/context/ChatContext';
import { useNavigate } from 'react-router-dom';
import { useNotification } from 'src/lib/notification';

const stoppingSearchLoadingText = <p>Stopping the search</p>;
const BuddyMatcher = () => {
const { playNotification } = useNotification();
const navigate = useNavigate();
Expand All @@ -16,6 +17,7 @@ const BuddyMatcher = () => {

// eslint-disable-next-line no-unused-vars
const [isFound, setIsFound] = useState(false);
const [isStoppingSearch, setIsStoppingSearch] = useState(false);
const socket = useContext(SocketContext);

const userID = auth.loginId;
Expand All @@ -29,6 +31,15 @@ const BuddyMatcher = () => {
socket.emit('join', { loginId: auth.loginId, email: auth.email });
};

const handleStopSearch = () => {
socket.emit('stop_search', { loginId: auth.loginId, email: auth.email });
setIsStoppingSearch(true);
};

useEffect(() => {
setLoadingText(isStoppingSearch ? stoppingSearchLoadingText : defaultLoadingText);
}, [isStoppingSearch]);

useEffect(() => {
if (loadingText === defaultLoadingText) {
timeout = setTimeout(() => {
Expand Down Expand Up @@ -119,6 +130,11 @@ const BuddyMatcher = () => {
setIsFound(false);
});

socket.on('stop_search_success', () => {
setIsStoppingSearch(false);
navigate('/');
});

return () => {
socket
.off('connect')
Expand All @@ -136,6 +152,14 @@ const BuddyMatcher = () => {
<div className="flex w-full justify-center items-center min-h-[86.5vh] flex-col bg-primary">
<ThreeDots fill="rgb(255 159 28)" />
<div className="text-lg text-center text-white">{loadingText}</div>
{!isStoppingSearch && <button
onClick={handleStopSearch}
className={
'hover:no-underline hover:text-white font-medium text-white text-[1.5em] w-[8em] h-[2.3em] mt-4 rounded-[30px] bg-[#FF3A46] flex flex-col items-center justify-center'
}
>
Stop
</button>}
</div>
);
};
Expand Down
5 changes: 5 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ io.on('connection', (socket) => {
socket.broadcast.to(emailOrLoginId).emit('inactive');
});
});

socket.on('stop_search', async ({ loginId, email }) => {
await delWaitingUser(email ?? loginId);
socket.emit('stop_search_success');
});
});

app.use(cors());
Expand Down