Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.

Commit 2f1c753

Browse files
author
James Baxley
authored
Schedule options reset (#1715)
* fix reseting of schedule status on successful transaction * updated changelog * fix flow error
1 parent 82c0202 commit 2f1c753

12 files changed

Lines changed: 72 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
- fixed live formatting of add to cart inputs when adding less than a dollar
2323
- fixed schedule date to be correct based on utc time
2424
- fixed issue when scheduling using custom frequency
25+
- fixed reseting of schedule on giving add to cart form

imports/components/@primitives/UI/forms/Checkbox.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export default class Checkbox extends React.Component {
3434
checked: false,
3535
}
3636

37+
componentWillReceiveProps(nextProps) {
38+
if (this.props.defaultValue && !nextProps.defaultValue) {
39+
this.setState({ checked: false });
40+
}
41+
}
42+
3743
disabled = () => {
3844
if (this.props.disabled) {
3945
return this.props.disabled; // eslint-disable-line
@@ -109,7 +115,7 @@ export default class Checkbox extends React.Component {
109115
style={{ marginTop: "-1px", cursor: "pointer" }}
110116
htmlFor={this.props.id || this.props.label || this.props.name}
111117
>
112-
<small data-spec="input-label" style={this.getStyles(this.props.defaultValue)}>
118+
<small data-spec="input-label" style={this.getStyles(this.state.checked !== false)}>
113119
{this.props.children}
114120
</small>
115121
</label>
@@ -120,9 +126,10 @@ export default class Checkbox extends React.Component {
120126
name={this.props.name || this.props.label}
121127
className={this.props.inputClasses}
122128
disabled={this.disabled()}
123-
defaultChecked={(this.state.checked !== false || this.props.defaultValue) ? "checked" : ""}
129+
checked={(this.state.checked !== false || this.props.defaultValue) ? "checked" : ""}
124130
onClick={this.props.clicked}
125131
style={{ width: 0 }}
132+
126133
data-spec="input"
127134
/>
128135

imports/components/@primitives/UI/forms/__tests__/Checkbox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ it ("should accept a default value", () => {
2121

2222
const inputProps = getSingleSpecWrapper(component, "input").props();
2323

24-
expect(inputProps.defaultChecked).toEqual("checked");
24+
expect(inputProps.checked).toEqual("checked");
2525
});
2626

2727
it ("should disable input with disabled prop", () => {

imports/components/@primitives/UI/forms/__tests__/__snapshots__/Checkbox.js.snap

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ exports[`test pushes type prop to inputclasses 1`] = `
1515
style={Object {}} />
1616
</label>
1717
<input
18+
checked=""
1819
data-spec="input"
19-
defaultChecked=""
2020
style={
2121
Object {
2222
"width": 0,
@@ -57,15 +57,11 @@ exports[`test should accept a default value 1`] = `
5757
}>
5858
<small
5959
data-spec="input-label"
60-
style={
61-
Object {
62-
"color": "#303030",
63-
}
64-
} />
60+
style={Object {}} />
6561
</label>
6662
<input
63+
checked="checked"
6764
data-spec="input"
68-
defaultChecked="checked"
6965
style={
7066
Object {
7167
"width": 0,
@@ -100,8 +96,8 @@ exports[`test should accept an id 1`] = `
10096
style={Object {}} />
10197
</label>
10298
<input
99+
checked=""
103100
data-spec="input"
104-
defaultChecked=""
105101
id="harambe"
106102
style={
107103
Object {
@@ -139,8 +135,8 @@ exports[`test should accept label 1`] = `
139135
style={Object {}} />
140136
</label>
141137
<input
138+
checked=""
142139
data-spec="input"
143-
defaultChecked=""
144140
id="harambe"
145141
name="harambe"
146142
style={
@@ -179,8 +175,8 @@ exports[`test should accept name prop 1`] = `
179175
style={Object {}} />
180176
</label>
181177
<input
178+
checked=""
182179
data-spec="input"
183-
defaultChecked=""
184180
id="harambe"
185181
name="harambe"
186182
style={
@@ -223,8 +219,8 @@ exports[`test should add classes with classes prop 1`] = `
223219
style={Object {}} />
224220
</label>
225221
<input
222+
checked=""
226223
data-spec="input"
227-
defaultChecked=""
228224
style={
229225
Object {
230226
"width": 0,
@@ -258,8 +254,8 @@ exports[`test should add classes with classes prop 2`] = `
258254
style={Object {}} />
259255
</label>
260256
<input
257+
checked=""
261258
data-spec="input"
262-
defaultChecked=""
263259
style={
264260
Object {
265261
"width": 0,
@@ -293,9 +289,9 @@ exports[`test should add inputClasses with inputClasses prop 1`] = `
293289
style={Object {}} />
294290
</label>
295291
<input
292+
checked=""
296293
className="test1 harambe"
297294
data-spec="input"
298-
defaultChecked=""
299295
style={
300296
Object {
301297
"width": 0,
@@ -329,8 +325,8 @@ exports[`test should disable input with disabled prop 1`] = `
329325
style={Object {}} />
330326
</label>
331327
<input
328+
checked=""
332329
data-spec="input"
333-
defaultChecked=""
334330
disabled={true}
335331
style={
336332
Object {
@@ -366,8 +362,8 @@ exports[`test should display children 1`] = `
366362
</small>
367363
</label>
368364
<input
365+
checked=""
369366
data-spec="input"
370-
defaultChecked=""
371367
style={
372368
Object {
373369
"width": 0,
@@ -401,8 +397,8 @@ exports[`test should hide label with hideLabel prop 1`] = `
401397
style={Object {}} />
402398
</label>
403399
<input
400+
checked=""
404401
data-spec="input"
405-
defaultChecked=""
406402
style={
407403
Object {
408404
"width": 0,
@@ -431,8 +427,8 @@ exports[`test should render with minimal props 1`] = `
431427
style={Object {}} />
432428
</label>
433429
<input
430+
checked=""
434431
data-spec="input"
435-
defaultChecked=""
436432
style={
437433
Object {
438434
"width": 0,
@@ -466,8 +462,8 @@ exports[`test should respond to onclick prop 1`] = `
466462
style={Object {}} />
467463
</label>
468464
<input
465+
checked=""
469466
data-spec="input"
470-
defaultChecked=""
471467
onClick={[Function]}
472468
style={
473469
Object {

imports/components/giving/add-to-cart/Layout.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ILayout = {
1919
authorized: boolean,
2020
canCheckout: boolean,
2121
setCanCheckout: Function,
22+
bindSubComponentReset: Function,
2223
}
2324

2425
export default ({
@@ -32,6 +33,7 @@ export default ({
3233
authorized,
3334
canCheckout,
3435
setCanCheckout,
36+
bindSubComponentReset,
3537
}: ILayout) => (
3638
<div className="push-top@handheld soft-half-top@lap-and-up">
3739
<Forms.Form
@@ -74,7 +76,11 @@ export default ({
7476
</div>
7577

7678
{/* Schedule */}
77-
<ScheduleLayout setCanCheckout={setCanCheckout} authorized={authorized} />
79+
<ScheduleLayout
80+
setCanCheckout={setCanCheckout}
81+
authorized={authorized}
82+
bindSubComponentReset={bindSubComponentReset}
83+
/>
7884

7985
{/* Total information */}
8086
<h3 className="display-inline-block text-dark-primary push-half-bottom push-half-right" style={{ verticalAlign: "middle" }}>

imports/components/giving/add-to-cart/Schedule/__tests__/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const generateComponent = (additionalProps = {}) => {
3131
authorized: true,
3232
saveSchedule: jest.fn(),
3333
setCanCheckout: jest.fn(),
34+
bindSubComponentReset: jest.fn(),
3435
};
3536
return (
3637
<Schedule {...defaultProps} {...additionalProps} />
@@ -284,4 +285,19 @@ describe("Class", () => {
284285
});
285286
});
286287
});
288+
289+
describe("reset-binding", () => {
290+
it("resets when called", () => {
291+
let reset = null;
292+
const bindSubComponentReset = (fn) => reset = jest.fn(fn);
293+
294+
const wrapper = mount(generateComponent({ bindSubComponentReset }));
295+
const originalState = wrapper.state();
296+
wrapper.setState({ checked: true });
297+
298+
reset();
299+
expect(wrapper.state()).toEqual(originalState);
300+
});
301+
});
302+
287303
});

imports/components/giving/add-to-cart/Schedule/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type IScheduleProps = {
1212
saveSchedule: Function,
1313
setCanCheckout: Function,
1414
preCheck: boolean,
15+
bindSubComponentReset: Function,
1516
};
1617

1718
type IScheduleState = {
@@ -50,6 +51,7 @@ export class Schedule extends Component {
5051

5152
componentWillMount() {
5253
if (this.props.preCheck) this.setState({ checked: true });
54+
this.props.bindSubComponentReset(this.toggleSchedule);
5355
}
5456

5557
componentDidMount() {

imports/components/giving/add-to-cart/__tests__/__snapshots__/index.js.snap

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ exports[`CartContainer > Lifecycle functions accepts query string prefills the f
258258
},
259259
]
260260
}
261+
bindSubComponentReset={[Function]}
261262
canCheckout={true}
262263
changeAmount={[Function]}
263264
changeFund={[Function]}
@@ -322,6 +323,7 @@ exports[`CartContainer > Lifecycle functions accepts query string prefills the f
322323
<div
323324
className="display-block one-whole soft-bottom" />
324325
<mockConstructor
326+
bindSubComponentReset={[Function]}
325327
setCanCheckout={[Function]}>
326328
<div />
327329
</mockConstructor>
@@ -402,7 +404,7 @@ Object {
402404

403405
exports[`CartContainer > Lifecycle functions should clear transactions on success 1`] = `
404406
Object {
405-
"canCheckout": true,
407+
"canCheckout": false,
406408
"subfunds": Array [
407409
Object {
408410
"accounts": Array [
@@ -422,7 +424,7 @@ Object {
422424

423425
exports[`CartContainer > Lifecycle functions should clear transactions on when amount is set to 0 1`] = `
424426
Object {
425-
"canCheckout": true,
427+
"canCheckout": false,
426428
"subfunds": Array [
427429
Object {
428430
"accounts": Array [
@@ -498,6 +500,7 @@ exports[`test should properly render with multiple accounts 1`] = `
498500
},
499501
]
500502
}
503+
bindSubComponentReset={[Function]}
501504
canCheckout={false}
502505
changeAmount={[Function]}
503506
changeFund={[Function]}
@@ -604,6 +607,7 @@ exports[`test should properly render with multiple accounts 1`] = `
604607
</SmallButton>
605608
</div>
606609
<mockConstructor
610+
bindSubComponentReset={[Function]}
607611
setCanCheckout={[Function]}>
608612
<div />
609613
</mockConstructor>
@@ -672,6 +676,7 @@ exports[`test should render with minimal props 1`] = `
672676
},
673677
]
674678
}
679+
bindSubComponentReset={[Function]}
675680
canCheckout={false}
676681
changeAmount={[Function]}
677682
changeFund={[Function]}

imports/components/giving/add-to-cart/__tests__/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,17 @@ describe("CartContainer > Lifecycle functions", () => {
116116

117117
it("should clear transactions on success", () => {
118118
const spy = jest.fn();
119+
const resetSpy = jest.fn();
119120
const component = mount(generateComponent({
120121
clearTransactions: spy,
121122
status: "success",
122123
}));
124+
const { bindSubComponentReset } = component.instance();
125+
bindSubComponentReset(resetSpy);
123126
expect(spy).toHaveBeenCalledTimes(1);
124127
component.setProps({ status: "default" });
125128
expect(component.state()).toMatchSnapshot();
129+
expect(resetSpy).toHaveBeenCalledTimes(1);
126130
expect(spy).toHaveBeenCalledTimes(2);
127131
});
128132

imports/components/giving/add-to-cart/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CartContainer extends Component {
6060
accounts: [],
6161
}
6262

63+
resetCbs = []
6364
state = { subfunds: [], canCheckout: true }
6465

6566
componentWillMount() {
@@ -89,11 +90,16 @@ class CartContainer extends Component {
8990
(this.props.total && total === 0)
9091
) {
9192
this.props.clearTransactions();
93+
this.resetCbs.forEach((fn) => fn && fn());
9294
const subfunds = this.calculateDefaultSubfunds();
93-
this.setState({ subfunds });
95+
this.setState({ subfunds, canCheckout: false });
9496
}
9597
}
9698

99+
bindSubComponentReset = (reset: Function) => {
100+
this.resetCbs.push(reset);
101+
}
102+
97103
calculateDefaultSubfunds = (query: Object = {}) => {
98104
// copy array to mutate it
99105
let accounts = [...this.props.accounts];
@@ -321,6 +327,7 @@ class CartContainer extends Component {
321327
authorized={authorized}
322328
canCheckout={this.canCheckout(total)}
323329
setCanCheckout={this.setCanCheckout}
330+
bindSubComponentReset={this.bindSubComponentReset}
324331
/>
325332

326333
);

0 commit comments

Comments
 (0)