Skip to content

Commit

Permalink
Day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
phptuts committed May 22, 2022
1 parent 268aeb0 commit f277f73
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
14 changes: 2 additions & 12 deletions src/components/LedStrip/FullStrip.js
Expand Up @@ -23,19 +23,9 @@ const FullStrip = ({ leds }) => {
return (
<>
<Strip leds={leftLeds} direction="left" />
<Strip
leds={rightDownLeds.map((l) => {
return { ...l, color: '#00AA00' };
})}
direction="right-down"
/>
<Strip leds={rightDownLeds} direction="right-down" />
<Strip leds={rightToLeft} direction="right" />
<Strip
leds={leftDown.map((l) => {
return { ...l, color: '#00AA00' };
})}
direction="left-down"
/>
<Strip leds={leftDown} direction="left-down" />
</>
);
};
Expand Down
18 changes: 14 additions & 4 deletions src/components/LedStrip/Led.js
@@ -1,11 +1,21 @@
import React from 'react';
import React, { useContext } from 'react';
import LedsContext from '../../context/ledContext';

const Led = ({ led }) => {
const { selectLed } = useContext(LedsContext);

function onSelectLed() {
selectLed(led);
}

const Led = ({ color, position, connectionClass = '' }) => {
return (
<div className={`led ${connectionClass}`}>
<div
onClick={onSelectLed}
className={`led ${led.selected ? 'selected' : ''} ${led.position}`}
>
<div
style={{
backgroundColor: color,
backgroundColor: led.color,
}}
className="light"
></div>
Expand Down
7 changes: 5 additions & 2 deletions src/components/LedStrip/Leds.js
@@ -1,9 +1,12 @@
import React from 'react';
import React, { useContext } from 'react';
import FullStrip from './FullStrip';
import config from '../../config';
import { chunk } from '../../helpers';
import ledsContext from '../../context/ledContext';

const Leds = () => {
const { leds } = useContext(ledsContext);

const Leds = ({ leds }) => {
const ledFullStips = chunk(leds, config.fullStripLength);
return (
<div id="leds">
Expand Down
8 changes: 1 addition & 7 deletions src/components/LedStrip/Strip.js
Expand Up @@ -5,13 +5,7 @@ const Strip = ({ leds, direction }) => {
return (
<div className={`strip ${direction}`}>
{leds.map((led) => {
return (
<Led
position={led.position}
key={`led-${led.position}`}
color={led.color}
/>
);
return <Led led={led} key={`led-${led.position}`} />;
})}
</div>
);
Expand Down
5 changes: 5 additions & 0 deletions src/context/ledContext.js
@@ -0,0 +1,5 @@
import { createContext } from 'react';

const ledsContext = createContext();

export default ledsContext;
6 changes: 6 additions & 0 deletions src/index.css
Expand Up @@ -16,7 +16,13 @@
border: solid black 1px;
width: 20px;
height: 20px;
cursor: pointer;
}

.led.selected {
background-color: #1a2201;
}

#leds {
position: relative;
width: 1000px;
Expand Down
1 change: 1 addition & 0 deletions src/leds.js
Expand Up @@ -11,5 +11,6 @@ export const generateLed = (color, position) => {
return {
color,
position,
selected: false,
};
};
30 changes: 26 additions & 4 deletions src/pages/Home.js
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from 'react';
import Leds from '../components/LedStrip/Leds';
import Player from '../components/Player';
import { generateLeds } from '../leds';
import LedsContext from '../context/ledContext';

const Home = () => {
const [currentFrameIndex, setCurrentFrameIndex] = useState(1);
Expand All @@ -13,21 +14,42 @@ const Home = () => {
setLeds(generateLeds(+e.target.value));
}

function onChangeColor(e) {
setLeds(
leds.map((l) => {
if (l.selected) {
return { ...l, color: e.target.value };
}
return l;
})
);
}

function selectLed(led) {
const newLeds = [
...leds.filter((l) => l.position !== led.position),
{ ...led, selected: !led.selected },
];

newLeds.sort((a, b) => a.position - b.position);
setLeds(newLeds);
}

return (
<>
<LedsContext.Provider value={{ leds, selectLed }}>
<div className="row">
<div className="col">
<h1>Home Page {numberLeds}</h1>
</div>
</div>
<div className="row">
<div className="col">
<input type="color" name="" id="" />
<input type="color" onChange={onChangeColor} />
</div>
</div>
<div className="row">
<div className="col">
<Leds leds={leds} />
<Leds />
</div>
</div>
<div className="row">
Expand Down Expand Up @@ -58,7 +80,7 @@ const Home = () => {
</div>
</div>
</div>
</>
</LedsContext.Provider>
);
};

Expand Down

0 comments on commit f277f73

Please sign in to comment.