|
| 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; |
0 commit comments