Skip to content

Commit d50b30d

Browse files
committed
Demo page organized
1 parent d24dfa1 commit d50b30d

File tree

6 files changed

+143
-283
lines changed

6 files changed

+143
-283
lines changed

demo/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { createStyles, Theme, WithStyles, withStyles } from '@material-ui/core/s
44
import { AppBar, Toolbar, Typography, IconButton, Badge } from '@material-ui/core';
55
import ShoppingCart from '@material-ui/icons/ShoppingCart';
66
import { MyBasketDataProvider } from './MyBasketDataProvider';
7-
import { PusherBasketDataProvider } from './PusherBasketDataProvider';
87
import { Products } from './Products';
98

109
export interface AppProps extends WithStyles<typeof styles> { }
@@ -14,7 +13,7 @@ class App extends React.Component<AppProps, any> {
1413
const { classes } = this.props;
1514

1615
return (
17-
<BasketProvider dataProvider={new PusherBasketDataProvider()}>
16+
<BasketProvider dataProvider={new MyBasketDataProvider()}>
1817
<div className={classes.root}>
1918
<AppBar position="static" elevation={0}>
2019
<Toolbar variant="dense">

demo/MyBasketDataProvider.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { DataProvider, BasketItem } from "../src";
22

33
export class MyBasketDataProvider implements DataProvider {
4+
5+
registerToChanges(callback: (items: BasketItem[]) => void) {
6+
}
7+
8+
products = [
9+
{ id: "1", name: 'Computer', price: 1722.44, quantity: 1 },
10+
{ id: "2", name: 'Phone', price: 522.14, quantity: 1 }
11+
];
12+
413
items = [
514
{ id: "1", name: 'Computer', price: 1722.44, quantity: 1 },
615
{ id: "2", name: 'Phone', price: 522.14, quantity: 2 }
@@ -14,8 +23,34 @@ export class MyBasketDataProvider implements DataProvider {
1423
});
1524
}
1625

26+
onAllItemsDeleted(): Promise<BasketItem[]> {
27+
return new Promise<BasketItem[]>((resolve, reject) => {
28+
setTimeout(() => {
29+
this.items = [];
30+
31+
resolve(this.items)
32+
}, 1000)
33+
});
34+
}
35+
1736
onItemAdded(id: string): Promise<BasketItem[]> {
18-
throw new Error("Method not implemented.");
37+
return new Promise<BasketItem[]>((resolve, reject) => {
38+
setTimeout(() => {
39+
let index = this.items.findIndex(item => item.id === id);
40+
if (index > -1) {
41+
this.items[index].quantity++;
42+
}
43+
else {
44+
const index = this.products.findIndex(item => item.id === id);
45+
if (index > -1) {
46+
const item = {...this.products[index]};
47+
this.items.push(item);
48+
}
49+
}
50+
51+
resolve(this.items)
52+
}, 1000)
53+
});
1954
}
2055

2156
onItemDeleted = (id: string): Promise<BasketItem[]> => {
@@ -30,9 +65,5 @@ export class MyBasketDataProvider implements DataProvider {
3065
resolve(this.items)
3166
}, 1000)
3267
});
33-
34-
35-
36-
3768
}
3869
}

demo/Products.tsx

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
import * as React from 'react';
22
import { CircularProgress, Card, CardContent, CardHeader, CardActions, Button, Typography } from '@material-ui/core';
3-
import Axios from 'axios';
43
import { BasketItem, withBasketData, BasketData } from '../src';
54

6-
class ProductsInner extends React.Component<{basketData: BasketData}, any> {
5+
class ProductsInner extends React.Component<{ basketData: BasketData }, any> {
76

87
constructor(props: any) {
98
super(props);
109

1110
this.state = {
1211
isLoading: false,
13-
items: []
12+
items: [
13+
{ id: "1", name: 'Computer', price: 1722.44, quantity: 1 },
14+
{ id: "2", name: 'Phone', price: 522.14, quantity: 2 }
15+
]
1416
}
1517
}
1618

17-
componentDidMount() {
18-
this.setState({ isLoading: true }, () => {
19-
Axios.get<BasketItem>("http://localhost:8080/products")
20-
.then(response => {
21-
this.setState({
22-
items: response.data,
23-
isLoading: false
24-
})
25-
})
26-
.catch(reason => {
27-
this.setState({
28-
error: reason,
29-
isLoading: false
30-
});
31-
});
32-
})
33-
}
34-
3519
public render() {
3620
if (this.state.isLoading) {
3721
return (
@@ -50,9 +34,9 @@ class ProductsInner extends React.Component<{basketData: BasketData}, any> {
5034
}
5135

5236
return (
53-
<div style={{display: 'flex', margin: -5}}>
37+
<div style={{ display: 'flex', margin: -5 }}>
5438
{this.state.items.map(item => (
55-
<Card style={{flex: 1, margin: 5}} elevation={0}>
39+
<Card style={{ flex: 1, margin: 5 }} elevation={0}>
5640
<CardContent>
5741
<Typography variant="h6">
5842
{item.name}
@@ -61,16 +45,16 @@ class ProductsInner extends React.Component<{basketData: BasketData}, any> {
6145
Price ${item.price}
6246
</Typography>
6347
</CardContent>
64-
<CardActions style={{justifyContent: 'flex-end'}}>
65-
<Button variant="outlined" color="primary" style={{textTransform: 'none'}}
48+
<CardActions style={{ justifyContent: 'flex-end' }}>
49+
<Button variant="outlined" color="primary" style={{ textTransform: 'none' }}
6650
onClick={() => {
6751
this.props.basketData.onItemAdded(item.id);
6852
}}
6953
>
7054
Add to Basket
7155
</Button>
7256
</CardActions>
73-
</Card>
57+
</Card>
7458
))}
7559
</div>
7660
)

demo/PusherBasketDataProvider.ts

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)