Change the images based on the item's categories react native - 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
)
}

Related

How can I checkout directly after I prefill carddetails in razorpay React Native?

The options for checkout are below. Although I can get the items prefilled but still need to click the checkout button.
var options = {
description: 'Credits',
image: 'https://i.imgur.com/3g7nmJC.png',
currency: 'INR',
key: "rzp_test_J"
amount: Response.Amount,
name: 'X',
order_id: ResponseData.GatewayOrderId,
modal: {
handleback: 'false',
},
prefill: {
email: props.CustomerProfile.data.Email,
contact: props.CustomerProfile.data.Phone,
name: props.CustomerProfile.data.FirstName,
method: 'card',
'card[name]': 'Gaurav Kumar',
'card[number]': '4111111111111111',
'card[expiry]': '12/21',
'card[cvv]': '123',
},
};

Hide a Column in fluent-UI Detailslist

How do one hide/prevent a column from rendering in fluent-UI DetailsList component.
Define columns you would like to show, like in const mycolumns.
Notice how in example items have three properties - id, name, surname. However, DetailsList shows only id and name. It is because these columns were defined in const mycolumns.
ListWithHiddenColumns.tsx
import React from 'react';
import { DetailsList } from '#fluentui/react/lib/DetailsList';
export interface IListWithHiddenColumnsProps {}
export const ListWithHiddenColumns: React.FC<IListWithHiddenColumnsProps> = () => {
return (
<>
<DetailsList items={myitems} columns={mycolumns} />
</>
);
};
export const myitems = [
{ id: 1, name: 'Jane', surname: 'Oak' },
{ id: 1, name: 'John', surname: 'Smith' }
];
export const mycolumns = [
{
key: 'id',
name: 'Id',
fieldName: 'id',
minWidth: 50,
maxWidth: 50,
isResizable: false
},
{
key: 'name',
name: 'Name',
fieldName: 'name',
minWidth: 100,
maxWidth: 200,
isResizable: true
}
];

Why get "Mismatch between request payer account number and session" when use paypal smart button with multiple payee

Is it possible to pay to multiple payee with using login informations or smart button?
I tried the following
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
reference_id: 'reference1',
amount: {
value: 1,
currency: 'USD',
breakdown: {
item_total: {
currency_code: 'USD',
value: 1
}
}
},
description: 'description',
payee: {
email_address: 'example2#email.com'
},
items: [{
name: 'item1',
unit_amount: {
currency_code: 'USD',
value: 1
},
quantity: 1
}]
},
{
reference_id: 'referenceid1',
amount: {
value: 1,
currency: 'USD',
breakdown: {
item_total: {
currency_code: 'USD',
value: 1
}
}
},
description: 'The payment transaction description.',
payee: {
email_address: 'example2#email.com'
},
items: [{
name: 'item2',
unit_amount: {
currency_code: 'USD',
value: 1
},
quantity: 1
}]
}]
})
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!')
})
}
}).render('#paypal-button-container')
<script src="https://www.paypal.com/sdk/js?client-id=xxx"></script>
and get error
{
description: "Mismatch between request payer account number and session"
field: "/purchase_units/#reference_id==reference1"
issue: "PAYER_INVALID_FOR_PAYMENT"
}
with single purchase unit work correctly
Not possible, only a single purchase_unit can be approved in the Checkout flow

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>

React native: How to make horizontal scroll but data is divided to 4 rows

I need to scroll my items horizontally but my data is divided to 4 rows every row contain 4 items. and I can scroll horizontally so the next 16 item come to the screen.
when using numColumns={4} it works if
horizontal={false}
but with
horizontal={true}
I can't specify numColumns attr.
Should I use SectionList instead of FlatList ?
and how it could be implemented ?
let items = [
{ id: 1, title: '1', price: '20' },
{ id: 2, title: '2', price: '16' },
{ id: 3, title: '3', price: '92' },
{ id: 4, title: '4', price: '93' },
{ id: 5, title: '5', price: '20' },
{ id: 6, title: '6', price: '16' },
{ id: 7, title: '7', price: '92' },
{ id: 8, title: '8', price: '93' },
{ id: 9, title: 'Grilled Steak', price: '20' },
{ id: 10, title: 'Pappas', price: '16' },
{ id: 11, title: 'Ciccione', price: '92' },
{ id: 12, title: 'Gyros Melt', price: '93' },
{ id: 13, title: 'Grilled Steak', price: '20' },
{ id: 14, title: 'Pappas', price: '16' },
{ id: 15, title: 'Ciccione', price: '92' },
{ id: 16, title: 'Gyros Melt', price: '93' },
];
<FlatList
keyExtractor={item => item.id}
data={items}
horizontal
numColumns={4}
renderItem={({ item }) => <Text>{item.title}</Text>}
/>
I have the same issue with same scenario. making group data is quite a bad practice in case you have long array. so I play with styles of flatlist and I was successful. following is my code if you can take advantage out of it:
<ScrollView showsHorizontalScrollIndicator={false} horizontal={true} style={styles.flatListContainer}>
<FlatList
showsHorizontalScrollIndicator={false}
data={item.items}
numColumns={4}
renderItem={_showData}
columnWrapperStyle={styles.flatListColumnWraper}
keyExtractor={(item) => item.key}
style={styles.flatListDataContainer}
scrollEnabled={false}
contentContainerStyle={
styles.flatListContentContainerStyle
}
/>
</ScrollView>
flatListContainer: {
marginTop: height(2),
height:height(60),
paddingVertical:height(2)
},
flatListDataContainer: {
alignSelf: 'center',
},
flatListContentContainerStyle: {
alignSelf: 'center',
flexDirection: 'row',
justifyContent: 'space-evenly',
},
flatListColumnWraper:{
flexDirection: 'column',
},
Unfortunately FlatList doesn't support a number of columns when it is horizontal.
https://facebook.github.io/react-native/docs/flatlist#numcolumns
Multiple columns can only be rendered with horizontal={false} and will
zig-zag like a flexWrap layout. Items should all be the same height -
masonry layouts are not supported.
However, you could group your data so that for every cell in a horizontal FlatList 4 items are rendered.
let items = [
[ { id: 1, title: '1', price: '20' },
{ id: 2, title: '2', price: '16' },
{ id: 3, title: '3', price: '92' },
{ id: 4, title: '4', price: '93' } ],
[ { id: 5, title: '5', price: '20' },
{ id: 6, title: '6', price: '16' },
{ id: 7, title: '7', price: '92' },
{ id: 8, title: '8', price: '93' } ],
[ { id: 9, title: 'Grilled Steak', price: '20' },
{ id: 10, title: 'Pappas', price: '16' },
{ id: 11, title: 'Ciccione', price: '92' },
{ id: 12, title: 'Gyros Melt', price: '93' } ],
[ { id: 13, title: 'Grilled Steak', price: '20' },
{ id: 14, title: 'Pappas', price: '16' },
{ id: 15, title: 'Ciccione', price: '92' },
{ id: 16, title: 'Gyros Melt', price: '93' } ]
];
Then update your renderItem so that it handles the increased input, something like this.
renderItem = ({item}) => {
return item.map(i => <Text>{i.title}</Text>)
}