-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57f4e7a
commit 68999dc
Showing
6 changed files
with
204 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
interface ItemProps { | ||
name: string; | ||
index: number; | ||
color: string; | ||
levels: string[]; | ||
selected: number; | ||
onSelect: (level: number) => void; | ||
} | ||
|
||
export default function Item({ name, index, color, levels, selected, onSelect }: ItemProps): JSX.Element { | ||
return ( | ||
<tr className="selectable"> | ||
<td style={{ borderBottomColor: color, borderLeftColor: color }}> | ||
<label htmlFor={`check-${index}`} title="markeren"> | ||
<input | ||
type="checkbox" | ||
id={`check-${index}`} | ||
name={`check-${index}`} | ||
/> | ||
</label> | ||
</td> | ||
<th scope="row" style={{ borderBottomColor: color }}> | ||
{name} | ||
</th> | ||
{levels | ||
.map((level, levelIndex) => ({ | ||
level, | ||
key: `option-${index}-${levelIndex}`, | ||
})) | ||
.map(({ level, key }, levelIndex) => ( | ||
<td | ||
key={level} | ||
style={{ | ||
borderBottomColor: color, | ||
borderRightColor: levelIndex === levels.length - 1 ? color : undefined | ||
}}> | ||
<label htmlFor={key} title={level}> | ||
<input | ||
type="radio" | ||
name={`option-${index}`} | ||
id={key} | ||
value={levelIndex} | ||
onChange={() => onSelect(levelIndex)} | ||
checked={levelIndex === selected} | ||
/> | ||
</label> | ||
</td> | ||
))} | ||
</tr> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Theme from './Theme'; | ||
|
||
interface TableProps { | ||
themes: [string, { color: string; items: string[] }][]; | ||
levels: string[]; | ||
} | ||
|
||
export default function Table({ levels, themes }: TableProps): JSX.Element { | ||
return ( | ||
<table> | ||
<thead> | ||
<tr> | ||
<th colSpan={2} style={{ border: 'none' }}></th> | ||
{levels.map((level, levelIndex) => ( | ||
<th key={level}>{levelIndex + 1}</th> | ||
))} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{themes | ||
.map(([name, { color, items }], index) => ({ name, color, items, index })) | ||
.map((theme) => ( | ||
<Theme key={theme.name} theme={theme} levels={levels} /> | ||
))} | ||
</tbody> | ||
</table> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Fragment, useContext } from 'react'; | ||
import Item from './Item'; | ||
import { PersistenceContext } from './usePersistence'; | ||
|
||
interface ThemeProps { | ||
theme: { | ||
name: string; | ||
color: string; | ||
items: string[]; | ||
index: number; | ||
}; | ||
levels: string[]; | ||
} | ||
|
||
export default function Theme({ | ||
theme: { name, color, items, index: themeIndex }, | ||
levels, | ||
}: ThemeProps): JSX.Element { | ||
const { getLevel, select } = useContext(PersistenceContext); | ||
|
||
return ( | ||
<Fragment> | ||
<tr> | ||
<th | ||
style={{ | ||
borderBottomColor: color, | ||
}} | ||
colSpan={7} | ||
className="theme" | ||
> | ||
<h3> | ||
<span style={{ background: color }}>{themeIndex + 1}</span> | ||
{name} | ||
</h3> | ||
</th> | ||
</tr> | ||
{items.map((item, index) => ( | ||
<Item | ||
key={item} | ||
name={item} | ||
index={index} | ||
levels={levels} | ||
color={color} | ||
selected={getLevel(themeIndex, index)} | ||
onSelect={(level: number) => select(themeIndex, index, level)} | ||
/> | ||
))} | ||
</Fragment> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { createContext, useEffect, useReducer } from 'react'; | ||
|
||
type Action = { type: 'select'; theme: number; item: number; level: number }; | ||
|
||
type State = { [key: string]: number }; | ||
|
||
function reducer(state: State, action: Action) { | ||
switch (action.type) { | ||
case 'select': | ||
return { ...state, [`${action.theme}-${action.item}`]: action.level }; | ||
default: | ||
throw new Error(); | ||
} | ||
} | ||
|
||
interface PersistenceState { | ||
getLevel: (theme: number, item: number) => number; | ||
select: (theme: number, item: number, level: number) => void; | ||
} | ||
|
||
function initialState(): State { | ||
const localState = window.localStorage.getItem('persistence'); | ||
|
||
return localState ? JSON.parse(localState) : {}; | ||
} | ||
|
||
export const PersistenceContext = createContext<PersistenceState>({ | ||
getLevel: () => 0, | ||
select: () => {}, | ||
}); | ||
|
||
export default function usePersistence(): PersistenceState { | ||
const [state, dispatch] = useReducer(reducer, [], initialState); | ||
|
||
useEffect(() => { | ||
window.localStorage.setItem('persistence', JSON.stringify(state)); | ||
}, [state]); | ||
|
||
return { | ||
getLevel: (theme: number, item: number) => state[`${theme}-${item}`], | ||
select: (theme: number, item: number, level: number) => | ||
dispatch({ type: 'select', theme, item, level }), | ||
}; | ||
} |