React Admin - send custom query during edit/create record - reactjs

I have a simple form to edit camera
export const CameraEdit: FC<ResourceComponentProps> = (props: ResourceComponentProps) => (
<Edit {...props}>
<SimpleForm>
<TextInput source="name" />
<TextInput source="connection" />
<Button variant="contained" label="Preview"></Button>
</SimpleForm>
</Edit>
);
When I click on Preview button I want to
fetch preview from backend (using GraphQL)
download the image
show it on the right side of form
I don't know how to:
issue additional async query (not related to editing data) as in normal react app I had container and component
download the image (result of query)
add additional div to SimpleForm - I've read it can handle only direct inputs

Related

Is there any way to show pre-selected items in the react-admin's ReferenceArrayInput -> SelectArrayInput component?

I have the following code in a react-admin edit form:
<Edit {...props}>
<SimpleForm>
<ReferenceArrayInput
source="clients"
reference="client"
required={true}
validate={[required()]}
>
<SelectArrayInput optionText={ClientOptionRenderer} />
</ReferenceArrayInput>
</SimpleForm>
</Edit>
When clicked the ReferenceArrayInput -> SelectArrayInput component on the ui, it does show all the record-related items (coming from client RestFul API) and it allows their multiple selection.
However, when the form loads in the first place, it does not show pre-selected items (although they are held in the form state, if any). Is there any particular property in the React-Admin SelectArrayInput component to set so it shows pre-selected items by default?

React-admin: ReferenceInput doesn't show loading <LinearProgress> while loading possible options

I have simple react-admin edit form with ReferenceInput. The problem is that ReferenceInput never show progress bar, so it might be confusing for users to see no options while it's loading.
I manually set delay 2 seconds on API calls but ReferenceInput never show loading state.
import React from 'react';
import { ReferenceInput, ReferenceArrayInput, required, SelectArrayInput, SelectInput, SimpleForm, TextInput } from 'react-admin';
const ModelForm = props => (
<SimpleForm {...props}>
<TextInput source="name" validate={[required()]} />
<ReferenceInput reference="goods_types" source="goodsType" validate={[required()]}>
<SelectInput optionText="name" />
</ReferenceInput>
<ReferenceInput reference="manufacturers" source="manufacturer" validate={[required()]}>
<SelectInput optionText="name" />
</ReferenceInput>
<ReferenceArrayInput reference="manufacturers" source="manufacturer" format={v => [v]} validate={[required()]}>
<SelectArrayInput optionText="name" />
</ReferenceArrayInput>
</SimpleForm>
);
export default ModelForm;
Just for test I added ReferenceArrayInput component and it does show loading progress bar.
Is it bug in react-admin? Or am I missing something?
React-admin: 3.11.1
React Admin introduced a timeout with default value of 1000. The loading component only appear after this time. You can override this value by passing a prop to the component.
ref: https://github.com/marmelab/react-admin/blob/ea1f85c73f5be8f12544a0fccf32e4f6bd7452be/packages/ra-ui-materialui/src/layout/LinearProgress.tsx

React-Admin: Clicking MenuItemLink for 2nd time clears form inputs

I have a react-admin based site working nicely.
Though i have an issue with the sidebar menu. If i click one of the items twice it clears all the form inputs. This is a link to an edit form of the resource item (in this case the current user profile):
<MenuItemLink to={"/users/" + user.id} primaryText="Profile" leftIcon={createElement(UserIcon)} onClick={onMenuTap}/>
with resource that looks like:
<Resource name="users" list={UserList} edit={UserEdit} create={UserCreate} icon={UserIcon} />
where UserEdit is
export const UserEdit = (props) => {
<Edit title={<UserEmail />} actions={<UserEditActions />} {...props}>
<SimpleForm validate={validateUserSave}>
<DisabledInput source="email"/>
<TextInput label="First Name" source="firstName" />
<TextInput label="Last Name" source="lastName" />
...
on first click all the inputs are populated from my REST api, but on 2nd tap (menu item selected) - all the form values are cleared...
Any ideas?
It is indeed a bug, I opened an issue on React Admin:
[#2291] Double-click on a Icon from the Menu reset the edition form
[#2322] Fix resetform when navigating to same page
A fix will be published with react-admin#2.3.2!
Thanks for reporting the issue.

admin-on-rest: customizing Edit component

I need to customize Edit component in two ways:
Add custom button, but not to the upper panel (with 'List' and
'Refresh' buttons), but at the bottom of the component (next to the default 'Save'
button).
Turn off redirect on default 'Save' button click (to make it just
save and stay on page).
How do I achieve this?
I came accross this unanswered question. Since I just did something like this very recently myself I will share how I did it here. I am using admin on rest 1.4.0 btw.
So, on your <Edit> component, add this toolbar={<MyCustomToolbar />}. Then create a custom toolbar with your buttons in it. On the button you can use redirect to redirect to another page.
Code example:
import { SaveButton, Toolbar, TextInput, Edit, SimpleForm } from 'admin-on-rest';
const MyToolbar = props =>
<Toolbar {...props} >
<SaveButton label="Save & to dashboard" redirect="/" />
.. more buttons here..
</Toolbar>;
export const EditForm = (props) => (
<Edit title="Edit" {...props}>
<SimpleForm toolbar={<MyToolbar />}>
<TextInput source="company_website" type="url" />
<TextInput source="address_street" />
<TextInput source="address_zip" />
<TextInput source="address_unitnr" />
<TextInput source="address_city" />
</SimpleForm>
</Edit>
);
Hope this helps!

How to get file upload progress?

I am new to AOR and redux-sagas and I am currently working on a list view drag-and-drop and upload capability. I want the user to be able to drag and drop files and see current upload progress and status from the List view.
The prototype of the component looks like this:
// ...
<Dropzone onDrop={this.onDrop} onDragLeave={this.onDragLeave} onDragEnter={this.onDragEnter} ref={(node) => {this.dropzoneRef = node;}} disableClick disablePreview style={{}}>
// Dropped files will appear here, with the upload progress
<Card style={{marginBottom: "1em"}}>
<CardTitle>Picture.jpg</CardTitle>
<CardText>
// The progress bar will show the upload progress
<LinearProgress value={50} mode="determinate" />
</CardText>
</Card>
<List {...props} actions={<FileActions onUploadClick={() => {this.dropzoneRef.open()}} />}>
<Datagrid>
<TextField source="name" />
<DateField source="createdAt" />
<EditButton />
</Datagrid>
</List>
</Dropzone>
the onDrop callback receives the dropped files, and that's where the upload needs to be handled. That's where I am bit puzzled about how I should handle the problem.
I understand I have to decorate my REST client in order to be able to upload my files. But how can I plug some functions in order to send the upload progress of each files to my component?

Resources