Skip to content

Commit

Permalink
saving for tomorrow
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Jones authored and Ryan Jones committed Sep 9, 2019
1 parent dead30c commit 7af4de9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
26 changes: 23 additions & 3 deletions ensure-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ let captureItemReducer = produce((draft, action) => {
return action.state;
}
case CAPTURE_FORCE_REQUIRED: {
draft._forceRequired = true;
break;
}
default: {
throw new Error(
\`Unknown action type "\${action.type}" in update item reducer.\`
Expand All @@ -59,10 +64,12 @@ export let setField = (key, value) => ({
let CAPTURE_RESET = 'capture/RESET';
export let reset = state => ({ type: CAPTURE_RESET, state });
let CAPTURE_FORCE_REQUIRED = 'capture/FORCE_REQUIRED';
let CaptureItemContext = React.createContext({});
export let CaptureItemProvider = CaptureItemContext.Provider;
export let useCaptureItem = ({
path = null, format = identity, validate = null
path = null, format = identity, validate = null, required = false
} = {}) => {
let captureItem = useContext(CaptureItemContext);
let touched = useRef(false);
Expand All @@ -83,8 +90,11 @@ export let useCaptureItem = ({
if (!item) return {};
let value = format.in(get(item, path));
let isValid =
touched.current && validate ? fromValidate[validate](value) : true;
validate && (touched.current || (required && item._forceRequired))
? fromValidate[validate](value)
: true;
let onChange = value => {
touched.current = true;
dispatch(setField(path, format.out(value)));
Expand All @@ -105,7 +115,17 @@ export let useCaptureItemProvider = (item, onSubmit) => {
dispatch(reset(item));
}, [item]);
return useMemo(() => [state, dispatch, onSubmit], [
return useMemo(() => {
async function _onSubmit() {
try {
if(!await onSubmit()) return;
} catch(error) {
console.log('onSubmit', error);
}
dispatch({ type: CAPTURE_FORCE_REQUIRED });
}
return [state, dispatch, _onSubmit];
}, [
state,
dispatch,
onSubmit,
Expand Down
3 changes: 3 additions & 0 deletions morph/react/get-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,7 @@ function maybeDataFormat(format, data) {
function maybeDataValidate(validate, data) {
if (!validate || validate.type !== 'js') return
data.push(`validate: '${validate.value}',`)
if (validate.required) {
data.push('required: true')
}
}
4 changes: 3 additions & 1 deletion parse/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@ export let getDataFormat = maybeProp => {

export let getDataValidate = maybeProp => {
if (!maybeProp || !maybeProp.value) return null
let [value, required] = maybeProp.value.split(' ')
return {
type: 'js',
value: maybeProp.value,
value,
required: required === 'required',
}
}

Expand Down

0 comments on commit 7af4de9

Please sign in to comment.