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

Ian Chow - World Clock #65

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1,637 changes: 1,044 additions & 593 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^5.3.2",
"react": "^18.1.0",
"react-bootstrap": "^2.9.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1"
},
Expand Down
16 changes: 13 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ import React from "react";
import logo from "./logo.png";
import "./App.css";

import WorldClock from "./WorldClock";

class App extends React.Component {
render() {
const clockData = [
"Europe/Athens",
"America/Los_Angeles",
"Asia/Singapore",
];
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
{/* ParentComponent App passes this.prop.timeZone to the child component Clock and Clock can then render that prop's timeZone value*/}
<WorldClock clockData={clockData} />
</header>
</div>
);
}
}

export default App;

// We declare a new Javascript date object {new Date().toString()} in the <p> tags and rendered as a string

// We add state which will store the date and time that we will update every second in this.state. We now have this.state.date to add new Date() to the JSX
39 changes: 39 additions & 0 deletions src/Clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from "react";

class Clock extends React.Component {
constructor(props) {
super(props);
this.state = { date: new Date() };
}

componentDidMount() {
// Set date value in state every second to current date and time
// Save setInterval timerId in class variable (this.timerId) for teardown in another class method, componentWillUnmount
this.timerId = setInterval(() => {
this.setState({
date: new Date(),
});
});
}

componentWillUnmount() {
// Unmounting: When the timer component is removed from the DOM, React calls this method right before removing the component to perform cleanup tasks like clearing timers
clearInterval(this.timerId);
}

render() {
// Render just the date value stored in the state variable and remove any _ in the strings

return (
// We access the ParentComponenet's, App.js, timeZone prop in our ChildComponent here
<p>
{/* {formattedTimeZone}:&nbsp; */}
{this.state.date.toLocaleString("en-GB", {
timeZone: this.props.timeZone,
})}
</p>
);
}
}

export default Clock;
25 changes: 25 additions & 0 deletions src/WorldClock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import Clock from "./Clock";
import { Container, Row, Col } from "react-bootstrap";

class WorldClock extends React.Component {
render() {
const clockData = this.props.clockData;
return (
<Container>
{clockData.map((val, index) => (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we don't use the index, no need to define it here. Most likely your console will give you an error/warning however, in regards to not specifying a key on a collection. You would need to indicate the key property on the Row component here, and giving it the index as value seems alright most of the time

<Row className="border">
<Col md={6} className="border">
Current time in {val}:
</Col>
<Col md={6} className="border">
<Clock timeZone={val} />
</Col>
</Row>
))}
</Container>
);
}
}

export default WorldClock;
8 changes: 8 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

.border {
border: 1px solid white;
}

img {
margin-bottom: 50px;
}
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import "bootstrap/dist/css/bootstrap.min.css";

// Rendering the root React element of the React app
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);

// function tick() {
// const element = (
// <div>
// <h1>Hello, world!</h1>
// <h2>It is {new Date().toLocaleTimeString()}.</h2>
// </div>
// );
// root.render(element);
// }

// setInterval(tick, 1000); // setInterval() calls the tick function every 1000 milliseconds -> re-generating the <h2> element and the date and time every second