Skip to content

Commit 8639231

Browse files
feat: Adds create inventory
1 parent 1b75c8e commit 8639231

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { authProvider, httpClient } from './Auth';
55
import RentalList from './components/RentalList';
66
import CustomerList from './components/CustomerList';
77
import RentalEdit from './components/RentalEdit';
8+
import RentalCreate from './components/RentalCreate';
89

910
function App() {
1011
return (
@@ -13,9 +14,8 @@ function App() {
1314
authProvider={authProvider}
1415
>
1516
<Resource name='films'/>
16-
1717
<Resource name='customers' list={CustomerList} />
18-
<Resource name='rentals' list={RentalList} edit={RentalEdit}/>
18+
<Resource name='rentals' create={RentalCreate} list={RentalList} edit={RentalEdit}/>
1919

2020
</Admin>
2121
);

src/components/RentalCreate.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React, { useEffect, useState } from 'react';
2+
import {
3+
Create,
4+
SimpleForm,
5+
DateTimeInput,
6+
SelectInput,
7+
useNotify,
8+
useRefresh,
9+
useRedirect,
10+
useQuery,
11+
} from 'react-admin';
12+
13+
const RentalCreate = (props) => {
14+
const notify = useNotify();
15+
const refresh = useRefresh();
16+
const redirect = useRedirect();
17+
18+
const onSuccess = () => {
19+
notify(`New Rental created `);
20+
redirect('/rentals');
21+
refresh();
22+
};
23+
24+
const [customers, setCustomers] = useState([]);
25+
const { data: customer } = useQuery({
26+
type: 'getList',
27+
resource: 'customers',
28+
payload: {
29+
pagination: { page: 1, perPage: 600 },
30+
sort: { field: 'email', order: 'ASC' },
31+
filter: {},
32+
},
33+
});
34+
useEffect(() => {
35+
if (customer)
36+
setCustomers(customer.map((d) => ({ id: d.id, name: d.email })));
37+
}, [customer]);
38+
39+
const [films, setFilms] = useState([]);
40+
const { data: film } = useQuery({
41+
type: 'getList',
42+
resource: 'films',
43+
payload: {
44+
pagination: { page: 1, perPage: 1000 },
45+
sort: { field: 'title', order: 'ASC' },
46+
filter: {},
47+
},
48+
});
49+
useEffect(() => {
50+
if (film) setFilms(film.map((d) => ({ id: d.id, name: d.title })));
51+
}, [film]);
52+
53+
return (
54+
<Create {...props} title='Create new Rental' onSuccess={onSuccess}>
55+
<SimpleForm>
56+
<SelectInput source='customer_id' choices={customers} />
57+
58+
<SelectInput source='film_id' choices={films} />
59+
<SelectInput
60+
source='status'
61+
choices={[
62+
{ id: 'new', name: 'new' },
63+
{ id: 'paid', name: 'paid' },
64+
{ id: 'outbound', name: 'outbound' },
65+
{ id: 'returned', name: 'returned' },
66+
{ id: 'canceled', name: 'canceled' },
67+
{ id: 'lost', name: 'lost' },
68+
]}
69+
/>
70+
71+
<DateTimeInput source='rental_date' />
72+
73+
<DateTimeInput source='return_date' />
74+
</SimpleForm>
75+
</Create>
76+
);
77+
};
78+
79+
export default RentalCreate;

src/components/RentalEdit.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {
33
Edit,
44
SimpleForm,
55
TextInput,
6-
DateInput,
6+
DateTimeInput,
77
SelectInput,
88
} from 'react-admin';
99

1010
const RentalEdit = (props) => (
1111
<Edit {...props} title='Edit of Rentals'>
12-
<SimpleForm rowClick='edit'>
12+
<SimpleForm>
1313
<TextInput disabled source='id' />
14-
<DateInput disabled source='rental_date' />
14+
<DateTimeInput disabled source='rental_date' />
1515

1616
<SelectInput
1717
source='status'
@@ -24,7 +24,7 @@ const RentalEdit = (props) => (
2424
{ id: 'lost', name: 'lost' },
2525
]}
2626
/>
27-
<DateInput min source='return_date' />
27+
<DateTimeInput source='return_date' />
2828
</SimpleForm>
2929
</Edit>
3030
);

0 commit comments

Comments
 (0)