spinner works locally
This commit is contained in:
20
src/App.tsx
20
src/App.tsx
@@ -1,16 +1,20 @@
|
|||||||
import React from 'react';
|
import React from "react";
|
||||||
import CssBaseline from '@material-ui/core/CssBaseline';
|
import CssBaseline from "@material-ui/core/CssBaseline";
|
||||||
import { Router, Link } from "@reach/router"
|
import Container from "@material-ui/core/Container";
|
||||||
import { Home } from './pages/home';
|
import { Router, Link } from "@reach/router";
|
||||||
|
import { Home } from "./pages/home";
|
||||||
|
import { Room } from "./pages/room";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<CssBaseline />
|
<CssBaseline />
|
||||||
<Router>
|
<Container >
|
||||||
<Home path="/" />
|
<Router>
|
||||||
</Router>
|
<Home path="/" />
|
||||||
|
<Room path="/room/:id" />
|
||||||
|
</Router>
|
||||||
|
</Container>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
5
src/components/Spinner.module.css
Normal file
5
src/components/Spinner.module.css
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
.center {
|
||||||
|
display: flex;
|
||||||
|
flex-flow: column;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
19
src/components/Spinner.tsx
Normal file
19
src/components/Spinner.tsx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import React from "react";
|
||||||
|
import Typography from "@material-ui/core/Typography";
|
||||||
|
import Button from "@material-ui/core/Button";
|
||||||
|
import Box from "@material-ui/core/Box";
|
||||||
|
import styles from "./Spinner.module.css";
|
||||||
|
|
||||||
|
export const Spinner: React.SFC<{ value?: String; onClick: () => void; isSpinning: boolean}> = ({ onClick, isSpinning, value }) => {
|
||||||
|
const spinText = isSpinning ? "Spinning" : value;
|
||||||
|
return (
|
||||||
|
<Box className={styles.center}>
|
||||||
|
<Typography variant="h3" align="center">
|
||||||
|
Spinner: {spinText}
|
||||||
|
</Typography>
|
||||||
|
<Button disabled={isSpinning} variant="contained" color="primary" onClick={onClick}>
|
||||||
|
Spin
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,23 +1,37 @@
|
|||||||
export interface ISpinnerItem {
|
export interface ISpinnerItem {
|
||||||
value: Number,
|
value: Number,
|
||||||
color: "Black" | "Red",
|
color: "Black" | "Red",
|
||||||
isMoneyCard?: Boolean
|
isMoneyCard?: Boolean;
|
||||||
|
toString: () => String;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SpinnerItem implements ISpinnerItem {
|
||||||
|
value: Number;
|
||||||
|
color: "Black" | "Red";
|
||||||
|
isMoneyCard?: Boolean | undefined;
|
||||||
|
constructor(value: Number, color: "Black" | "Red", isMoneyCard?: Boolean) {
|
||||||
|
this.value = value;
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
return this.isMoneyCard ? `Money Card!` : `${this.value} ${this.color}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Spinner {
|
export class Spinner {
|
||||||
spinnerOptions: Array<ISpinnerItem> = [
|
spinnerOptions: Array<ISpinnerItem> = [
|
||||||
{ value: 4, color: "Black", },
|
new SpinnerItem(4, "Black"),
|
||||||
{ value: 2, color: "Red", },
|
new SpinnerItem(2, "Red"),
|
||||||
{ value: 1, color: "Black", },
|
new SpinnerItem(1, "Black"),
|
||||||
{ value: 3, color: "Red", },
|
new SpinnerItem(3, "Red"),
|
||||||
{ value: 5, color: "Black", },
|
new SpinnerItem(5, "Black"),
|
||||||
{ value: 2, color: "Red", },
|
new SpinnerItem(2, "Red"),
|
||||||
{ value: 4, color: "Black", },
|
new SpinnerItem(4, "Black"),
|
||||||
{ value: 3, color: "Red", },
|
new SpinnerItem(3, "Red"),
|
||||||
{ value: 1, color: "Black", },
|
new SpinnerItem(1, "Black"),
|
||||||
{ value: 5, color: "Red", isMoneyCard: true }
|
new SpinnerItem(5, "Red", true)
|
||||||
]
|
]
|
||||||
spin () {
|
spin () {
|
||||||
return this.spinnerOptions[Math.floor(Math.random() * this.spinnerOptions.length)];
|
return this.spinnerOptions[Math.floor(Math.random() * this.spinnerOptions.length)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/pages/room.tsx
Normal file
21
src/pages/room.tsx
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import { RouteComponentProps } from "@reach/router"
|
||||||
|
import Typography from '@material-ui/core/Typography';
|
||||||
|
import { Spinner } from '../components/Spinner';
|
||||||
|
import { Spinner as SpinnerModel, ISpinnerItem } from "../models/Spinner";
|
||||||
|
|
||||||
|
const spinnerSingleton = new SpinnerModel();
|
||||||
|
|
||||||
|
export function Room (props: RouteComponentProps<{id: Number}>) {
|
||||||
|
const [isSpinning, setIsSpinning] = useState<boolean>(false);
|
||||||
|
const [spinValue, setSpinValue] = useState<ISpinnerItem | null>(null);
|
||||||
|
return (
|
||||||
|
<Spinner value={spinValue?.toString() || ""} isSpinning={isSpinning} onClick={ () => {
|
||||||
|
setIsSpinning(true);
|
||||||
|
setTimeout(() => {
|
||||||
|
setIsSpinning(false);
|
||||||
|
setSpinValue(spinnerSingleton.spin())
|
||||||
|
},1500);
|
||||||
|
}} />
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user