useGetMany - fetches data by sending traction but does not update data field - code copied and used from react-admin document - react-admin

I had used the useGetMany to get topics for mathpapers. I can see based on reference the getMany
Tracation goes and fetch data as shown in console.log response. But the data is not stored in the
" const { data, loaded, error } - as it shows array of same size but undefined objects in console.log prints."
Please suggest what I am missing here.
const Mysubtopics = ({ record }: any) => {
console.log(record);
const subtopicsIds = record.subtopic_id;
console.log('subtopicsID', subtopicsIds);
const { data, loaded, error } = useGetMany('subtopics', subtopicsIds);
//if (error) return <Error />;
if (error) return <span>Error</span>;
if (!loaded) return <span>Loading</span>;
if (!data) return null;
console.log('data', data);
const allsubtopics = data.reduce((acc, subtopic) => {
acc = acc.concat(subtopic.name.en);
return acc;
}, '');
return (
<span>
{allsubtopics}
{record.id}
</span>
);
};
const MathPaperList = (props: any) => {
const classes = useStyles();
const isSmall = useMediaQuery((theme: Theme) =>
theme.breakpoints.down('sm')
);
return (
<List
{...props}
filters={<MathPaperFilter />}
sort={{ field: 'generated_at', order: 'DESC' }}
exporter={exporter}
>
{isSmall ? (
<SimpleList
primaryText={record => record.title}
secondaryText={record => <Mysubtopics record={record} />}
tertiaryText={record =>
new Date(record.generated_at).toLocaleDateString()
}
/>
) : ...........some more for desktop error is in above secondaryText.
I can see the following console.log -
getMany subtopics {"ids":[10,12,14,3,5,8,1,2,4]}
index.js:31
Object
data: Array(9)
0: {id: 1, name: {…}}
1: {id: 2, name: {…}}
2: {id: 3, name: {…}}
3: {id: 4, name: {…}}
4: {id: 5, name: {…}}
5: {id: 8, name: {…}}
6: {id: 10, name: {…}}
7: {id: 12, name: {…}}
8: {id: 14, name: {…}}
length: 9
Object
index.js:30 getMany subtopics {"ids":[10,12,14,3,5,8,1,2,4]}
MathPaperList.tsx:137 {id: 3, title: "3rd test for batch 2:30", batch_id: 1, subtopic_id: Array(3), generated_at: Thu Dec 30 2021 05:30:00 GMT+0530 (India Standard Time), …}
MathPaperList.tsx:139 subtopicsID (3) [10, 12, 14]
MathPaperList.tsx:146 data (3) [undefined, undefined, undefined]
MathPaperList.tsx:137 {id: 2, title: "2nd test for batch 2:30", batch_id: 1, subtopic_id: Array(3), generated_at: Sat Dec 26 2020 05:30:00 GMT+0530 (India Standard Time), …}
MathPaperList.tsx:139 subtopicsID (3) [3, 5, 8]
MathPaperList.tsx:146 data (3) [undefined, undefined, undefined]
__proto__: ...
.........
Here is data.tsx
mathpapers: [
{
id: 1,
title: '1st test for batch 2:30',
batch_id: 1,
subtopic_id: [1, 2, 4],
generated_at: new Date('2020-12-06'),
difficulty: 80,
mark1q: 0,
mark2q: 5,
mark3q: 0,
mark4q: 6,
mark5q: 2,
},
{
id: 2,
title: '2nd test for batch 2:30',
batch_id: 1,
subtopic_id: [3, 5, 8],
generated_at: new Date('2020-12-26'),
difficulty: 40,
mark1q: 3,
mark2q: 0,
mark3q: 4,
mark4q: 3,
mark5q: 2,
},
.....
],
subtopics: [
{
id: 1,
name: { en: 'Sport' },
},
{
id: 2,
name: { en: 'Technology' },
},
.....
]

Here the missing part, which is never documented in the react-admin was you need to have resourses in the App.tsx file to hook up the resource to get data, so need below 2 resource lines with subtopics in the App.tsx -
import subtopics from './subtopics';
.....
const App = () => {
return (
<Admin
title=""
dataProvider={dataProvider}
customReducers={{ theme: themeReducer }}
customRoutes={customRoutes}
authProvider={authProvider}
dashboard={Dashboard}
loginPage={Login}
layout={Layout}
i18nProvider={i18nProvider}
disableTelemetry
>
<Resource name="mathpapers" {...mathpapers} />
<Resource name="subtopics" {...subtopics} />
{/*<Resource
subtopics file is :
export default {
};

Related

Change the images based on the item's categories react native

I'm making a budget tracker app. And I'm implementing the functions display and add transactions for the app. However, I'm struggling to find a way to dynamically set the image URL (the icon) based on the transaction category type.
The app is written in React Native.
For example, I have a list of transactions as below:
[
{
id: 1,
type: 'Expense',
category: 'Food',
description: 'Burger',
amount: 100,
date: '2020-10-10',
createdAt: '2021-01-01',
},
{
id: 2,
type: 'Expense',
category: 'Entertainment',
description: 'Movie',
amount: 200,
date: '2020-10-10',
createdAt: '2021-10-02',
},
{
id: 3,
type: 'Income',
category: 'Salary',
description: 'Salary',
amount: 1000,
date: '2020-10-10',
createdAt: '2021-10-03',
},
{
id: 4,
type: 'Expense',
category: 'Food',
description: 'Burger',
amount: 100,
date: '2020-10-10',
createdAt: '2021-01-01',
},
]
Then I want to display it in a list, and each list item contains the icon representing the category, like this image:
You can just import images and add them as a key like this :
import burgerImage from '../images/burger.jpg';
import movieImage from '../images/movie.jpg';
[
{
id: 1,
type: 'Expense',
category: 'Food',
description: 'Burger',
amount: 100,
date: '2020-10-10',
createdAt: '2021-01-01',
icon: burgerImage,
},
{
id: 2,
type: 'Expense',
category: 'Entertainment',
description: 'Movie',
amount: 200,
date: '2020-10-10',
createdAt: '2021-10-02',
icon: movieImage
},
]
Pass the icon key as a source to Image component like this :
data.map((datum)=> <Image source={datum.icon} />);
I think you should use category field to render icon be like:
const renderIcon = (icon) => {
switch (icon) {
case "Food":
return <Icon icon="food" />
case "Movie":
return <Icon icon="movie" />
// Diff case
default:
return <Icon icon="iconDefault" />
}
And in renderItem of FlatList:
const renderItem = ({ item, index }) => {
return (
// render wrapper
{renderIcon(item.category)}
// render name, time, price of product
)
}

TextInput field not fetching value inside Map function react-native

i want to fetch 'DeliveredQuantity' in Textinput Field, do anyone know solution for this
const [arrayList, setArraylist]= [
{id:0, ExpectedQuantity:10, DeliveredQuantity:7},
{id:1, ExpectedQuantity:19, DeliveredQuantity:9},
{id:2, ExpectedQuantity:11, DeliveredQuantity:11},
{id:3, ExpectedQuantity:45, DeliveredQuantity:30},
]
arrayList.map((items,index)=>{
return
<TextInput value={items.DeliveredQuantity} />
})
Store the array in a state and use it like this
const [arrayList, setArraylist] = React.useState([
{ id: 0, ExpectedQuantity: 10, DeliveredQuantity: 7 },
{ id: 1, ExpectedQuantity: 19, DeliveredQuantity: 9 },
{ id: 2, ExpectedQuantity: 11, DeliveredQuantity: 11 },
{ id: 3, ExpectedQuantity: 45, DeliveredQuantity: 30 },
]);
return (
<View style={styles.container}>
{arrayList.map((items, index) => (
<TextInput value={items.DeliveredQuantity} />
))}
</View>
);
Working Example

Adding list under <SelectInput> field

I need to add a dropdown list under the purpose, I have added under choices inside the selectInput field.
The list is displaying, but when I click on save button its showing purpose must be a string.
<SimpleForm>
<SelectInput label="Purpose" source="purpose" choices={[
{ id: 1, name: "Diagnosis" },
{ id: 2, name: "Surgery" },
{ id: 3, name: "Chemotherapy" },
{ id: 4, name: 'Concurrent (Chemo+Radiation)' },
{ id: 5, name: 'Concurrent Followup(Chemo+Radiation)'},
{ id: 6, name: 'Palliative Treatment' },
{ id: 7, name: 'Post-Chemotherapy Follow up' },
{ id: 8, name: 'Post-Radiation Follow up' },
{ id: 9, name: 'Post-Surgery Follow up' },
{ id: 10, name: 'Radiation' },
{ id: 11, name: 'Side effect/ Supportive Care' }
]} />
<SelectInput label="Hospitals" source="hospital_id" choices={hospitalsArr} required />
<SelectInput label="Patients" source="patient_id" choices={patientsArr} required />
<DateInput source="appointment_date" onChange={this.handleOnchange} />
<SelectInput label="Slots" source="appointment_id" choices={slotsInputArr} required />
</SimpleForm>
</Create>

How to prevent lodash map from creating a nested object?

I want to map an array of objects and add a property to each item. At the moment the original values get nested. How can I change it to get an item with a flattened object?
code example
import _ from 'lodash';
let arr = [{name: "tim"}, {name: "tom"}]
const id = {id: 1}
console.dir(_.map(arr, item => ({...id, item})));
result:
[ { id: 1, item: { name: 'tim' } },
{ id: 1, item: { name: 'tom' } } ]
wished result:
[ { id: 1, name: 'tim' },
{ id: 1, name: 'tom' } ]
Spread the item as well:
let arr = [{name: "tim"}, {name: "tom"}]
const id = {id: 1}
console.dir(_.map(arr, item => ({ ...id, ...item })));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Or use _.merge() with lodash/fp:
const mergeId = id => _.map(_.merge(id))
let arr = [{name: "tim"}, {name: "tom"}]
const id = {id: 1}
result = mergeId(id)(arr)
console.dir(result);
<script src='https://cdn.jsdelivr.net/g/lodash#4(lodash.min.js+lodash.fp.min.js)'></script>

In react native how to append fetch response array object in to render function?

I Need to Display Below the Array Data into Render Function :
I would Know how to loop the data and display it in Render.
[{
profileid: 1,
enabled: 1,
attachment: '',
id: 233,
topicid: 47,
tstamp: 'January, 21 2016 15:06:31 +1100',
body: 'to check orders'
}, {
profileid: 2,
enabled: 1,
attachment: '',
id: 233,
topicid: 47,
tstamp: 'January, 21 2016 15:06:31 +1100',
body: 'to check orders'
} ]
I hope I help you
class Parent extends React.Component{
constructor(props){
super(props);
this.state = {
gists : []
}
}
componentDidMount() {
fetch('http://rest.......')
.then(response => response.json())
.then(gists => this.setState({ gists }))
}
render(){
return (
<ul>
{this.state.gists.map(gist => (
<li key={gist.countryId}>{gist.name}</li>
))}
</ul>
)
}
}
Parent.propTypes = {
data: React.PropTypes.array
}
Here is an example.
import React from 'react'
import { View, Text } from 'react-native'
...
const data = [{
profileid: 1,
enabled: 1,
attachment: '',
id: 233,
topicid: 47,
tstamp: 'January, 21 2016 15:06:31 +1100',
body: 'to check orders'
}, {
profileid: 2,
enabled: 1,
attachment: '',
id: 233,
topicid: 47,
tstamp: 'January, 21 2016 15:06:31 +1100',
body: 'to check orders'
} ]
...
render() {
return (
<View>
{data.map((dataItem) =>
<View key={dataItem.profileid}>
<Text>{dataItem.profileId}</Text>
<Text>{dataItem.enabled}</Text>
<Text>{dataItem.attachment}</Text>
<Text>{dataItem.id}</Text>
<Text>{dataItem.topicid}</Text>
<Text>{dataItem.tstamp}</Text>
<Text>{dataItem.body}</Text>
</View>
)}
</View>
)
}