Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert demo example into typescript #4516

Merged
merged 28 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cc348ed
Convert dataProvider, fakeServer and i18n to ts
josephktcheung Mar 11, 2020
5097061
Convert routes and themeReducer to ts
josephktcheung Mar 11, 2020
ef57191
Convert Aside to ts
josephktcheung Mar 12, 2020
7f9c9d5
Convert MobileGrid to ts
josephktcheung Mar 12, 2020
f38f10b
Convert NbItemField to ts
josephktcheung Mar 13, 2020
d423f98
Properly type graphql.ts
josephktcheung Mar 13, 2020
91a0f69
Properly type rest.ts
josephktcheung Mar 13, 2020
e4233c6
Fix graphql.ts
josephktcheung Mar 13, 2020
84fccde
Update graphql.ts
josephktcheung Mar 13, 2020
cff1d07
Properly type themeReducer
josephktcheung Mar 13, 2020
b65a591
Add fakerest module
josephktcheung Mar 13, 2020
55b7bf8
Clean up
josephktcheung Mar 13, 2020
03eb9fc
Update parseResponse type
josephktcheung Mar 13, 2020
15e8d9e
Update AsideEvent
josephktcheung Mar 13, 2020
dd8bb2e
Update AsideEvent date
josephktcheung Mar 13, 2020
973703b
Clean up
josephktcheung Mar 13, 2020
9dc9b25
Merge branch 'next' into feature/demo-ts
josephktcheung Mar 13, 2020
7e04491
Convert OrderEdit to ts
josephktcheung Mar 15, 2020
2c854f1
Convert OrderList to ts
josephktcheung Mar 15, 2020
842a224
Update OrderList type
josephktcheung Mar 16, 2020
90ba92c
Merge branch 'next' into feature/demo-ts
josephktcheung Apr 28, 2020
927fdd9
Fix typo
josephktcheung May 5, 2020
e2476b4
Remove unnecessary declarations
josephktcheung May 5, 2020
5952521
Add OTHER_ACTION to themeReducer
josephktcheung May 5, 2020
e473be1
Update themeReducer and handleChange types
josephktcheung May 5, 2020
ddb35b6
Fix ListComponentProps
josephktcheung May 5, 2020
0d7083a
Move hooks back to TabbedDatagrid
josephktcheung May 5, 2020
56df98d
Remove unused type
josephktcheung May 5, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"not op_mini all"
],
"devDependencies": {
"@types/fetch-mock": "^7.3.2",
"@types/classnames": "^2.2.9",
"@types/jest": "^24.0.23",
"@types/node": "^12.12.14",
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const App = () => {

const fetchDataProvider = async () => {
restoreFetch = await fakeServerFactory(
process.env.REACT_APP_DATA_PROVIDER
process.env.REACT_APP_DATA_PROVIDER || ''
);
const dataProviderInstance = await dataProviderFactory(
process.env.REACT_APP_DATA_PROVIDER
process.env.REACT_APP_DATA_PROVIDER || ''
);
setDataProvider(
// GOTCHA: dataProviderInstance can be a function
Expand Down
1 change: 1 addition & 0 deletions examples/demo/src/data-generator-retail.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'data-generator-retail';
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { ApolloQueryResult } from 'apollo-client';
import buildApolloClient, {
buildQuery as buildQueryFactory,
} from 'ra-data-graphql-simple';
import { DELETE } from 'ra-core';
import { DELETE, LegacyDataProvider } from 'ra-core';
import gql from 'graphql-tag';
const getGqlResource = resource => {
import {
IntrospectionField,
IntrospectionSchema,
IntrospectionType,
} from 'graphql';

const getGqlResource = (resource: string) => {
switch (resource) {
case 'customers':
return 'Customer';
Expand All @@ -28,7 +35,20 @@ const getGqlResource = resource => {
}
};

const customBuildQuery = introspectionResults => {
type IntrospectionResource = IntrospectionType & {
[key: string]: IntrospectionField;
};

interface IntrospectionResults {
types: IntrospectionType[];
queries: IntrospectionField[];
resources: IntrospectionResource[];
schema: IntrospectionSchema;
}

const customBuildQuery = (
introspectionResults: IntrospectionResults
): LegacyDataProvider => {
const buildQuery = buildQueryFactory(introspectionResults);

return (type, resource, params) => {
Expand All @@ -38,7 +58,7 @@ const customBuildQuery = introspectionResults => {
remove${resource}(id: $id)
}`,
variables: { id: params.id },
parseResponse: ({ data }) => {
parseResponse: ({ data }: ApolloQueryResult<any>) => {
if (data[`remove${resource}`]) {
return { data: { id: params.id } };
}
Expand All @@ -59,11 +79,17 @@ export default () => {
},
introspection: {
operationNames: {
[DELETE]: resource => `remove${resource.name}`,
[DELETE]: (resource: IntrospectionType) =>
`remove${resource.name}`,
},
},
buildQuery: customBuildQuery,
}).then(dataProvider => (type, resource, params) =>
dataProvider(type, getGqlResource(resource), params)
}).then(
(dataProvider: LegacyDataProvider) => (
...rest: Parameters<LegacyDataProvider>
) => {
const [type, resource, params] = rest;
return dataProvider(type, getGqlResource(resource), params);
}
);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default type => {
export default (type: string) => {
switch (type) {
case 'graphql':
return import('./graphql').then(factory => factory.default());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ const delayedDataProvider = new Proxy(restProvider, {
get: (target, name, self) =>
name === 'then' // as we await for the dataProvider, JS calls then on it. We must trap that call or else the dataProvider will be called with the then method
? self
: (resource, params) =>
: (resource: string, params: any) =>
new Promise(resolve =>
setTimeout(
() => resolve(restProvider[name](resource, params)),
() =>
resolve(
restProvider[name as string](resource, params)
),
500
)
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default type => {
export default (type: string) => {
switch (type) {
case 'graphql':
return import('./graphql').then(factory => factory.default());
Expand Down
1 change: 1 addition & 0 deletions examples/demo/src/fakerest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'fakerest';
1 change: 1 addition & 0 deletions examples/demo/src/json-graphql-server.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'json-graphql-server';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// in src/comments.js
import React from 'react';
import React, { FC } from 'react';
import Card from '@material-ui/core/Card';
import CardHeader from '@material-ui/core/CardHeader';
import CardContent from '@material-ui/core/CardContent';
Expand All @@ -14,6 +14,7 @@ import {
} from 'react-admin';

import CustomerReferenceField from '../visitors/CustomerReferenceField';
import { RecordMap, Identifier, Record } from 'ra-core';

const useListStyles = makeStyles(theme => ({
card: {
Expand All @@ -24,22 +25,33 @@ const useListStyles = makeStyles(theme => ({
},
cardTitleContent: {
display: 'flex',
flexDirection: 'rows',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
cardContent: theme.typography.body1,
cardContentRow: {
display: 'flex',
flexDirection: 'rows',
flexDirection: 'row',
alignItems: 'center',
margin: '0.5rem 0',
},
}));

const MobileGrid = ({ ids, data, basePath }) => {
interface MobileGridProps {
ids?: Identifier[];
data?: RecordMap<Record>;
basePath?: string;
}

const MobileGrid: FC<MobileGridProps> = ({ ids, data, basePath }) => {
const translate = useTranslate();
const classes = useListStyles();

if (!ids || !data || !basePath) {
return null;
}

return (
<div style={{ margin: '1em' }}>
{ids.map(id => (
Expand Down Expand Up @@ -88,7 +100,6 @@ const MobileGrid = ({ ids, data, basePath }) => {
record={data[id]}
source="total"
options={{ style: 'currency', currency: 'USD' }}
className={classes.total}
/>
</span>
<span className={classes.cardContentRow}>
Expand Down
13 changes: 0 additions & 13 deletions examples/demo/src/orders/NbItemsField.js

This file was deleted.

20 changes: 20 additions & 0 deletions examples/demo/src/orders/NbItemsField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { FC } from 'react';
import { FunctionField } from 'react-admin';
import { Order, FieldProps } from '../types';

const render = (record: Order) => record.basket.length;

interface NbItemsFieldProps extends FieldProps {
textAlign?: string;
}

const NbItemsField: FC<NbItemsFieldProps> = props => (
<FunctionField {...props} render={render} />
);

NbItemsField.defaultProps = {
label: 'Nb Items',
textAlign: 'right',
};

export default NbItemsField;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { FC } from 'react';
import {
AutocompleteInput,
BooleanInput,
Expand All @@ -10,25 +10,30 @@ import {
useTranslate,
} from 'react-admin';
import { makeStyles } from '@material-ui/core/styles';
import { Order, Customer, EditComponentProps } from '../types';

import Basket from './Basket';

const OrderTitle = ({ record }) => {
interface OrderTitleProps {
record?: Order;
}

const OrderTitle: FC<OrderTitleProps> = ({ record }) => {
const translate = useTranslate();
return (
return record ? (
<span>
{translate('resources.commands.title', {
reference: record.reference,
})}
</span>
);
) : null;
};

const useEditStyles = makeStyles({
root: { alignItems: 'flex-start' },
});

const OrderEdit = props => {
const OrderEdit: FC<EditComponentProps> = props => {
const classes = useEditStyles();
return (
<Edit
Expand All @@ -41,7 +46,7 @@ const OrderEdit = props => {
<DateInput source="date" />
<ReferenceInput source="customer_id" reference="customers">
<AutocompleteInput
optionText={choice =>
optionText={(choice: Customer) =>
`${choice.first_name} ${choice.last_name}`
}
/>
Expand Down
Loading