pricing.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { ScrollView, View } from 'app/design/view'
  2. import Tabs from 'app/ui/molecules/tabs/tabs'
  3. import { Modal } from 'app/design/controls';
  4. import Stripe from 'app/ui/molecules/integrations/stripe';
  5. import { useState, useRef } from 'react';
  6. import { components } from 'app/components/registry';
  7. import { appStatic } from 'app/lib/app-static'
  8. import { useTranslation } from 'react-i18next'
  9. import { fetcher } from 'app/lib/fetcher';
  10. import Redirect from 'app/ui/atoms/redirect';
  11. import { BlockWrapper } from 'app/components/block-wrapper'
  12. export default function ElementPricing({ data, blockWrapperProps }) {
  13. const { t } = useTranslation()
  14. const preparedTabs = [];
  15. const periodMap = new Map();
  16. data.data.forEach(item => {
  17. const periodValue = item?.period?.value;
  18. if (periodValue) {
  19. const key = `${periodValue.unit}`;
  20. periodMap.set(key, periodValue);
  21. }
  22. });
  23. const periodSet = [...periodMap.values()];
  24. const order = ["year", "month"];
  25. const sorted = periodSet.sort((a, b) => {
  26. return order.indexOf(a.unit) - order.indexOf(b.unit);
  27. });
  28. if (sorted.length == 1)
  29. return <BlockWrapper {...blockWrapperProps}><ElementPricingPeriod data={data.data} period={sorted[0]} unit={data.settings.unit} settings={data.settings} /></BlockWrapper>
  30. sorted.forEach(item => {
  31. preparedTabs.push({
  32. key: `tab_${item.unit}`,
  33. title: item.unit ? t('price-period-' + item.unit) : t('price-period-lifetime'),
  34. content: <ElementPricingPeriod data={data.data} period={item} unit={data.settings.unit} settings={data.settings} />
  35. });
  36. });
  37. return (
  38. <BlockWrapper {...blockWrapperProps}>
  39. {appStatic('components_pricing_header_' + data.settings.object, data.settings)}
  40. <Tabs tabs={preparedTabs} activeTab={preparedTabs[0].key} />
  41. {appStatic('components_pricing_footer_' + data.settings.object, data.settings)}
  42. </BlockWrapper>
  43. )
  44. }
  45. function ElementPricingPeriod({ data, period, unit, settings }) {
  46. const redirectRef = useRef();
  47. const filtered = data.filter(item => item?.period?.value.unit === period.unit);
  48. const [showModal, setShowModal] = useState(false);
  49. const fetchData = async (action, ids) => {
  50. let sUrl = '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' + settings.object + '&a=' + action + '&ids[]=' + ids;
  51. if (settings?.query_append)
  52. Object.keys(settings.query_append).forEach((sKey) => {
  53. sUrl += '&' + sKey + '=' + settings.query_append[sKey];
  54. });
  55. return await fetcher(sUrl);
  56. };
  57. const getAction = async (data) => {
  58. if (data.object_name == "stripe_v3") {
  59. setShowModal(data)
  60. }
  61. if (data.type == "callback") {
  62. // `choose` (free level) posts the grid row id; `buy` posts its cart item.
  63. const ids = data.items?.length ? data.items.join('') : data.attr?.bx_grid_action_data;
  64. await fetchData(data.name || 'buy', ids);
  65. if (data.redirect_url) redirectRef.current.redirect(data.redirect_url);
  66. }
  67. }
  68. return (
  69. <View className={`${unit !== 'productlist' ? 'flex-row py-3 ' : ''} flex-wrap `}>
  70. <Redirect ref={redirectRef} />
  71. {filtered.map((item, index) => {
  72. const Price = components['unit']['price'];
  73. const cls = filtered.length > 1 ? 'w-full lg:w-1/' + Math.min(filtered.length, 3) : 'w-full'
  74. return (
  75. <View key={item.id || item.name || item.title || `price-${index}`} className={`${cls} p-2`}><Price data={item} unit={unit} onBuy={getAction} /></View>
  76. )
  77. })}
  78. {showModal && <Modal onVisible={!!showModal} onClose={() => { setShowModal(false) }} transparent={false}>
  79. <ScrollView className="h-[400px]">
  80. <Stripe payment_type={showModal.payment_type} seller_id={showModal.seller_id} items={showModal.items} />
  81. </ScrollView>
  82. </Modal>}
  83. </View>
  84. );
  85. }