Skip to content

Commit

Permalink
refactor(hook): migrate to TypeScript (#1872)
Browse files Browse the repository at this point in the history
* refactor(hook): migrate to TypeScript

* chore: changeset

* fix(use-toggle-state): do not assign value as type
  • Loading branch information
adnasa authored Apr 28, 2021
1 parent 7d56aa9 commit 043e9d2
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 109 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-poems-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/hooks': patch
---

Migrate `hooks` to TypeScript

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useState, useCallback } from 'react';
import isNil from 'lodash/isNil';

type TSortDefinition = {
key: string;
order: 'desc' | 'asc';
};

type TDataTableSortingState = {
value: TSortDefinition;
onChange: (
key: TSortDefinition['key'],
order: TSortDefinition['order']
) => void;
};

const defaultValues: TSortDefinition = { key: 'createdAt', order: 'desc' };
const applyIf = (
values: Partial<TSortDefinition>,
key: 'order' | 'key'
): Partial<TDataTableSortingState> =>
!isNil(values[key]) ? { [key]: values[key] } : {};

const useDataTableSortingState = (
initialValues: Partial<TSortDefinition> = {}
): TDataTableSortingState => {
const mergedValues: TSortDefinition = {
...defaultValues,
...applyIf(initialValues, 'key'),
...applyIf(initialValues, 'order'),
};
const [sortDefinition, setSortDefinition] = useState<TSortDefinition>(
mergedValues
);
const onTableSortingChange = useCallback<TDataTableSortingState['onChange']>(
(key: TSortDefinition['key'], order: TSortDefinition['order']): void => {
setSortDefinition({
key,
order,
});
},
[]
);

return {
value: sortDefinition,
onChange: onTableSortingChange,
};
};

export default useDataTableSortingState;
File renamed without changes.
14 changes: 0 additions & 14 deletions packages/hooks/src/use-field-id/use-field-id.js

This file was deleted.

19 changes: 19 additions & 0 deletions packages/hooks/src/use-field-id/use-field-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { getFieldId } from '@commercetools-uikit/utils';

type CreateIdFn = () => string;

const useFieldId = (id: string, createIdFn: CreateIdFn): string => {
const [internalId, setId] = React.useState<string>(id);

React.useEffect(() => {
const props = { id };
const state = { id: internalId };

setId(getFieldId(props, state, createIdFn));
}, [id, internalId, setId, createIdFn]);

return internalId;
};

export default useFieldId;
50 changes: 0 additions & 50 deletions packages/hooks/src/use-pagination-state/use-pagination-state.js

This file was deleted.

73 changes: 73 additions & 0 deletions packages/hooks/src/use-pagination-state/use-pagination-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { useState, useCallback } from 'react';
import isNil from 'lodash/isNil';

type TPaginationDefinition = {
page: number;
perPage: number;
};
type TState = {
value: number;
onChange: (value: number) => void;
};
type TPaginationState = {
[P in keyof TPaginationDefinition]: TState;
};

const applyIf = (
values: Partial<TPaginationDefinition>,
key: 'page' | 'perPage'
): Partial<TPaginationDefinition> =>
!isNil(values[key]) ? { [key]: values[key] } : {};

const defaultValues: TPaginationDefinition = {
page: 1,
perPage: 20,
};

const usePaginationState = (
initialValues: Partial<TPaginationDefinition> = {}
): TPaginationState => {
const mergedValues: TPaginationDefinition = {
...defaultValues,
...applyIf(initialValues, 'page'),
...applyIf(initialValues, 'perPage'),
};

const [page, setPage] = useState<TPaginationDefinition['page']>(
mergedValues.page
);
const [perPage, setPerPage] = useState<TPaginationDefinition['perPage']>(
mergedValues.perPage
);

const onPageChange = useCallback<TState['onChange']>(
(nextPage) => {
setPage(nextPage);
},
[setPage]
);

const onPerPageChange = useCallback<TState['onChange']>(
(nextPerPage) => {
// side-effect:
// GIVEN client updates `perPage`,
// THEN we reset `page` (discards initialValues.page)
setPage(1);
setPerPage(nextPerPage);
},
[setPerPage, setPage]
);

return {
page: {
value: page,
onChange: onPageChange,
},
perPage: {
value: perPage,
onChange: onPerPageChange,
},
};
};

export default usePaginationState;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

const usePrevious = (value) => {
const ref = React.useRef();
const usePrevious = <Ref>(value: Ref) => {
const ref = React.useRef<Ref>();
React.useEffect(() => {
ref.current = value;
}, [value]);
Expand Down
18 changes: 0 additions & 18 deletions packages/hooks/src/use-toggle-state/use-toggle-state.js

This file was deleted.

18 changes: 18 additions & 0 deletions packages/hooks/src/use-toggle-state/use-toggle-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useCallback, useState } from 'react';

const useToggleState = (defaultValue?: boolean) => {
const initialValue = typeof defaultValue === 'boolean' ? defaultValue : true;

const [isToggled, setIsToggled] = useState(initialValue);
const toggle = useCallback<(forceIsToggled?: boolean) => void>(
(forceIsToggled?: boolean) => {
setIsToggled(
typeof forceIsToggled === 'boolean' ? forceIsToggled : !isToggled
);
},
[isToggled, setIsToggled]
);
return [isToggled, toggle];
};

export default useToggleState;

1 comment on commit 043e9d2

@vercel
Copy link

@vercel vercel bot commented on 043e9d2 Apr 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.