JavaScript and React course Β· Module 3: State and Events
Project: Martian Colony Dashboard
In this lesson5
Congratulations - you have reached the final project of the "State and Events" module! In previous lessons, you learned about useState, event handling, data flow, lifting state up, and controlled forms. Now you will combine all that knowledge into one cohesive application.
Your task is to build a Martian Colony Dashboard - an interactive panel for managing a base on Mars. Ra, your cosmic guide, has gathered the requirements from command headquarters. Mission: write a React application that monitors and controls key colony systems.
What you are building
The dashboard consists of four connected sections:
- Colony Systems Panel - monitors for atmosphere, power, water, and temperature
- Control Center - buttons for manually adjusting parameters
- Astronaut Registration Form - a controlled form for adding crew members
- Event Log - a log that every panel writes to through the
addLogEntryfunction from props
All sections must work together - changes in one panel affect the others.
Technical requirements
1. Multiple states (useState)
Each colony system has its own state:
const [atmosphere, setAtmosphere] = useState(78);
const [power, setPower] = useState(85);
const [water, setWater] = useState(62);
const [temperature, setTemperature] = useState(-10);
const [crew, setCrew] = useState([
{ id: 1, name: 'Anna Kowalska', role: 'Commander' },
{ id: 2, name: 'Jan Wisniewski', role: 'Engineer' }
]);2. Event handling
Each system has adjustment buttons. Remember the safe ranges:
const handleAtmosphereBoost = () => {
setAtmosphere(prev => Math.min(100, prev + 5));
addLogEntry('Increased O2 level by 5%');
};3. Controlled registration form
A form for adding new astronauts to the crew:
const [newCrew, setNewCrew] = useState({ name: '', role: '' });
const handleCrewChange = (event) => {
const { name, value } = event.target;
setNewCrew(prev => ({ ...prev, [name]: value }));
};
const handleCrewSubmit = (event) => {
event.preventDefault();
if (!newCrew.name.trim()) return;
setCrew(prev => [...prev, { id: Date.now(), ...newCrew }]);
setNewCrew({ name: '', role: '' });
};4. Alarms in event handlers
Check the critical state in the handler that changes the value - the alarm appears exactly at the moment of the change:
const changePower = (delta) => {
const next = Math.min(100, Math.max(0, power + delta));
setPower(next);
if (next < 20) {
addLogEntry('ALARM: Critically low power level!');
}
};Changes over time (e.g. water consumption every 5 seconds) need the useEffect hook with a cleanup - you will learn it in the module on effects and then extend this dashboard.
5. Lifting state up - shared event log
The log state and addLogEntry function live in the main component and are passed down:
function MarsColonyDashboard() {
const [log, setLog] = useState([]);
const addLogEntry = (message) => {
setLog(prev => [
{ id: Date.now(), time: new Date().toLocaleTimeString(), message },
...prev
].slice(0, 20));
};
return (
<div>
<SystemsPanel onLog={addLogEntry} />
<CrewPanel onLog={addLogEntry} />
<EventLog entries={log} />
</div>
);
}Component structure
This is what the component tree can look like - the state lives at the top, and the panels get it through props:
MarsColonyDashboard (main state)
βββ SystemsPanel (props: values + callbacks)
β βββ SystemGauge Γ 4
βββ CrewPanel (props: crew, form, callbacks)
β βββ CrewList
β βββ CrewForm
βββ EventLog (props: entries)Data flows down through props, events flow up through callbacks - the heart of unidirectional data flow in React!
Evaluation criteria
- useState - minimum 4 states (atmosphere, power, water, temperature) + crew state + form state
- Event handling - buttons adjusting parameters with safeguards (min/max)
- Controlled form - adding new astronauts with validation
- Alarms - checking the critical state in handlers and writing to the log
- Lifting state up - shared event log accessible from multiple components
- Componentization - minimum 3 separate components communicating through props
Starter skeleton
To get started, you get an app skeleton that follows the structure above: MarsColonyDashboard holds the states and the log, and the panels receive data and functions through props. The atmosphere and power buttons already work and write entries to the log - fill in the places marked TODO: the water and temperature states, the 0-100 limits, the low power alarm and the astronaut registration form.
Code for this lesson: App.jsx
import { useState } from 'react';
// Skeleton of the "Martian Colony Dashboard" project (lesson 3_7).
// The states and the log live in MarsColonyDashboard, and the panels get data and functions through props.
// Fill in the places marked TODO.
let nextLogId = 1;
function SystemGauge({ label, value, unit, onAdjust }) {
return (
<div style={styles.gauge}>
<h3 style={styles.gaugeLabel}>{label}</h3>
<p style={styles.gaugeValue}>{value}{unit}</p>
<button style={styles.button} onClick={() => onAdjust(-5)}>-5</button>
<button style={styles.button} onClick={() => onAdjust(5)}>+5</button>
</div>
);
}
function SystemsPanel({ atmosphere, power, onAtmosphereChange, onPowerChange }) {
return (
<section style={styles.panel}>
<h2 style={styles.title}>Colony systems</h2>
<div style={styles.gauges}>
<SystemGauge label="Atmosphere (O2)" value={atmosphere} unit="%" onAdjust={onAtmosphereChange} />
<SystemGauge label="Power" value={power} unit="%" onAdjust={onPowerChange} />
{/* TODO: water (%) and temperature (Β°C) gauges */}
</div>
</section>
);
}
function CrewPanel({ crew }) {
// TODO: a controlled form { name, role } with e.preventDefault() and validation.
// A function from props adds the new astronaut, because the crew state lives in the parent.
return (
<section style={styles.panel}>
<h2 style={styles.title}>Crew ({crew.length})</h2>
<ul style={styles.list}>
{crew.map((member) => (
<li key={member.id}>{member.name} - {member.role}</li>
))}
</ul>
</section>
);
}
function EventLog({ entries }) {
return (
<section style={styles.panel}>
<h2 style={styles.title}>Event log</h2>
{entries.length === 0 ? (
<p style={styles.muted}>No entries yet - use the buttons in the systems panel.</p>
) : (
<ul style={styles.list}>
{entries.map((entry) => (
<li key={entry.id}>[{entry.time}] {entry.message}</li>
))}
</ul>
)}
</section>
);
}
function MarsColonyDashboard() {
const [atmosphere, setAtmosphere] = useState(78);
const [power, setPower] = useState(85);
// TODO: the water (62) and temperature (-10) states
const [crew, setCrew] = useState([
{ id: 1, name: 'Anna Kowalska', role: 'Commander' },
{ id: 2, name: 'Jan Wisniewski', role: 'Engineer' },
]);
const [log, setLog] = useState([]);
// Shared log: the panels add entries through this function passed in props
const addLogEntry = (message) => {
const entry = { id: nextLogId++, time: new Date().toLocaleTimeString(), message };
setLog((prev) => [entry, ...prev].slice(0, 20));
};
const changeAtmosphere = (delta) => {
// TODO: keep the value within 0-100 (Math.min and Math.max)
setAtmosphere(atmosphere + delta);
addLogEntry('Atmosphere: ' + (delta > 0 ? '+' : '') + delta + '%');
};
const changePower = (delta) => {
// TODO: the 0-100 range and an alarm in the log when power drops below 20%
setPower(power + delta);
addLogEntry('Power: ' + (delta > 0 ? '+' : '') + delta + '%');
};
// TODO: an addCrewMember(member) function that adds the astronaut with setCrew
// and writes an entry to the log; pass it to CrewPanel
return (
<div style={styles.app}>
<h1 style={styles.header}>Martian Colony Dashboard</h1>
<SystemsPanel
atmosphere={atmosphere}
power={power}
onAtmosphereChange={changeAtmosphere}
onPowerChange={changePower}
/>
<CrewPanel crew={crew} />
<EventLog entries={log} />
</div>
);
}
const styles = {
app: { fontFamily: 'Arial, sans-serif', background: '#0b1020', color: '#e6ecff', minHeight: '100vh', padding: '20px' },
header: { color: '#ff8c42', textAlign: 'center', marginTop: 0 },
panel: { background: '#151c33', borderRadius: '12px', padding: '16px', marginBottom: '16px' },
title: { marginTop: 0, color: '#7fd1ff' },
gauges: { display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(160px, 1fr))', gap: '12px' },
gauge: { background: '#1e2747', borderRadius: '10px', padding: '12px', textAlign: 'center' },
gaugeLabel: { margin: '0 0 6px', fontSize: '15px' },
gaugeValue: { margin: '0 0 10px', fontSize: '28px', fontWeight: 'bold' },
button: { margin: '0 4px', padding: '6px 12px', border: 'none', borderRadius: '6px', background: '#3a4f9a', color: 'white', cursor: 'pointer' },
list: { margin: 0, paddingLeft: '20px', lineHeight: 1.7 },
muted: { color: '#8f9bbf', margin: 0 },
};
export default function App() {
return <MarsColonyDashboard />;
}Good luck, pilot! This project is your first real mission in the world of React - show that you can manage a colony on Mars. Remember: every panel is a separate ship module, but data and decisions flow through one commander - the main component.
Docs entries used in this lesson
Spotted a mistake in this lesson?