Skip to content

Commit

Permalink
Merge pull request #74 from ggggg/UX-vote-loading
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
huonggiangbui authored Dec 28, 2022
2 parents 3facdf5 + f78c263 commit 7d91038
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 6 deletions.
93 changes: 93 additions & 0 deletions client/src/components/ErrorAlert.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React from "react";

interface ErrorAlertProps {
title: string;
text?: string;
onClose: () => void;
enabled: boolean;
buttonText?: string;
}

export const ErrorAlert = ({
title,
text,
onClose,
enabled,
buttonText,
}: ErrorAlertProps) => {
return (
<div
className={`relative z-10 ${enabled ? "" : "hidden"}`}
aria-labelledby="modal-title"
role="dialog"
aria-modal={enabled}
>
{/* Background backdrop, show/hide based on modal state. */}

{/* Entering: "ease-out duration-300" */}
{/* From: "opacity-0" */}
{/* To: "opacity-100" */}
{/* Leaving: "ease-in duration-200" */}
{/* From: "opacity-100" */}
{/* To: "opacity-0" */}
<div className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity"></div>

<div className="fixed inset-0 z-10 overflow-y-auto">
<div className="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
{/* Modal panel, show/hide based on modal state. */}

{/* Entering: "ease-out duration-300" */}
{/* From: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95" */}
{/* To: "opacity-100 translate-y-0 sm:scale-100" */}
{/* Leaving: "ease-in duration-200" */}
{/* From: "opacity-100 translate-y-0 sm:scale-100" */}
{/* To: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95" */}
<div className="relative transform overflow-hidden rounded-lg bg-white text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg">
<div className="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div className="sm:flex sm:items-start">
<div className="mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10">
{/* Heroicon name: outline/exclamation-triangle */}
<svg
className="h-6 w-6 text-red-600"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth="1.5"
stroke="currentColor"
aria-hidden="true"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 10.5v3.75m-9.303 3.376C1.83 19.126 2.914 21 4.645 21h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 4.88c-.866-1.501-3.032-1.501-3.898 0L2.697 17.626zM12 17.25h.007v.008H12v-.008z"
/>
</svg>
</div>
<div className="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
<h3
className="text-lg font-medium leading-6 text-gray-900"
id="modal-title"
>
{title}
</h3>
<div className="mt-2">
<p className="text-sm text-gray-500">{text}</p>
</div>
</div>
</div>
</div>
<div className="bg-gray-50 px-4 py-3 sm:flex sm:flex-row-reverse sm:px-6">
<button
type="button"
className="inline-flex w-full justify-center rounded-md border border-transparent bg-red-600 px-4 py-2 text-base font-medium text-white shadow-sm hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 sm:ml-3 sm:w-auto sm:text-sm"
onClick={onClose}
>
{buttonText ?? "Close"}
</button>
</div>
</div>
</div>
</div>
</div>
);
};
58 changes: 56 additions & 2 deletions client/src/pages/VotePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import {
questionStarted,
} from "../constants/constants";
import { socket } from "../socket";
import { ErrorAlert } from "../components/ErrorAlert";

export const VotePage = () => {
const history = useHistory();
const cookies = new Cookies();
const [pollCode] = useState(cookies.get(pollCodeCookie));
const [started, setStarted] = useState(false);

const [loading, setLoading] = useState(false);
const [timeoutCode, setTimeoutCode] = useState(0);
const [timeoutError, setTimeoutError] = useState(false);

const [errorCode, setErrorCode] = useState(0);
const [selectedOption, setSelectionOption] = useState("");
const [isFocus, setFocus] = useState(true);
Expand Down Expand Up @@ -73,6 +78,8 @@ export const VotePage = () => {

const voteAckHandler = (data: any) => {
setSelectionOption(String.fromCharCode(data + 64));
clearTimeout(timeoutCode);
setLoading(false);
};
socket.on("ack", voteAckHandler);

Expand All @@ -85,9 +92,18 @@ export const VotePage = () => {
}, [errorCode, started, selectedOption]);

const pollButtonHandler = (selectedOption: string) => {
setLoading(true);
setTimeoutCode(
setTimeout(() => {
triggerTimeOutError();
setLoading(false);
}, 10000) as unknown as number
);
socket.emit("vote", (selectedOption.charCodeAt(0) % 65) + 1);
};

const triggerTimeOutError = () => {
setTimeoutError(true);
};
const optionButtons = () => {
const pollOptionButtons = [];
for (let i = 65; i < 70; i++) {
Expand All @@ -111,7 +127,45 @@ export const VotePage = () => {
<div className={"flex flex-col items-center px-5"}>
<Header text={`Poll Code: ${pollCode}`} />
<Header text={`Selected Option: ${selectedOption}`} />
<div className={"flex flex-col max-w-md"}>{optionButtons()}</div>
<div className={"relative"}>
<div className={"flex flex-col max-w-md"}>{optionButtons()}</div>
{loading ? (
<div className={"z-50 top-0 left-0 block w-full h-full absolute "}>
<div
className={
"flex items-center w-full bg- h-full flex-col justify-center align-middle"
}
style={{ backgroundColor: "rgba(0,0,0,0.5)" }}
>
<svg
aria-hidden="true"
className="w-1/4 h-1/4 text-gray-200 animate-spin dark:text-gray-900 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="transparent"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="white"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
</div>
) : null}
</div>
<ErrorAlert
text={"Your vote was not received, please try again."}
title={"Vote not received!"}
enabled={timeoutError}
onClose={() => {
setTimeoutError(false);
}}
/>
</div>
);
};
8 changes: 4 additions & 4 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1937,9 +1937,9 @@
integrity sha512-8MLkBIYQMuhRBQzGN9875bYsOhPnf/0rgXGo66S2FemHkhbn9qtsz9ywV1iCG+vbjigE4WUNVvw37Dx+L0qsPg==

"@types/node@^12.0.0":
version "12.20.33"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.33.tgz#24927446e8b7669d10abacedd16077359678f436"
integrity sha512-5XmYX2GECSa+CxMYaFsr2mrql71Q4EvHjKS+ox/SiwSdaASMoBIWE6UmZqFO+VX1jIcsYLStI4FFoB6V7FeIYw==
version "12.20.55"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240"
integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==

"@types/normalize-package-data@^2.4.0":
version "2.4.1"
Expand Down Expand Up @@ -10825,7 +10825,7 @@ [email protected], source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, sourc
source-map@^0.5.0, source-map@^0.5.6:
version "0.5.7"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
integrity sha512-LbrmJOMUSdEVxIKvdcJzQC+nQhe8FUZQTXQy6+I75skNgn3OoQ0DZA8YnFa7gp8tqtL3KPf1kmo0R5DoApeSGQ==

source-map@^0.7.3, source-map@~0.7.2:
version "0.7.3"
Expand Down

0 comments on commit 7d91038

Please sign in to comment.