Skip to content

Commit

Permalink
Convert Aside to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
josephktcheung committed Mar 12, 2020
1 parent 5097061 commit 51ad46c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/demo/src/reviews/StarRatingField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const useStyles = makeStyles({
});

interface OwnProps {
size: 'large' | 'small';
size?: 'large' | 'small';
}

const StarRatingField: FC<FieldProps & OwnProps> = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { FC } from 'react';
import {
NumberField,
TextField,
Expand Down Expand Up @@ -27,6 +27,8 @@ import { makeStyles } from '@material-ui/core/styles';

import ProductReferenceField from '../products/ProductReferenceField';
import StarRatingField from '../reviews/StarRatingField';
import { Record, RecordMap, Identifier } from 'ra-core';
import { Order as OrderRecord, Review as ReviewRecord } from '../types';

const useAsideStyles = makeStyles(theme => ({
root: {
Expand All @@ -37,7 +39,12 @@ const useAsideStyles = makeStyles(theme => ({
},
}));

const Aside = ({ record, basePath }) => {
interface AsideProps {
record?: Record;
basePath?: string;
}

const Aside: FC<AsideProps> = ({ record, basePath }) => {
const classes = useAsideStyles();
return (
<div className={classes.root}>
Expand All @@ -51,7 +58,11 @@ Aside.propTypes = {
basePath: PropTypes.string,
};

const EventList = ({ record, basePath }) => {
interface EventListProps {
record?: Record;
basePath?: string;
}
const EventList: FC<EventListProps> = ({ record, basePath }) => {
const translate = useTranslate();
const { data: orders, ids: orderIds } = useGetList(
'commands',
Expand Down Expand Up @@ -170,13 +181,13 @@ const EventList = ({ record, basePath }) => {
{events.map(event =>
event.type === 'order' ? (
<Order
record={event.data}
record={event.data as OrderRecord}
key={`event_${event.data.id}`}
basePath={basePath}
/>
) : (
<Review
record={event.data}
record={event.data as ReviewRecord}
key={`review_${event.data.id}`}
basePath={basePath}
/>
Expand All @@ -186,23 +197,38 @@ const EventList = ({ record, basePath }) => {
);
};

const mixOrdersAndReviews = (orders, orderIds, reviews, reviewIds) => {
const eventsFromOrders = orderIds.map(id => ({
const mixOrdersAndReviews = (
orders: RecordMap<OrderRecord>,
orderIds: Identifier[],
reviews: RecordMap<ReviewRecord>,
reviewIds: Identifier[]
) => {
const eventsFromOrders: {
type: string;
date: any;
data: Record;
}[] = orderIds.map(id => ({
type: 'order',
date: orders[id].date,
data: orders[id],
}));
const eventsFromReviews = reviewIds.map(id => ({
const eventsFromReviews: {
type: string;
date: any;
data: Record;
}[] = reviewIds.map(id => ({
type: 'review',
date: reviews[id].date,
data: reviews[id],
}));
const events = eventsFromOrders.concat(eventsFromReviews);
events.sort((e1, e2) => new Date(e1.date) - new Date(e2.date));
events.sort(
(e1, e2) => new Date(e1.date).getTime() - new Date(e2.date).getTime()
);
return events;
};

const useEventStyles = makeStyles({
const useEventStyles = makeStyles(theme => ({
card: {
margin: '0 0 1em 1em',
},
Expand All @@ -215,12 +241,17 @@ const useEventStyles = makeStyles({
'-webkit-box-orient': 'vertical',
overflow: 'hidden',
},
});
}));

const Order = ({ record, basePath }) => {
interface OrderProps {
record?: OrderRecord;
basePath?: string;
}

const Order: FC<OrderProps> = ({ record, basePath }) => {
const translate = useTranslate();
const classes = useEventStyles();
return (
return record ? (
<Card className={classes.card}>
<CardHeader
className={classes.cardHeader}
Expand All @@ -229,7 +260,6 @@ const Order = ({ record, basePath }) => {
aria-label={translate('resources.commands.name', {
smart_count: 1,
})}
className={classes.avatar}
>
<order.icon />
</Avatar>
Expand Down Expand Up @@ -267,13 +297,18 @@ const Order = ({ record, basePath }) => {
}
/>
</Card>
);
) : null;
};

const Review = ({ record, basePath }) => {
interface ReviewProps {
record?: ReviewRecord;
basePath?: string;
}

const Review: FC<ReviewProps> = ({ record, basePath }) => {
const translate = useTranslate();
const classes = useEventStyles();
return (
return record ? (
<Card className={classes.card}>
<CardHeader
className={classes.cardHeader}
Expand All @@ -282,7 +317,6 @@ const Review = ({ record, basePath }) => {
aria-label={translate('resources.reviews.name', {
smart_count: 1,
})}
className={classes.avatar}
>
<review.icon />
</Avatar>
Expand All @@ -309,10 +343,15 @@ const Review = ({ record, basePath }) => {
}
/>
</Card>
);
) : null;
};

const EditButton = ({ record, basePath }) => {
interface EditButtonProps {
record?: Record;
basePath?: string;
}

const EditButton: FC<EditButtonProps> = ({ record, basePath }) => {
const translate = useTranslate();
return (
<Tooltip title={translate('ra.action.edit')}>
Expand Down

0 comments on commit 51ad46c

Please sign in to comment.