More elements migrated to typescript

This commit is contained in:
Alejandro Celaya
2020-08-22 19:03:25 +02:00
parent 62df46d648
commit 2eba607874
9 changed files with 47 additions and 45 deletions

View File

@@ -1,23 +1,23 @@
import React from 'react';
import PropTypes from 'prop-types';
import React, { ChangeEvent, FC } from 'react';
import classNames from 'classnames';
import { v4 as uuid } from 'uuid';
import { identity } from 'ramda';
export const basePropTypes = {
checked: PropTypes.bool.isRequired,
onChange: PropTypes.func.isRequired,
children: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]),
className: PropTypes.string,
};
export interface BooleanControlProps {
checked?: boolean;
onChange?: (checked: boolean, e: ChangeEvent<HTMLInputElement>) => void;
className?: string;
}
const propTypes = {
...basePropTypes,
type: PropTypes.oneOf([ 'switch', 'checkbox' ]).isRequired,
};
interface BooleanControlWithTypeProps extends BooleanControlProps {
type: 'switch' | 'checkbox';
}
const BooleanControl = ({ checked, onChange, className, children, type }) => {
const BooleanControl: FC<BooleanControlWithTypeProps> = (
{ checked = false, onChange = identity, className, children, type },
) => {
const id = uuid();
const onChecked = (e) => onChange(e.target.checked, e);
const onChecked = (e: ChangeEvent<HTMLInputElement>) => onChange(e.target.checked, e);
const typeClasses = {
'custom-switch': type === 'switch',
'custom-checkbox': type === 'checkbox',
@@ -31,6 +31,4 @@ const BooleanControl = ({ checked, onChange, className, children, type }) => {
);
};
BooleanControl.propTypes = propTypes;
export default BooleanControl;

View File

@@ -1,8 +0,0 @@
import React from 'react';
import BooleanControl, { basePropTypes } from './BooleanControl';
const Checkbox = (props) => <BooleanControl type="checkbox" {...props} />;
Checkbox.propTypes = basePropTypes;
export default Checkbox;

6
src/utils/Checkbox.tsx Normal file
View File

@@ -0,0 +1,6 @@
import React, { FC } from 'react';
import BooleanControl, { BooleanControlProps } from './BooleanControl';
const Checkbox: FC<BooleanControlProps> = (props) => <BooleanControl type="checkbox" {...props} />;
export default Checkbox;

View File

@@ -1,8 +0,0 @@
import React from 'react';
import BooleanControl, { basePropTypes } from './BooleanControl';
const ToggleSwitch = (props) => <BooleanControl type="switch" {...props} />;
ToggleSwitch.propTypes = basePropTypes;
export default ToggleSwitch;

View File

@@ -0,0 +1,6 @@
import React, { FC } from 'react';
import BooleanControl, { BooleanControlProps } from './BooleanControl';
const ToggleSwitch: FC<BooleanControlProps> = (props) => <BooleanControl type="switch" {...props} />;
export default ToggleSwitch;

View File

@@ -1,6 +1,6 @@
import bowser from 'bowser';
import { zipObj } from 'ramda';
import { hasValue } from '../utils';
import { Empty, hasValue } from '../utils';
const DEFAULT = 'Others';
const BROWSERS_WHITELIST = [
@@ -17,17 +17,22 @@ const BROWSERS_WHITELIST = [
'WeChat',
];
export const parseUserAgent = (userAgent) => {
interface UserAgent {
browser: string;
os: string;
}
export const parseUserAgent = (userAgent: string | Empty): UserAgent => {
if (!hasValue(userAgent)) {
return { browser: DEFAULT, os: DEFAULT };
}
const { browser: { name: browser }, os: { name: os } } = bowser.parse(userAgent);
return { os: os || DEFAULT, browser: browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT };
return { os: os ?? DEFAULT, browser: browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT };
};
export const extractDomain = (url) => {
export const extractDomain = (url: string | Empty): string => {
if (!hasValue(url)) {
return 'Direct';
}
@@ -37,4 +42,5 @@ export const extractDomain = (url) => {
return domain.split(':')[0];
};
export const fillTheGaps = (stats, labels) => Object.values({ ...zipObj(labels, labels.map(() => 0)), ...stats });
export const fillTheGaps = (stats: Record<string, number>, labels: string[]): number[] =>
Object.values({ ...zipObj(labels, labels.map(() => 0)), ...stats });

View File

@@ -2,7 +2,7 @@ import { isEmpty, isNil, range } from 'ramda';
export type OrderDir = 'ASC' | 'DESC' | undefined;
export const determineOrderDir = (currentField: string, newField: string, currentOrderDir: OrderDir): OrderDir => {
export const determineOrderDir = (currentField: string, newField: string, currentOrderDir?: OrderDir): OrderDir => {
if (currentField !== newField) {
return 'ASC';
}