immer'ified

This commit is contained in:
Ken
2019-01-30 15:06:03 -08:00
parent c44af2a38f
commit e3e977a98d
5 changed files with 33 additions and 8 deletions

View File

@@ -10,18 +10,19 @@ export interface TodoAppProps {
filter: FilterTypes; filter: FilterTypes;
add: (label: string) => void; add: (label: string) => void;
remove: (id: string) => void; remove: (id: string) => void;
complete: (id: string) => void;
setFilter: (filter: FilterTypes) => void; setFilter: (filter: FilterTypes) => void;
} }
export class TodoApp extends React.Component<TodoAppProps> { export class TodoApp extends React.Component<TodoAppProps> {
render() { render() {
const { todos, filter, add, remove, setFilter } = this.props; const { todos, filter, add, remove, setFilter, complete } = this.props;
return ( return (
<Stack horizontalAlign="center"> <Stack horizontalAlign="center">
<Stack style={{ width: 650 }} verticalGap={25}> <Stack style={{ width: 650 }} verticalGap={25}>
<TodoHeader {...{ add, remove, filter }} /> <TodoHeader {...{ add, remove, filter }} />
<TodoList {...{ todos, filter }} /> <TodoList {...{ todos, filter, complete }} />
<TodoFooter {...{ todos, setFilter }} /> <TodoFooter {...{ todos, setFilter }} />
</Stack> </Stack>
</Stack> </Stack>

View File

@@ -6,6 +6,7 @@ import { TodoItem, FilterTypes } from '../store';
export interface TodoListProps { export interface TodoListProps {
todos: { [id: string]: TodoItem }; todos: { [id: string]: TodoItem };
filter: FilterTypes; filter: FilterTypes;
complete: (id: string) => void;
} }
export class TodoList extends React.Component<TodoListProps> { export class TodoList extends React.Component<TodoListProps> {
@@ -15,7 +16,7 @@ export class TodoList extends React.Component<TodoListProps> {
<Stack verticalGap={10}> <Stack verticalGap={10}>
{Object.keys(todos).map(id => { {Object.keys(todos).map(id => {
const todo = todos[id]; const todo = todos[id];
return <TodoListItem key={id} checked={todo.completed} label={todo.label} />; return <TodoListItem key={id} checked={todo.completed} label={todo.label} complete={this.props.complete} id={id} />;
})} })}
</Stack> </Stack>
); );

View File

@@ -1,16 +1,31 @@
import React from 'react'; import React from 'react';
import { Text, Stack } from '@uifabric/experiments'; import { Stack } from '@uifabric/experiments';
import { Checkbox } from 'office-ui-fabric-react'; import { Checkbox, IconButton } from 'office-ui-fabric-react';
import { mergeStyles } from '@uifabric/styling';
export interface TodoListItemProps { export interface TodoListItemProps {
id: string;
checked: boolean; checked: boolean;
label: string; label: string;
complete: (id: string) => void;
} }
export const TodoListItem = (props: TodoListItemProps) => { export const TodoListItem = (props: TodoListItemProps) => {
const className = mergeStyles({
selectors: {
':global(.clearButton)': {
display: 'none'
},
'&:hover :global(.clearButton)': {
display: 'block'
}
}
});
return ( return (
<Stack horizontal> <Stack horizontal className={className}>
<Checkbox label={props.label} checked={props.checked} /> <Checkbox label={props.label} checked={props.checked} onChange={() => props.complete(props.id)} />
<IconButton iconProps={{ iconName: 'Cancel' }} className="clearButton" />
</Stack> </Stack>
); );
}; };

View File

@@ -4,9 +4,12 @@ import { createStore } from 'redux';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { reducer } from './reducers'; import { reducer } from './reducers';
import TodoAppContainer from './components/TodoAppContainer'; import TodoAppContainer from './components/TodoAppContainer';
import { initializeIcons } from '@uifabric/icons';
const store = createStore(reducer); declare var window: any;
const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
initializeIcons();
ReactDOM.render( ReactDOM.render(
<Provider store={store}> <Provider store={store}>
<TodoAppContainer /> <TodoAppContainer />

View File

@@ -17,6 +17,11 @@ export const reducer = combineReducers<Store>({
remove(draft, action) { remove(draft, action) {
delete draft[action.id]; delete draft[action.id];
return draft; return draft;
},
complete(draft, action) {
draft[action.id].completed = !draft[action.id].completed;
return draft;
} }
} }
), ),