Skip to content

Commit

Permalink
Move actions to derived state
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jul 2, 2024
1 parent 079150a commit 92607ff
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ const {
i18n: { __ },
} = wp;

const { actions } = store( 'booking-form', {
const { state } = store( 'booking-form', {
state: {
get reviewFields() {
const { steps } = getContext();
return steps.reduce((acc, step) => {
return [...acc, ...step.fields];
}, []);
},
get isFirstStep() {
const { steps } = getContext();
return steps[0].isActive;
},
get isLastStep() {
const { steps } = getContext();
return steps[steps.length - 1].isActive;
},
},
actions: {
next( e ) {
e.preventDefault();
Expand Down Expand Up @@ -53,7 +69,7 @@ const { actions } = store( 'booking-form', {
context.submitting = true;

const title = __( 'Booking Form Submission', 'booking-form' );
const content = actions.reviewFields().reduce( ( acc, field ) => {
const content = state.reviewFields.reduce( ( acc, field ) => {
return `${ acc }<p><strong>${ field.label }</strong>: ${ field.value }</p>`;
}, '' );

Expand Down Expand Up @@ -95,20 +111,6 @@ const { actions } = store( 'booking-form', {
const { field } = getContext();
field.value = e.target.value;
},
reviewFields() {
const { steps } = getContext();
return steps.reduce( ( acc, step ) => {
return [ ...acc, ...step.fields ];
}, [] );
},
isFirstStep() {
const { steps } = getContext();
return steps[ 0 ].isActive;
},
isLastStep() {
const { steps } = getContext();
return steps[ steps.length - 1 ].isActive;
},
},
} );

Expand Down

0 comments on commit 92607ff

Please sign in to comment.