Skip to content

Commit

Permalink
Merge pull request #74 from jim-deriv/Jim/80860/fix-parent-2
Browse files Browse the repository at this point in the history
Jim/80860/fix parent 2
  • Loading branch information
jim-deriv committed Nov 14, 2022
2 parents 85900a4 + e28c509 commit 14af159
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions packages/components/stories/page-error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const DummyComponent = props => (
| messages | {array} | null | Array of strings as error text |
| image_url | {string} | null | This image will be shown beside the error text |
| classNameImage | {string} | null | |
| redirect_label | {string} | null | Redirect button text |
| redirect_url | {string} | null | Where you want to land user after clicking on redirect button |
| redirect_labels | {array} | null | Redirect button text |
| redirect_urls | {array} | null | Where you want to land user after clicking on redirect button |
| setError | {function} | null | Function to remove error on the parent component |
| should_clear_error_on_click | {boolean} | null | Set it to true to disable error on parent component on redirect. `setError` must be defined |

Expand All @@ -36,9 +36,9 @@ const DummyComponent = props => (
messages={props.messages}
classNameImage='may-class'
image_url={props.image_url}
redirect_label={props.redirect_label}
redirect_labels={props.redirect_labels}
buttonOnClick={props.buttonOnClick}
redirect_url={props.redirect_url}
redirect_urls={props.redirect_urls}
/>
);
```
5 changes: 3 additions & 2 deletions packages/shared/src/utils/helpers/__tests__/durations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import * as Duration from '../duration.js';
import * as Duration from '../duration';
import moment from 'moment';

describe('buildDurationConfig', () => {
Expand Down Expand Up @@ -29,6 +29,7 @@ describe('buildDurationConfig', () => {
max: 31536000,
},
},
forward: {},
},
units_display: {
spot: [
Expand All @@ -41,7 +42,7 @@ describe('buildDurationConfig', () => {
};

it('Returns correct value when durations is not passed', () => {
expect(Duration.buildDurationConfig(contract)).to.eql(durations);
expect(Duration.buildDurationConfig(contract, durations)).to.eql(durations);
});

it('Returns correct value when durations passed', () => {
Expand Down
33 changes: 16 additions & 17 deletions packages/shared/src/utils/helpers/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type TUnit = {
value: string;
};

type TDurations = {
export type TDurations = {
min_max: {
spot?: Partial<Record<'tick' | 'intraday' | 'daily', TMaxMin>>;
forward?: Record<'intraday', TMaxMin>;
spot: Partial<Record<'tick' | 'intraday' | 'daily', TMaxMin>>;
forward: Partial<Record<'intraday', TMaxMin>>;
};
units_display: Partial<Record<'spot' | 'forward', TUnit[]>>;
};
Expand All @@ -34,7 +34,7 @@ type TDurationMinMax = {
};

const getDurationMaps = () => ({
t: { display: localize('Ticks'), order: 1, to_second: 0 },
t: { display: localize('Ticks'), order: 1, to_second: null },
s: { display: localize('Seconds'), order: 2, to_second: 1 },
m: { display: localize('Minutes'), order: 3, to_second: 60 },
h: { display: localize('Hours'), order: 4, to_second: 60 * 60 },
Expand All @@ -43,25 +43,25 @@ const getDurationMaps = () => ({

export const buildDurationConfig = (
contract: TContract,
durations: TDurations = { min_max: {}, units_display: {} }
durations: TDurations = { min_max: { spot: {}, forward: {} }, units_display: {} }
) => {
type TDurationMaps = keyof typeof duration_maps;
let duration_min_max = durations.min_max[contract.start_type as keyof typeof durations.min_max];
let duration_units = durations.units_display[contract.start_type as keyof typeof durations.units_display];

duration_min_max = duration_min_max || {};
duration_units = duration_units || [];
durations.units_display[contract.start_type as keyof typeof durations.units_display] =
durations.units_display[contract.start_type as keyof typeof durations.units_display] || [];

const duration_min_max = durations.min_max[contract.start_type as keyof typeof durations.min_max];
const obj_min = getDurationFromString(contract.min_contract_duration);
const obj_max = getDurationFromString(contract.max_contract_duration);

duration_min_max[contract.expiry_type as keyof typeof duration_min_max] = {
durations.min_max[contract.start_type as keyof typeof durations.min_max][
contract.expiry_type as keyof typeof duration_min_max
] = {
min: convertDurationUnit(obj_min.duration, obj_min.unit, 's') || 0,
max: convertDurationUnit(obj_max.duration, obj_max.unit, 's') || 0,
};

const arr_units: string[] = [];
duration_units.forEach(obj => {
durations?.units_display?.[contract.start_type as keyof typeof durations.units_display]?.forEach?.(obj => {
arr_units.push(obj.value);
});

Expand All @@ -84,10 +84,9 @@ export const buildDurationConfig = (
});
}

duration_units = arr_units
durations.units_display[contract.start_type as keyof typeof durations.units_display] = arr_units
.sort((a, b) => (duration_maps[a as TDurationMaps].order > duration_maps[b as TDurationMaps].order ? 1 : -1))
.reduce((o, c) => [...o, { text: duration_maps[c as TDurationMaps].display, value: c }], [] as TUnit[]);

return durations;
};

Expand All @@ -98,13 +97,13 @@ export const convertDurationUnit = (value: number, from_unit: string, to_unit: s

const duration_maps = getDurationMaps();

if (from_unit === to_unit || !('to_second' in duration_maps[from_unit as keyof typeof duration_maps])) {
if (from_unit === to_unit || duration_maps[from_unit as keyof typeof duration_maps].to_second === null) {
return value;
}

return (
(value * duration_maps[from_unit as keyof typeof duration_maps].to_second) /
duration_maps[to_unit as keyof typeof duration_maps].to_second
(value * (duration_maps[from_unit as keyof typeof duration_maps]?.to_second ?? 1)) /
(duration_maps[to_unit as keyof typeof duration_maps]?.to_second ?? 1)
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ export type TOptions = {
regex?: RegExp;
};

const validRequired = (value?: string /* , options, field */) => {
const validRequired = (value?: string | number /* , options, field */) => {
if (value === undefined || value === null) {
return false;
}

const str = value.replace(/\s/g, '');
const str = value.toString().replace(/\s/g, '');
return str.length > 0;
};
export const validAddress = (value: string) => !/[`~!$%^&*_=+[}{\]\\"?><|]+/.test(value);
export const validPostCode = (value: string) => value === '' || /^[A-Za-z0-9][A-Za-z0-9\s-]*$/.test(value);
export const validTaxID = (value: string) => /(?!^$|\s+)[A-Za-z0-9./\s-]$/.test(value);
export const validPhone = (value: string) => /^\+?([0-9-]+\s)*[0-9-]+$/.test(value);
export const validLetterSymbol = (value: string) => /^[A-Za-z]+([a-zA-Z\.' -])*[a-zA-Z\.' -]+$/.test(value);
export const validLetterSymbol = (value: string) => /^[A-Za-z]+([a-zA-Z.' -])*[a-zA-Z.' -]+$/.test(value);
export const validLength = (value = '', options: TOptions) =>
(options.min ? value.length >= options.min : true) && (options.max ? value.length <= options.max : true);
export const validPassword = (value: string) => /(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]+/.test(value);
Expand Down

0 comments on commit 14af159

Please sign in to comment.