Skip to content

Commit

Permalink
feat(core): 更新ts类型
Browse files Browse the repository at this point in the history
  • Loading branch information
stbui committed Feb 29, 2020
1 parent 006bb27 commit 3d734f2
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 86 deletions.
20 changes: 17 additions & 3 deletions packages/core/src/dataProvider/Muation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* https://github.com/stbui/prophet
*/

import { FunctionComponent } from 'react';
import useMutation from './useMutation';

/*
Expand Down Expand Up @@ -31,7 +32,7 @@ const UserProfile = ({ record }) => (
);
*/

interface ChildrenFunParms {
interface ChildrenFuncParams {
data?: any;
total?: number;
loading?: boolean;
Expand All @@ -40,14 +41,27 @@ interface ChildrenFunParms {
}

export interface Props {
children(props: ChildrenFunParms): JSX.Element;
children: any;
type: string;
resource: string;
payload?: any;
options?: any;
}

export const Mutation = ({ children, type, resource, payload, options }) =>
/**
*
* @param {Function} children
* @param {string} type
* @param {string} resource
* @param {Object} payload
* @param {Object} options
* @param {string} options.action
* @param {Function} options.onSuccess
* @param {Function} options.onFailure
*
* @example
*/
export const Mutation: FunctionComponent<Props> = ({ children, type, resource, payload, options }) =>
children(useMutation({ type, resource, payload }, options));

export default Mutation;
49 changes: 31 additions & 18 deletions packages/core/src/dataProvider/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,38 @@
* https://github.com/stbui/prophet
*/

import { FunctionComponent } from 'react';
import useQuery from './useQuery';

interface ChildrenFuncParams {
data?: any;
total?: number;
loading?: boolean;
loaded?: boolean;
error?: any;
}

export interface Props {
children(props: ChildrenFuncParams): JSX.Element;
type: string;
resource: string;
payload?: any;
options?: any;
}

/**
*
* @param {Function} children
* @param {string} type
* @param {string} resource
* @param {Object} payload
* @param {Object} options
* @param {string} options.action
* @param {Function} options.onSuccess
* @param {Function} options.onFailure
*
* @example
*/
/*
import { Query } from 'prophet-core';
Expand Down Expand Up @@ -45,24 +75,7 @@ const UserList = () => (
</Query>
);
*/

interface ChildrenFunParms {
data?: any;
total?: number;
loading?: boolean;
loaded?: boolean;
error?: any;
}

export interface Props {
children(props: ChildrenFunParms): JSX.Element;
type: string;
resource: string;
payload?: any;
options?: any;
}

const Query = ({ children, type, resource, payload, options }) =>
const Query: FunctionComponent<Props> = ({ children, type, resource, payload, options }) =>
children(useQuery({ type, resource, payload }, options));

export default Query;
15 changes: 14 additions & 1 deletion packages/core/src/dataProvider/useCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@ const UserProfile = ({ record }) => {
};
*/

const useCreate = (resource: string, data: any = {}, options?: any) =>
/**
*
* @param {string} resource
* @param {Object} data
* @param {Object} options
* @param {string} options.action
* @param {Function} options.onSuccess
* @param {Function} options.onFailure
*
* @returns [create, { data, error, loading, loaded }]
*
* @example
*/
const useCreate = (resource: string, data: object = {}, options?: object) =>
useMuation(
{ type: CREATE, resource, payload: { data } },
{ ...options, action: CRUD_CREATE }
Expand Down
18 changes: 16 additions & 2 deletions packages/core/src/dataProvider/useDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,25 @@ const UserProfile = ({ record }) => {
};
*/

/**
*
* @param {string} resource
* @param {string} id
* @param {Object} previousData
* @param {Object} options
* @param {string} options.action
* @param {Function} options.onSuccess
* @param {Function} options.onFailure
*
* @returns
*
* @example
*/
export const useDelete = (
resource: string,
id: string | number,
previousData: any = {},
options?: any
previousData: object = {},
options?: object
) =>
useMuation(
{ type: DELETE, resource, payload: { id, previousData } },
Expand Down
35 changes: 26 additions & 9 deletions packages/core/src/dataProvider/useEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,39 @@ const UserEdit = () => {

export interface EditProps {
resource: string;
basePath: string;
payload: any;
id: string | number;
payload?: object;
}

export const useEdit = (props: EditProps) => {
const { resource, basePath, id, payload } = props;
export interface UseEditValue {
record: any;
id: string | number;
loading: any;
isSaving: any;
save: any
error: any,
updateError: any
}

/**
*
* @param {string} resource
* @param {string} id
* @param {Object} payload
*
* @returns
*
* @example
*/
export const useEdit = ({ resource, id, payload }: EditProps): UseEditValue => {

const { data: record, loading, error } = useQuery({
type: GET_ONE,
resource,
payload: { id, ...payload },
});

const [update, { loading: isSaving, saveError }] = useMutation(
const [update, { loading: isSaving, error: updateError }] = useMutation(
{
type: UPDATE,
resource,
Expand All @@ -52,20 +70,19 @@ export const useEdit = (props: EditProps) => {

const save = useCallback(
(data: any, { onSuccess, onFailure, refresh }: any = {}) =>
// @ts-ignore
update({ data }, { onSuccess, onFailure, refresh }),
[resource, basePath, update]
[resource, update]
);

return {
resource,
basePath,
record,
id,
loading,
isSaving,
save,
error,
saveError
updateError
};
};

Expand Down
34 changes: 29 additions & 5 deletions packages/core/src/dataProvider/useGetList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { useSelector, shallowEqual } from 'react-redux';
import useQueryWithStore from './useQueryWithStore';
import { GET_LIST, CRUD_GET_LIST } from '../actions';

import { Pagination, Sort } from '../type'
/*
import { useGetList } from '@stbui/prophet-core';
Expand All @@ -32,13 +32,37 @@ const UserList = () => {
};
*/

export interface UseQueryValue {
data?: any;
total?: number;
error?: any;
loading?: boolean;
loaded?: boolean;
ids?: any;
}

/**
*
* @param {string} resource
* @param {Object} pagination
* @param {Object} filter
* @param {Object} sort
* @param {Object} options
* @param {string} options.action
* @param {Function} options.onSuccess
* @param {Function} options.onFailure
*
* @returns
*
* @example
*/
export const useGetList = (
resource: string,
pagination: Object,
pagination: Pagination,
filter: Object,
sort: Object,
options?: any
) => {
sort: Sort,
options?: object
): UseQueryValue => {
const { data: ids, total, loading, loaded, error } = useQueryWithStore(
{
type: GET_LIST,
Expand Down
39 changes: 38 additions & 1 deletion packages/core/src/dataProvider/useGetOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,47 @@ cconst UserProfile = record => {
};
*/

export interface UseQueryValue {
data?: any;
error?: any;
loading?: boolean;
loaded?: boolean;
}

/**
*
* @param {string} resource
* @param {string} id
* @param {Object} options
* @param {string} options.action
* @param {Function} options.onSuccess
* @param {Function} options.onFailure
*
* @returns
*
* @example
*
import { useGetOne } from '@stbui/prophet-core';
cconst UserProfile = record => {
const { data, loading, error } = useGetOne('users', record.id);
if (loading) {
return 'loading';
}
if (error) {
return error.message;
}
return <div>{data.username}</div>;
};
*/
export const useGetOne = (
resource: string,
id: string | number,
options?: any
) =>
): UseQueryValue =>
useQueryWithStore(
{
type: GET_ONE,
Expand All @@ -42,4 +78,5 @@ export const useGetOne = (
? state.resources[resource].data[id]
: null
);

export default useGetOne;
Loading

0 comments on commit 3d734f2

Please sign in to comment.