Skip to content

Commit

Permalink
Inspect 1-click-bom on staging
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulrhmnGhanem committed Oct 29, 2021
1 parent 9d6d90e commit 93b5be0
Showing 1 changed file with 69 additions and 67 deletions.
136 changes: 69 additions & 67 deletions frontend/src/components/Board/BuyParts/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import { array, bool, func, number, string } from 'prop-types'
import OneClickBom from '1-click-bom-minimal'
import { Header, Icon, Segment, Input, Button } from 'semantic-ui-react'
Expand All @@ -9,17 +9,74 @@ import DirectStores from './DirectStores'
import styles from './index.module.scss'

const BuyParts = ({ projectFullName, lines, parts }) => {
const [extensionPresence, setExtensionPresence] = useState('unknown')
// it's needed to fix the extension integration.
// eslint-disable-next-line no-unused-vars
const [buyParts, setBuyParts] = useState(null)
// const [extensionPresence, setExtensionPresence] = useState('unknown')
const extensionPresence = useRef('unknown')
const [buyParts, setBuyParts] = useState(() => install1ClickBOM)
const [buyMultiplier, setBuyMultiplier] = useState(1)
const [mult, setMult] = useState(1)
const [buyAddPercent, setBuyAddPercent] = useState(0)
const [adding, setAdding] = useState({})

const retailerList = OneClickBom.getRetailers()
const retailerButtons = retailerList
useEffect(() => {
const multi = buyMultiplier
if (Number.isNaN(multi) || multi < 1) {
setMult(1)
}
const percent = buyAddPercent
if (Number.isNaN(percent) || percent < 1) {
setMult(0)
}
setMult(multi + multi * (percent / 100))
}, [buyMultiplier, buyAddPercent])

useEffect(() => {
const handleExtensionMessages = event => {
if (event.source !== window) {
return
}

if (event.data.from === 'extension') {
extensionPresence.current = 'present'
switch (event.data.message) {
case 'register':
setBuyParts(() => retailer => {
window.plausible('Buy Parts', {
props: {
project: projectFullName,
vendor: retailer,
multiplier: mult,
},
})
console.log('%cpostMessage', 'background: #222; color: red')
window.postMessage(
{
from: 'page',
message: 'quickAddToCart',
value: {
retailer,
multiplier: mult,
},
},
'*',
)
})
break
case 'updateAddingState':
console.log({ adding: event.data.value })
setAdding(event.data.value)
break
default:
break
}
}
}

window.addEventListener('message', handleExtensionMessages)

return () => window.removeEventListener('message', handleExtensionMessages)
}, [mult, projectFullName])

const retailerButtons = OneClickBom.getRetailers()
.map(name => {
const [numberOfLines, numberOfParts] = lines.reduce(
([numOfLines, numOfParts], line) => {
Expand All @@ -35,8 +92,10 @@ const BuyParts = ({ projectFullName, lines, parts }) => {
<RetailerButton
name={name}
adding={adding[name]}
extensionPresence={name === 'Digikey' ? 'absent' : extensionPresence}
buyParts={() => install1ClickBOM()}
extensionPresence={
name === 'Digikey' ? 'absent' : extensionPresence.current
}
buyParts={buyParts.bind(null, name)}
numberOfParts={numberOfParts}
numberOfLines={numberOfLines}
totalLines={lines.length}
Expand All @@ -48,63 +107,6 @@ const BuyParts = ({ projectFullName, lines, parts }) => {
})
.filter(l => l != null)

useEffect(() => {
// extension communication
window.addEventListener(
'message',
event => {
if (event.source !== window) {
return
}
if (event.data.from === 'extension') {
setExtensionPresence('present')
switch (event.data.message) {
case 'register':
setBuyParts(retailer => {
window.plausible('Buy Parts', {
props: {
project: projectFullName,
vendor: retailer,
multiplier: mult,
},
})
window.postMessage(
{
from: 'page',
message: 'quickAddToCart',
value: {
retailer,
multiplier: mult,
},
},
'*',
)
})
break
case 'updateAddingState':
setAdding(event.data.value)
break
default:
break
}
}
},
false,
)
}, [mult, projectFullName])

useEffect(() => {
const multi = buyMultiplier
if (Number.isNaN(multi) || multi < 1) {
setMult(1)
}
const percent = buyAddPercent
if (Number.isNaN(percent) || percent < 1) {
setMult(0)
}
setMult(multi + multi * (percent / 100))
}, [buyMultiplier, buyAddPercent])

const linesToTsv = () => {
const linesMult = lines.map(line => ({
...line,
Expand All @@ -119,7 +121,7 @@ const BuyParts = ({ projectFullName, lines, parts }) => {
<Icon className={styles.BuyPartsIcon} name="shopping basket" />
Buy Parts
</Header>
<InstallPrompt extensionPresence={extensionPresence} />
<InstallPrompt extensionPresence={extensionPresence.current} />
<AdjustQuantity
buyMultiplier={buyMultiplier}
buyAddPercent={buyAddPercent}
Expand Down

0 comments on commit 93b5be0

Please sign in to comment.