Skip to content

Commit

Permalink
Mouser and LCSC
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulrhmnGhanem committed Oct 11, 2024
1 parent dc19190 commit d95fb99
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 57 deletions.
52 changes: 1 addition & 51 deletions frontend/src/components/Board/BuyParts/DirectStores.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import React, { useEffect, useState, useCallback } from 'react'

import DigikeyData from '1-click-bom-minimal/lib/data/digikey.json'
import FarnellData from '1-click-bom-minimal/lib/data/farnell.json'
import countriesData from '1-click-bom-minimal/lib/data/countries.json'

const DirectStores = ({ items, multiplier }: DirectStoresProps) => {
const [countryCode, setCountryCode] = useState('Other')
const [digikeyParts, setDigikeyParts] = useState([])
const [farnellParts, setFarnellParts] = useState([])
const [newarkParts, setNewarkParts] = useState([])

const getParts = useCallback(
retailer =>
Expand All @@ -35,15 +32,11 @@ const DirectStores = ({ items, multiplier }: DirectStoresProps) => {
})
}
setDigikeyParts(getParts('Digikey'))
setFarnellParts(getParts('Farnell'))
setNewarkParts(getParts('Newark'))
return () => {
abortController.abort()
}
}, [getParts])

const tildeDelimiter = part => `${part.sku}~${part.quantity}`

const digikeyPartRenderer = (part, index) => {
index += 1
return (
Expand All @@ -70,50 +63,7 @@ const DirectStores = ({ items, multiplier }: DirectStoresProps) => {
)
}

const farnell = (code, parts) => {
const site = FarnellData.sites[FarnellData.lookup[code]]
const queryString = parts.map(tildeDelimiter).join('~')
return (
<form
key="FarnellForm"
action={`https${site}/jsp/extlink.jsp`}
id="FarnellForm"
method="GET"
target="_blank"
>
<input name="CMP" type="hidden" value="ref_kitnic" />
<input name="action" type="hidden" value="buy" />
<input name="product" type="hidden" value={queryString} />
</form>
)
}

const newark = parts => {
const queryString = parts.map(tildeDelimiter).join('~')
return (
<form
key="NewarkForm"
action="https://www.newark.com/jsp/extlink.jsp"
id="NewarkForm"
method="GET"
target="_blank"
>
<input name="CMP" type="hidden" value="ref_kitnic" />
<input name="action" type="hidden" value="buy" />
<input name="product" type="hidden" value={queryString} />
</form>
)
}

return (
<span>
{[
digikey(countryCode, digikeyParts),
farnell(countryCode, farnellParts),
newark(newarkParts),
]}
</span>
)
return <span>{[digikey(countryCode, digikeyParts)]}</span>
}

const getLocation = async (signal: AbortSignal) => {
Expand Down
80 changes: 74 additions & 6 deletions frontend/src/components/Board/BuyParts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BuyParts = ({ projectFullName, lines, parts }: BuyPartsProps) => {
const [mult, setMult] = useState(1)
const [buyAddPercent, setBuyAddPercent] = useState(0)

const downloadBom = (retailer: string) => {
const downloadBom = (retailer: Retailer) => {
window.plausible('Buy Parts', {
props: {
project: projectFullName,
Expand Down Expand Up @@ -47,7 +47,7 @@ const BuyParts = ({ projectFullName, lines, parts }: BuyPartsProps) => {
return (
<RetailerButton
key={name}
downloadBom={() => downloadBom(name)}
downloadBom={() => downloadBom(name as Retailer)}
name={name}
numberOfLines={numberOfLines}
numberOfParts={numberOfParts}
Expand Down Expand Up @@ -269,9 +269,9 @@ const rsBom = (lines: Array<Line>, multiplier: number, addPercent: number) => {
const farnellBom = (lines: Array<Line>, multiplier: number, addPercent: number) => {
const bom: Array<farnellRow> = [['Part Number', 'Quantity', 'Line Note']]
for (const line of lines) {
if (line.retailers?.Farnell) {
if (line.retailers.Farnell || line.retailers.Newark) {
const row: farnellRow = [
line.retailers.Farnell,
line.retailers.Farnell || line.retailers.Newark,
calculateQuantity(line.quantity, multiplier, addPercent).toString(),
line.description,
]
Expand All @@ -281,11 +281,68 @@ const farnellBom = (lines: Array<Line>, multiplier: number, addPercent: number)
return bom
}

const mouserBom = (lines: Array<Line>, multiplier: number, addPercent: number) => {
const bom: Array<rsRow> = [
[
'Product Number',
'Brand',
'MPN',
'Description',
'Quantity',
'Customer Part Number',
],
]

for (const line of lines) {
if (line.retailers?.Mouser) {
const row: rsRow = [
line.retailers.Mouser,
line.partNumbers?.[0]?.manufacturer,
line.partNumbers?.[0]?.part,
line.description,
calculateQuantity(line.quantity, multiplier, addPercent).toString(),
line.row,
]
bom.push(row)
}
}

return bom
}

const lcscBom = (lines: Array<Line>, multiplier: number, addPercent: number) => {
const bom: Array<lcscBom> = [
[
'Quantity',
'LCSC Part Number',
'Manufacturer',
'Manufacturer Part Number',
'Description',
'Customer Part Number',
],
]

for (const line of lines) {
const row: lcscBom = [
calculateQuantity(line.quantity, multiplier, addPercent).toString(),
line.retailers?.LCSC,
line.partNumbers?.[0]?.manufacturer,
line.partNumbers?.[0]?.part,
line.description,
line.row,
]
bom.push(row)
}

return bom
}
type Retailer = 'RS' | 'Newark' | 'Farnell' | 'Mouser' | 'LCSC'

const csvBom = (
lines: Array<Line>,
multiplier: number,
addPercent: number,
retailer: string,
retailer: Retailer,
) => {
switch (retailer) {
case 'RS':
Expand All @@ -294,6 +351,10 @@ const csvBom = (
case 'Newark':
case 'Farnell':
return farnellBom(lines, multiplier, addPercent)
case 'Mouser':
return mouserBom(lines, multiplier, addPercent)
case 'LCSC':
return lcscBom(lines, multiplier, addPercent)
default:
throw new Error(`Unknown retailer: ${retailer}`)
}
Expand Down Expand Up @@ -338,9 +399,16 @@ interface StoreIconProps {

type rsRow = [string, string, string, string, string, string]
type farnellRow = [string, string, string]
type lcscBom = [string, string, string, string, string, string]

type Line = {
retailers: { RS: string; Farnell: string }
retailers: {
RS: string
Farnell: string
Newark: string
LCSC: string
Mouser: string
}
partNumbers: Array<{ manufacturer: string; part: string }>
description: string
quantity: number
Expand Down

0 comments on commit d95fb99

Please sign in to comment.