Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjusting implementation to redux-elm v2
- Loading branch information
Showing
15 changed files
with
125 additions
and
164 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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { Updater, Matchers } from 'redux-elm'; | ||
import { Updater } from 'redux-elm'; | ||
|
||
export const isActive = model => model; | ||
|
||
export default new Updater(false, Matchers.exactMatcher) | ||
.case('Toggle', function*(model) { | ||
return !model; | ||
}) | ||
.toReducer(); | ||
export const initialModel = false; | ||
|
||
export default new Updater(initialModel) | ||
.case('Toggle', model => !model) | ||
.toReducer(); |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React from 'react'; | ||
import { view } from 'redux-elm'; | ||
|
||
export default ({ model, dispatch }) => | ||
<button style={{ color: model ? 'green' : 'red'}} onClick={() => dispatch({ type: 'Toggle'})}>Toggle</button>; | ||
export default view(({ model, dispatch }) => | ||
<button style={{ color: model ? 'green' : 'red'}} onClick={() => dispatch({ type: 'Toggle'})}>Toggle</button>); |
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { Updater, Matchers } from 'redux-elm'; | ||
import { Updater } from 'redux-elm'; | ||
|
||
export function increment(model) { | ||
return model + 1; | ||
} | ||
export const increment = model => model + 1; | ||
|
||
export function incrementByTwo(model) { | ||
export const incrementByTwo = model => { | ||
if (model >= 10) { | ||
return model + 2; | ||
} else { | ||
return model + 1; | ||
} | ||
} | ||
|
||
export default new Updater(0) | ||
export const initialModel = 0; | ||
|
||
export default new Updater(initialModel) | ||
.toReducer(); |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import React from 'react'; | ||
import { view } from 'redux-elm'; | ||
|
||
export default ({ model }) => | ||
<div>Value: {model}</div>; | ||
export default view(({ model }) => | ||
<div>Value: {model}</div>); | ||
|
36 changes: 14 additions & 22 deletions
36
redux-elm-tomkis1/src/gif-viewer-pair-of-pairs/updater.js
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 |
---|---|---|
@@ -1,25 +1,17 @@ | ||
import { Updater, mapEffects, Matchers } from 'redux-elm'; | ||
import { Updater } from 'redux-elm'; | ||
import { takeEvery } from 'redux-saga'; | ||
import { put } from 'redux-saga/effects'; | ||
|
||
import gifViewerPairUpdater, { init as gifViewerPairInit } from '../gif-viewer-pair/updater'; | ||
import gifViewerUpdater, { initialModel as gifViewerInitialModel } from '../gif-viewer-pair/updater'; | ||
|
||
export function* init() { | ||
return { | ||
leftPair: yield* mapEffects(gifViewerPairInit(), 'LeftPair'), | ||
rightPair: yield* mapEffects(gifViewerPairInit(), 'RightPair') | ||
}; | ||
} | ||
export const initialModel = { | ||
leftPair: gifViewerInitialModel, | ||
rightPair: gifViewerInitialModel | ||
}; | ||
|
||
export default new Updater(init) | ||
.case('LeftPair', function*(model, action) { | ||
return { | ||
...model, | ||
leftPair: yield* mapEffects(gifViewerPairUpdater(model.leftPair, action), 'LeftPair') | ||
}; | ||
}) | ||
.case('RightPair', function*(model, action) { | ||
return { | ||
...model, | ||
rightPair: yield* mapEffects(gifViewerPairUpdater(model.rightPair, action), 'RightPair') | ||
}; | ||
}) | ||
.toReducer(); | ||
export default new Updater(initialModel) | ||
.case('LeftPair', (model, action) => | ||
({ ...model, leftPair: gifViewerUpdater(model.leftPair, action) })) | ||
.case('RightPair', (model, action) => | ||
({ ...model, rightPair: gifViewerUpdater(model.rightPair, action) })) | ||
.toReducer(); |
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 |
---|---|---|
@@ -1,28 +1,17 @@ | ||
import { Updater, mapEffects, Matchers } from 'redux-elm'; | ||
import { Updater } from 'redux-elm'; | ||
import { takeEvery } from 'redux-saga'; | ||
import { put } from 'redux-saga/effects'; | ||
|
||
import gifViewerUpdater, { init as gifViewerInit, fetchGif } from '../gif-viewer/updater'; | ||
import gifViewerUpdater, { init as gifViewerInit } from '../gif-viewer/updater'; | ||
|
||
const funnyCatsGifViewerInit = gifViewerInit('funny cats'); | ||
const funnyDogsGifViewerInit = gifViewerInit('funny dogs'); | ||
export const initialModel = { | ||
top: gifViewerInit('funny cats'), | ||
bottom: gifViewerInit('funny dogs') | ||
}; | ||
|
||
export function* init() { | ||
return { | ||
top: yield* mapEffects(funnyCatsGifViewerInit(), 'Top'), | ||
bottom: yield* mapEffects(funnyDogsGifViewerInit(), 'Bottom') | ||
}; | ||
} | ||
|
||
export default new Updater(init) | ||
.case('Top', function*(model, action) { | ||
return { | ||
...model, | ||
top: yield* mapEffects(gifViewerUpdater(model.top, action), 'Top') | ||
}; | ||
}) | ||
.case('Bottom', function*(model, action) { | ||
return { | ||
...model, | ||
bottom: yield* mapEffects(gifViewerUpdater(model.bottom, action), 'Bottom') | ||
}; | ||
}) | ||
export default new Updater(initialModel) | ||
.case('Top', (model, action) => | ||
({ ...model, top: gifViewerUpdater(model.top, action) })) | ||
.case('Bottom', (model, action) => | ||
({ ...model, bottom: gifViewerUpdater(model.bottom, action) })) | ||
.toReducer(); |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import React from 'react'; | ||
import { forwardTo } from 'redux-elm'; | ||
import { forwardTo, view } from 'redux-elm'; | ||
|
||
import GifViewer from '../gif-viewer/view'; | ||
|
||
export default ({ model, dispatch }) => ( | ||
export default view(({ model, dispatch }) => ( | ||
<div> | ||
<GifViewer model={model.top} dispatch={forwardTo(dispatch, 'Top')} /> | ||
<GifViewer model={model.bottom} dispatch={forwardTo(dispatch, 'Bottom')} /> | ||
</div> | ||
); | ||
)); |
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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import request from 'superagent-bluebird-promise'; | ||
require('isomorphic-fetch'); | ||
|
||
export const fetchGif = (dispatch, topic) => { | ||
request(`http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=${topic}`) | ||
.then(response => dispatch({ type: 'NewGif', url: response.body.data.image_url })); | ||
}; | ||
export const fetchGif = topic => fetch(`http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=${topic}`) | ||
.then(response => { | ||
if (response.status > 400) { | ||
throw new Error('Error while fetching from the server'); | ||
} else { | ||
return response.json(); | ||
} | ||
}) | ||
.then(body => body.data.image_url); | ||
|
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 |
---|---|---|
@@ -1,34 +1,30 @@ | ||
import { Updater, Matchers } from 'redux-elm'; | ||
import { sideEffect } from 'redux-side-effects'; | ||
import { Updater } from 'redux-elm'; | ||
import { takeEvery } from 'redux-saga'; | ||
import { call, put, select } from 'redux-saga/effects'; | ||
|
||
import * as Effects from './effects'; | ||
|
||
export function init(topic) { | ||
return function*() { | ||
yield sideEffect(Effects.fetchGif, topic); | ||
const getTopic = model => model.topic; | ||
|
||
return { | ||
topic, | ||
gifUrl: null | ||
}; | ||
}; | ||
}; | ||
function* fetchGif() { | ||
const topic = yield select(getTopic); | ||
const url = yield call(Effects.fetchGif, topic); | ||
yield put({ type: 'NewGif', url }); | ||
} | ||
|
||
export function* fetchGif(model) { | ||
yield sideEffect(Effects.fetchGif, model.topic); | ||
function* saga() { | ||
yield* fetchGif(); | ||
yield* takeEvery('RequestMore', fetchGif); | ||
} | ||
|
||
return { | ||
...model, | ||
gifUrl: null | ||
}; | ||
}; | ||
export const requestMore = () => ({ type: 'RequestMore' }); | ||
|
||
export default new Updater(init('funny cats'), Matchers.exactMatcher) | ||
.case('NewGif', function*(model, action) { | ||
return { | ||
...model, | ||
gifUrl: action.url | ||
} | ||
}) | ||
.case('RequestMore', fetchGif) | ||
.toReducer(); | ||
export const init = topic => ({ | ||
topic, | ||
gifUrl: null | ||
}); | ||
|
||
export default new Updater(init('funny cats'), saga) | ||
.case('NewGif', (model, { url }) => ({ ...model, gifUrl: url })) | ||
.case('RequestMore', model => ({ ...model, gifUrl: null })) | ||
.toReducer(); |
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
Oops, something went wrong.