|
// |
|
// Copyright © 2022-Present, Punchh, Inc. |
|
// All rights reserved. |
|
// |
|
'use strict'; |
|
|
|
import React from 'react'; |
|
import { BaseComponent, SectionHeader, PunchhText } from 'react-native-punchh-components'; |
|
import { View } from 'react-native'; |
|
|
|
export class CartList extends BaseComponent { |
|
renderTitle(styles, products) { |
|
const { headerStyle } = this.styleSheet(); |
|
const itemCount = products?.length; |
|
const itemText = ' (' + `${itemCount} ${itemCount === 1 ? this.ls('item') : this.ls('items')}` + ')'; |
|
const title = this.ls('basketHeading') + itemText; |
|
const editImage = this.appImages('edit'); |
|
|
|
return ( |
|
<View style={headerStyle}> |
|
<SectionHeader |
|
title={title} |
|
titleTextStyle={styles.sectionTitle} |
|
trailingIcon={editImage.source} |
|
trailingIconStyle={[editImage.style, styles.editIconStyle]} |
|
trailingIconTintColor={styles.imageTintColor} |
|
trailingIconAccessibilityLabel={this.ls('accessibility_editIcon')} |
|
onTrailingIconPress={() => this.props.onEditPress()} |
|
/> |
|
</View> |
|
); |
|
} |
|
|
|
renderList(products) { |
|
const productCount = products?.length; |
|
return products.map((item, index) => this.renderRow(item, index, productCount)); |
|
} |
|
|
|
renderRow(item, index, productCount) { |
|
const styles = this.styleSheet(); |
|
const isLastItem = index == productCount - 1; |
|
let choices = null; |
|
if (item.choices && item.choices?.length) { |
|
choices = item.choices |
|
.filter(i => i.name !== '-') |
|
.map(i => (i.quantity > 1 ? `${i.name.trim()} (${i.quantity})` : i.name.trim())) |
|
.join(', '); |
|
} |
|
|
|
return ( |
|
<View style={styles.infoContainerStyle}> |
|
<View style={styles.titleView}> |
|
<PunchhText |
|
style={styles.quantityLabelStyle} |
|
label={item.quantity} |
|
accessibilityLabel={this.ls('accessibility_itemQuantity').sformat(item.quantity)} |
|
/> |
|
<View style={styles.verticalSepratorStyle} /> |
|
<View style={styles.itemContainerStyle}> |
|
<PunchhText |
|
type="bold" |
|
style={styles.itemStyle} |
|
label={item.name} |
|
accessibilityLabel={this.ls('accessibility_itemName').sformat(item.name)} |
|
/> |
|
{choices && <PunchhText style={styles.subTitleStyle} label={choices} />} |
|
</View> |
|
<PunchhText |
|
type="bold" |
|
style={styles.amountStyle} |
|
label={this.ls('$') + item.totalcost.toFixed(2)} |
|
accessibilityLabel={this.ls('accessibility_itemPrice').sformat(item.totalcost.toFixed(2))} |
|
/> |
|
</View> |
|
{!isLastItem && <View style={styles.sepratorStyle} />} |
|
</View> |
|
); |
|
} |
|
|
|
render() { |
|
const styles = this.styleSheet(); |
|
if (this.props.basket?.products) { |
|
return ( |
|
<View style={styles.container}> |
|
{this.renderTitle(styles, this.props.basket?.products)} |
|
{this.renderList(this.props.basket?.products)} |
|
</View> |
|
); |
|
} |
|
return <View />; |
|
} |
|
|
|
defaultStyles() { |
|
const { Colors, Typography } = this.theme(); |
|
|
|
return { |
|
headerStyle: { |
|
backgroundColor: Colors.SECTION_HEADER_BG |
|
}, |
|
infoContainerStyle: { |
|
flex: 1, |
|
minHeight: 48, |
|
justifyContent: 'center' |
|
}, |
|
titleView: { flexDirection: 'row', padding: Typography.regular }, |
|
itemStyle: { |
|
paddingHorizontal: Typography.xxSmall |
|
}, |
|
itemContainerStyle: { |
|
flexDirection: 'column', |
|
flex: 1 |
|
}, |
|
subTitleStyle: { |
|
marginTop: Typography.xTiny, |
|
paddingHorizontal: Typography.xxSmall |
|
}, |
|
editIconStyle: { |
|
marginRight: Typography.regular, |
|
width: 20, |
|
height: 20 |
|
}, |
|
verticalSepratorStyle: { |
|
width: 0.5, |
|
backgroundColor: Colors.LIGHT_SEPARATOR_COLOR |
|
}, |
|
sepratorStyle: { |
|
backgroundColor: Colors.LIGHT_SEPARATOR_COLOR, |
|
height: 0.5 |
|
}, |
|
quantityLabelStyle: { |
|
marginRight: Typography.xxSmall, |
|
minWidth: 10 |
|
}, |
|
sectionTitle: { |
|
fontSize: Typography.fontSize.small |
|
}, |
|
amountStyle: {}, |
|
imageTintColor: Colors.SECTION_HEADER_IMAGE_TINT_COLOR |
|
}; |
|
} |
|
|
|
customStyles() { |
|
return this.theme().Payment.CartList?.call(this, this.props); |
|
} |
|
} |