Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Step Manager work #855

Draft
wants to merge 1 commit into
base: supporter_level_goal
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions client/js/nonprofits/donate/amount-step.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ const getPostfixElement = require('./postfix_element').default;
const {dollarsToCentsSafe} = require('../../../../javascripts/src/lib/format');
const { default: amount_button_contents } = require('./amount_button_contents')

function init(donationDefaults, params$) {
function init(donationDefaults, params$, stepManager) {
var state = {
params$: params$
, evolveDonation$: flyd.stream() // Stream of objects that can be used to R.evolve the initial donation object
, buttonAmountSelected$: flyd.stream(true) // Whether the button or input is selected
, currentStep$: flyd.stream()
, stepManager
}

// A stream of objects that an be used to modify the existing donation by using R.evolve
Expand Down Expand Up @@ -161,7 +162,7 @@ function amountFields(state) {
, on: {click: ev => {
state.evolveDonation$({amount: R.always(dollarsToCents(amt.amount))})
state.buttonAmountSelected$(true)
state.currentStep$(1) // immediately advance steps when selecting an amount button
state.stepManager.next() // immediately advance steps when selecting an amount button
} }
}, amount_button_contents(app.currency_symbol, amt))
])
Expand All @@ -185,7 +186,7 @@ function amountFields(state) {
, h('fieldset', [
h('button.button.u-width--full.btn-next', {
props: {type: 'submit', disabled: !state.donation$().amount || state.donation$().amount <= 0}
, on: {click: [state.currentStep$, 1]}
, on: {click: stepManager.next }
}, I18n.t('nonprofits.donate.amount.next'))
])
]),
Expand All @@ -205,7 +206,7 @@ function showSingleAmount(isRecurring, state) {
, h('span.u-padding--0', { class: {'u-hide': !isRecurring} }, ' monthly')
, h('span', {class: {'u-hide': !state.params$().designation && !gift.id}}, [ ' for ' + (desig || gift.name) ])
])
, h('button.button.u-marginBottom--20', {on: {click: [state.currentStep$, 1]}}, I18n.t('nonprofits.donate.amount.next'))
, h('button.button.u-marginBottom--20', {on: {click: state.stepManager.next}}, I18n.t('nonprofits.donate.amount.next'))
])
}

Expand Down
46 changes: 46 additions & 0 deletions client/js/nonprofits/donate/components/wizard/StepManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class StepManager implements EventTarget {
private eventTarget = new EventTarget();
private _currentStep = 0;

get currentStep(): number {
return this._currentStep;
}

private set currentStep(newStep:number) {
this._currentStep = newStep
}

next = () => {
this.currentStep = this._currentStep + 1;
}

reset = () => {
this.currentStep = 0;
}

addEventListener(type: 'updated', listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void {
this.eventTarget.addEventListener(type, listener, options);
}
dispatchEvent(event: Event): boolean {
return this.eventTarget.dispatchEvent(event);
}
removeEventListener(type: 'updated', callback: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void {
this.eventTarget.removeEventListener(type, callback, options);
}

private handleCompleted = (_evt: Event) => {
this.dispatchEvent(new Event('updated'));
}

private handleErrored = (_evt: Event) => {
this.dispatchEvent(new Event('updated'));
}

private handleSavedCard = (_evt: Event) => {
this.dispatchEvent(new Event('updated'));
}

private handleBeginSubmit = (_evt: Event) => {
this.dispatchEvent(new Event('updated'));
}
}
8 changes: 7 additions & 1 deletion client/js/nonprofits/donate/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const init = params$ => {
state.hide_anonymous = state.params$().hide_anonymous || app.nonprofit.no_anon

state.selectedPayment$ = flyd.stream('sepa')

state.stepManager = new StepManager();
state.amountStep = amountStep.init(donationDefaults, state.params$)

state.donationAmount$ = flyd.map((donation) => { return donation.amount}, state.amountStep.donation$)
Expand All @@ -82,6 +82,12 @@ const init = params$ => {
, params$: state.params$
})



const currentStep$ = flyd.stream(0);

flyd.map(() => state.stepManager.reset(), state.params$)

const currentStep$ = flyd.mergeAll([
state.amountStep.currentStep$
, state.infoStep.currentStep$
Expand Down
Loading