generated from vtex-apps/react-app-template
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create block product-installments.list
- Loading branch information
Showing
8 changed files
with
171 additions
and
5 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
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,69 @@ | ||
import React, { useContext } from 'react' | ||
import { defineMessages } from 'react-intl' | ||
import { useCssHandles } from 'vtex.css-handles' | ||
import { ProductContext } from 'vtex.product-context' | ||
|
||
import { BasicPriceProps } from './types' | ||
import InstallmentsRenderer, { | ||
Installments, | ||
} from './components/InstallmentsRenderer' | ||
|
||
const CSS_HANDLES = ['installmentsListContainer'] as const | ||
|
||
function InstallmentsList(props: BasicPriceProps) { | ||
const { message, markers } = props | ||
const { selectedItem } = useContext(ProductContext) | ||
const handles = useCssHandles(CSS_HANDLES) | ||
|
||
const commertialOffer = selectedItem?.sellers[0]?.commertialOffer | ||
|
||
if ( | ||
!commertialOffer?.Installments || | ||
commertialOffer.Installments?.length === 0 | ||
) { | ||
return null | ||
} | ||
|
||
const sortedInstallments = commertialOffer.Installments.sort( | ||
(a: Installments, b: Installments) => | ||
a.NumberOfInstallments - b.NumberOfInstallments | ||
) | ||
|
||
return ( | ||
<div | ||
className={`${handles.installmentsListContainer} flex flex-column items-center`} | ||
> | ||
{sortedInstallments.map((inst: Installments, i: number) => { | ||
return ( | ||
<InstallmentsRenderer | ||
key={i} | ||
message={message} | ||
markers={markers} | ||
installments={inst} | ||
/> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
defineMessages({ | ||
title: { | ||
id: 'admin/installments-list.title', | ||
}, | ||
titleMessage: { | ||
id: 'admin/installments.title', | ||
}, | ||
description: { | ||
id: 'admin/installments.description', | ||
}, | ||
default: { | ||
id: 'store/installments.default', | ||
}, | ||
}) | ||
|
||
InstallmentsList.schema = { | ||
title: 'admin/installments-list.title', | ||
} | ||
|
||
export default InstallmentsList |
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,80 @@ | ||
import React from 'react' | ||
import { FormattedNumber } from 'react-intl' | ||
import { useCssHandles } from 'vtex.css-handles' | ||
import { FormattedCurrency } from 'vtex.format-currency' | ||
import { IOMessageWithMarkers } from 'vtex.native-types' | ||
|
||
import { BasicPriceProps } from '../types' | ||
|
||
export interface Installments { | ||
Value: number | ||
InterestRate: number | ||
TotalValuePlusInterestRate: number | ||
NumberOfInstallments: number | ||
} | ||
|
||
const CSS_HANDLES = [ | ||
'installments', | ||
'installmentsNumber', | ||
'installmentValue', | ||
'installmentsTotalValue', | ||
'interestRate', | ||
] as const | ||
|
||
interface Props extends BasicPriceProps { | ||
installments: Installments | ||
} | ||
|
||
function InstallmentsRenderer(props: Props) { | ||
const { message, markers, installments } = props | ||
const handles = useCssHandles(CSS_HANDLES) | ||
|
||
const { | ||
Value, | ||
NumberOfInstallments, | ||
InterestRate, | ||
TotalValuePlusInterestRate, | ||
} = installments | ||
const hasInterest = InterestRate !== 0 | ||
|
||
return ( | ||
<span className={handles.installments}> | ||
<IOMessageWithMarkers | ||
message={message} | ||
markers={markers} | ||
handleBase="installments" | ||
values={{ | ||
installmentsNumber: ( | ||
<span | ||
key="installmentsNumber" | ||
className={handles.installmentsNumber} | ||
> | ||
<FormattedNumber value={NumberOfInstallments} /> | ||
</span> | ||
), | ||
installmentValue: ( | ||
<span key="installmentValue" className={handles.installmentValue}> | ||
<FormattedCurrency value={Value} /> | ||
</span> | ||
), | ||
installmentsTotalValue: ( | ||
<span | ||
key="installmentsTotalValue" | ||
className={handles.installmentsTotalValue} | ||
> | ||
<FormattedCurrency value={TotalValuePlusInterestRate} /> | ||
</span> | ||
), | ||
interestRate: ( | ||
<span key="interestRate" className={handles.interestRate}> | ||
<FormattedNumber value={InterestRate} style="percent" /> | ||
</span> | ||
), | ||
hasInterest, | ||
}} | ||
/> | ||
</span> | ||
) | ||
} | ||
|
||
export default InstallmentsRenderer |
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