-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9319 from marmelab/tip-create-relatde-resource
Add ability to pass url state in `<CreateButton>`
- Loading branch information
Showing
13 changed files
with
196 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from 'react'; | ||
import { CreateButton, useRecordContext } from 'react-admin'; | ||
|
||
const CreateRelatedReviewButton = () => { | ||
const record = useRecordContext(); | ||
|
||
return ( | ||
<CreateButton | ||
resource="reviews" | ||
state={{ record: { product_id: record.id } }} | ||
/> | ||
); | ||
}; | ||
|
||
export default CreateRelatedReviewButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as React from 'react'; | ||
import { | ||
SimpleForm, | ||
Create, | ||
ReferenceInput, | ||
TextInput, | ||
DateInput, | ||
AutocompleteInput, | ||
required, | ||
useNotify, | ||
useRedirect, | ||
getRecordFromLocation, | ||
} from 'react-admin'; | ||
import { useLocation } from 'react-router'; | ||
|
||
import StarRatingInput from './StarRatingInput'; | ||
|
||
const ReviewCreate = () => { | ||
const notify = useNotify(); | ||
const redirect = useRedirect(); | ||
const location = useLocation(); | ||
|
||
const onSuccess = (_: any) => { | ||
const record = getRecordFromLocation(location); | ||
notify('ra.notification.created'); | ||
if (record && record.product_id) { | ||
redirect(`/products/${record.product_id}/reviews`); | ||
} else { | ||
redirect(`/reviews`); | ||
} | ||
}; | ||
|
||
return ( | ||
<Create mutationOptions={{ onSuccess }}> | ||
<SimpleForm defaultValues={{ status: 'pending' }}> | ||
<ReferenceInput source="customer_id" reference="customers"> | ||
<AutocompleteInput validate={required()} /> | ||
</ReferenceInput> | ||
<ReferenceInput source="product_id" reference="products"> | ||
<AutocompleteInput | ||
optionText="reference" | ||
validate={required()} | ||
/> | ||
</ReferenceInput> | ||
<DateInput | ||
source="date" | ||
defaultValue={new Date()} | ||
validate={required()} | ||
/> | ||
<StarRatingInput source="rating" defaultValue={2} /> | ||
<TextInput | ||
source="comment" | ||
multiline | ||
fullWidth | ||
resettable | ||
validate={required()} | ||
/> | ||
</SimpleForm> | ||
</Create> | ||
); | ||
}; | ||
|
||
export default ReviewCreate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Box, Rating, Typography, styled } from '@mui/material'; | ||
import Icon from '@mui/icons-material/Stars'; | ||
import { InputProps, useInput, useTranslate } from 'react-admin'; | ||
|
||
const StarRatingInput = (props: InputProps) => { | ||
const { name = 'resources.reviews.fields.rating' } = props; | ||
const { field } = useInput(props); | ||
const translate = useTranslate(); | ||
|
||
return ( | ||
<Box display="flex" flexDirection="column" marginBottom="1rem"> | ||
<Typography>{translate(name)}</Typography> | ||
<StyledRating | ||
{...field} | ||
icon={<Icon />} | ||
onChange={(event, value) => field.onChange(value)} | ||
/> | ||
</Box> | ||
); | ||
}; | ||
|
||
const StyledRating = styled(Rating)({ | ||
'& .MuiRating-iconFilled': { | ||
color: '#000', | ||
}, | ||
'& .MuiRating-iconHover': { | ||
color: '#000', | ||
}, | ||
}); | ||
|
||
export default StarRatingInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import ReviewIcon from '@mui/icons-material/Comment'; | ||
import ReviewList from './ReviewList'; | ||
import ReviewCreate from './ReviewCreate'; | ||
|
||
export default { | ||
icon: ReviewIcon, | ||
list: ReviewList, | ||
create: ReviewCreate, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.