Skip to content

Commit

Permalink
Add color and delay
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Dadoune authored and Reed Dadoune committed May 31, 2017
1 parent 2b3b0cd commit f2e1cf0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/actions/autoComplete.ts
Expand Up @@ -44,5 +44,6 @@ export const saveEpic: Epic<any, any> = (action$) =>
.mergeMap((action) =>
Observable
.of(action.item)
.delay((Math.floor(Math.random() * 10) + 1) * 1000)
.map((response) => savedResult(response)),
);
7 changes: 6 additions & 1 deletion src/components/itemList/presenter.tsx
Expand Up @@ -5,7 +5,12 @@ export class ItemList extends React.Component<any, any> {
return (
<ul>
{this.props.items.map((item: any, index: number) => {
return <li key={item.abbr}>{item.name}</li>;
return (
<li
style={{color: item.saving ? 'red' : 'black'}}
key={item.abbr}>
{item.name}
</li>);
})}
</ul>
);
Expand Down
3 changes: 2 additions & 1 deletion src/index.tsx
Expand Up @@ -11,8 +11,9 @@ const store = configureStore();
ReactDOM.render(
<Provider store={store}>
<div>
<Autocomplete />
<h3>My Favorite States</h3>
<ItemList />
<Autocomplete />
</div>
</Provider>,
document.getElementById('app'),
Expand Down
22 changes: 19 additions & 3 deletions src/reducers/itemList.ts
Expand Up @@ -6,19 +6,35 @@ const initialState = {

export default function(state = initialState, action: any) {
switch (action.type) {
case actionTypes.SAVE:
return save(state, action);
case actionTypes.SAVE_RESULT:
return saveItem(state, action);
return saveResult(state, action);
}
return state;
}

function saveItem(state: any, action: any) {
function save(state: any, action: any) {
const { item } = action;
const { items } = state;
for (const existing of items) {
if (existing.abbr === item.abbr) {
return state;
}
}
return {...state, items: [...items, item]};
return {...state, items: [...items, {
...item,
saving: true,
}]};
}

function saveResult(state: any, action: any) {
const { item } = action;
const { items } = state;
return {...state, items: items.map((existing: any) => {
if (existing.abbr === item.abbr) {
return item;
}
return existing;
})};
}

0 comments on commit f2e1cf0

Please sign in to comment.