Last active
December 15, 2016 12:41
-
-
Save terrierscript/89bdacbc496cdc20bd49 to your computer and use it in GitHub Desktop.
Reduxでのクライアントサイドvalidationをどこでやるべきか? ref: http://qiita.com/inuscript/items/5bed7812b3c1447b7b60
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
// 値は値でreducerに返す | |
export const appendTask = (task) => { | |
return {type: "TODO_ADD", payload: task} | |
} | |
// 別途、エラーがあれば、そちらもactionを生成する | |
export const checkTaskError = (task) => { | |
if(!isValidTodo(task)){ | |
return {type: "ERROR_MESSAGE", payload: "Input Some word"} | |
} | |
return {type: "ERROR_MESSAGE", payload: ""} | |
} |
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
class InputComponent extends Component{ | |
: | |
handleClick(e){ | |
: | |
this.props.actions.appendTask(currentInput) | |
// validation用にもactionを飛ばす | |
this.props.actions.checkTaskError(currentInput) | |
: |
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
const errorMessage = (state = null, action) => { | |
switch(action.type) { | |
case "TODO_ADD": | |
if(!isValidTodo(action.payload)){ | |
return "Invalid Task" | |
} | |
} | |
return state | |
} |
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
handleClick(e){ | |
let currentInput = this.state.currentInput | |
// validationエラーをstateでやる | |
if(!isValidTodo(currentInput)){ | |
this.setState({ | |
errorMessage: "Input some word" | |
}) | |
return | |
} | |
this.props.actions.appendTask(currentInput) | |
this.setState({ | |
currentInput: "" | |
}) | |
} |
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
export const appendTask = (task) => { | |
// エラーかどうかで返すactionを変える | |
if(!isValidTodo(task)){ | |
return {type: "ERROR_MESSAGE", payload: "Input Some word"} | |
} | |
return {type: "TODO_ADD", payload: task} | |
} |
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
const doValidate = (action) => { | |
let errors = [] | |
switch(action.type){ | |
case "TODO_ADD": | |
if(!isValidTodo(action.payload)){ | |
return "Invalid Task" | |
} | |
} | |
} | |
export const validateMiddleware = ({ dispatch, getStore }) => next => action => { | |
let error = doValidate(action) | |
if(error){ | |
// errorがあったらerrorMsgのactionだけ発火させる | |
dispatch(actions.errorMsg(error)) | |
return | |
} | |
let result = next(action) | |
return result | |
} |
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
const tasks = (state = [], action) => { | |
switch (action.type) { | |
case 'TODO_ADD': | |
let task = action.payload | |
// validate通った時だけpushする | |
if(isValidTodo(task)){ | |
state.push(task) | |
} | |
return state.concat() | |
case 'TODO_REMOVE': | |
: |
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
export const isValidTodo = (task) => { | |
if(task === ""){ | |
return false | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment