forked from houdiniproject/houdini
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow custom amounts with highlight icons in widget custom_amounts param
- Loading branch information
Showing
6 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
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
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
42 changes: 42 additions & 0 deletions
42
client/js/nonprofits/donate/parseFields/customAmounts/JsonStringParser.spec.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// License: LGPL-3.0-or-later | ||
import JsonStringParser from './JsonStringParser'; | ||
|
||
describe('JsonStringParser', () => { | ||
|
||
describe.each([ | ||
["with bracket", "["], | ||
["with brace", "[{]"], | ||
["with no amount given", "[{name:'name', label: 'LABEL'}]"], | ||
])("when invalid %s", (_n, input)=> { | ||
const parser = new JsonStringParser(input) | ||
it('has correct result', () => { | ||
expect(parser.results).toStrictEqual([]); | ||
}); | ||
|
||
it('has error', () => { | ||
expect(parser.errors).not.toBeEmpty(); | ||
}); | ||
|
||
it('is marked not valid', () => { | ||
expect(parser.isValid).toBe(false) | ||
}); | ||
}); | ||
|
||
describe.each([ | ||
['when an empty array', '[]', []], | ||
])("when valid %s", (_name, input, result) => { | ||
const parser = new JsonStringParser(input); | ||
|
||
it('has no errors', () => { | ||
expect(parser.errors).toBeEmpty(); | ||
}); | ||
|
||
it('has is marked valid', () => { | ||
expect(parser.isValid).toStrictEqual(true); | ||
}); | ||
|
||
it('matches expected result', () => { | ||
expect(parser.results).toStrictEqual(result); | ||
}); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
client/js/nonprofits/donate/parseFields/customAmounts/JsonStringParser.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// License: LGPL-3.0-or-later | ||
import has from 'lodash/has'; | ||
import { parse } from 'json5'; | ||
import { Amount, CustomAmount } from '../customAmounts'; | ||
|
||
function isCustomAmountObject(item: unknown): item is CustomAmount { | ||
return typeof item == 'object' && has(item, 'amount'); | ||
} | ||
export default class JsonStringParser { | ||
public errors: SyntaxError[] = []; | ||
public readonly results: Amount[] = []; | ||
constructor(public readonly fieldsString: string) { | ||
this._parse(); | ||
} | ||
|
||
get isValid(): boolean { | ||
return this.errors.length == 0; | ||
} | ||
|
||
private _parse = (): void => { | ||
try { | ||
const result = parse(this.fieldsString); | ||
if (result instanceof Array) { | ||
result.forEach((i) => { | ||
if (isCustomAmountObject(i)) { | ||
this.results.push({ ...i }); | ||
} else if (typeof i == 'number') { | ||
this.results.push(i); | ||
} else { | ||
this.errors.push(new SyntaxError(JSON.stringify(i) + ' is not a valid custom amount')); | ||
} | ||
}); | ||
} else { | ||
this.errors.push(new SyntaxError('Input did not parse to an array')); | ||
} | ||
} catch (e: any) { | ||
this.errors.push(e); | ||
} | ||
}; | ||
} |
22 changes: 22 additions & 0 deletions
22
client/js/nonprofits/donate/parseFields/customAmounts/index.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// License: LGPL-3.0-or-later | ||
import JsonStringParser from './JsonStringParser'; | ||
const R = require('ramda'); | ||
const { getDefaultAmounts } = require('../../custom_amounts'); | ||
import { splitParam } from '..'; | ||
|
||
export type Amount = number | CustomAmount; | ||
|
||
export interface CustomAmount { | ||
amount: NonNullable<number>; | ||
highlight: string; | ||
} | ||
|
||
export default function parseCustomFields(amountsString: string): Amount[] { | ||
const defaultAmts = getDefaultAmounts().join(); | ||
|
||
if (amountsString.includes('{')) { | ||
return new JsonStringParser(`[${amountsString}]`).results; | ||
} else { | ||
return R.compose(R.map(Number), splitParam)(amountsString || defaultAmts); | ||
} | ||
} |
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