-
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.
Add a summing pair of counters example
- Loading branch information
Showing
4 changed files
with
95 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { Record } from 'immutable'; | ||
import { Union } from 'results'; | ||
import { component, Update } from '../../spindle'; | ||
|
||
|
||
const Model = Record({ | ||
value: 0, | ||
}); | ||
|
||
|
||
const Msg = Union({ | ||
Increment: null, | ||
Decrement: null, | ||
}); | ||
|
||
|
||
const update = (msg, model) => Msg.match(msg, { | ||
Increment: () => { | ||
const newModel = model.update('value', v => v + 1); | ||
return Update({ | ||
model: newModel, | ||
emit: newModel.get('value'), | ||
}); | ||
}, | ||
|
||
Decrement: () => { | ||
const newModel = model.update('value', v => v - 1); | ||
return Update({ | ||
model: newModel, | ||
emit: newModel.get('value'), | ||
}); | ||
}, | ||
}); | ||
|
||
|
||
const view = (model, BoundMsg) => ( | ||
<p> | ||
<button onClick={BoundMsg.Decrement}>-</button> | ||
{model.get('value')} | ||
<button onClick={BoundMsg.Increment}>+</button> | ||
</p> | ||
); | ||
|
||
|
||
export default component('Counter', | ||
{ Model, Msg, update, view }); |
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,40 @@ | ||
import React from 'react'; | ||
import { Record } from 'immutable'; | ||
import { Union } from 'results'; | ||
import { component, Update } from '../../spindle'; | ||
import Counter from './Counter'; | ||
|
||
|
||
const Model = Record({ | ||
topValue: null, | ||
bottomValue: null, | ||
}); | ||
|
||
|
||
const Msg = Union({ | ||
TopValue: null, | ||
BottomValue: null, | ||
}); | ||
|
||
|
||
const update = (msg, model) => Msg.match(msg, { | ||
TopValue: v => | ||
Update({ model: model.set('topValue', v) }), | ||
|
||
BottomValue: v => | ||
Update({ model: model.set('bottomValue', v) }), | ||
}); | ||
|
||
|
||
const view = (model, BoundMsg) => ( | ||
<div> | ||
<p>here are some counters:</p> | ||
<Counter onEmit={BoundMsg.TopValue} /> | ||
<Counter onEmit={BoundMsg.BottomValue} /> | ||
<p>their sum is: {model.get('topValue') + model.get('bottomValue')}</p> | ||
</div> | ||
); | ||
|
||
|
||
export default component('SummedPair', | ||
{ Model, Msg, update, view }); |
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,6 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import SummedPair from './SummedPair'; | ||
|
||
|
||
ReactDOM.render(<SummedPair />, document.getElementById('app')); |
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