@dropins/storefront-company-switcher 1.0.0-alpha2 → 1.0.0-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  /*! Copyright 2025 Adobe
2
2
  All Rights Reserved. */
3
- import{jsx as d}from"@dropins/tools/preact-jsx-runtime.js";import{useState as y,useCallback as o,useEffect as I}from"@dropins/tools/preact-hooks.js";import{Picker as v}from"@dropins/tools/components.js";import{events as a}from"@dropins/tools/event-bus.js";import{g as x}from"../chunks/customerCompanies.js";import{setFetchGraphQlHeader as f,removeFetchGraphQlHeader as E}from"@dropins/tools/fetch-graphql.js";const r="X-Adobe-Company",s="DROPIN__COMPANYSWITCHER__COMPANY__CONTEXT",H=({children:A,initialData:O,...h})=>{const[c,m]=y([]),[g,i]=y({text:"",value:""}),p=o(async()=>{var t,n;try{const e=await x();m(e.customerCompanies),(t=e.currentCompany)!=null&&t.name&&((n=e.currentCompany)!=null&&n.id)&&i({text:e.currentCompany.name,value:e.currentCompany.id})}catch(e){console.error("Failed to load company data:",e)}},[]),_=o(t=>{const e=t.target.value;a.emit("companyContext/changed",e),a.emit("checkout/updated",!0),sessionStorage.setItem(s,e),f(r,e)},[]),u=o(()=>{const t=sessionStorage.getItem(s);t&&f(r,t)},[]),C=o(()=>{E(r),sessionStorage.removeItem(s),m([]),i({text:"",value:""})},[]),l=o(async t=>{t?(u(),await p()):C()},[p,u,C]);return I(()=>{const t=a.on("authenticated",l,{eager:!0});return()=>{t&&typeof t=="function"&&t()}},[l]),c.length<2?null:d("div",{...h,children:d(v,{options:c,defaultOption:g,onChange:_})})};export{H as CompanySwitcher,H as default};
3
+ import{jsx as l}from"@dropins/tools/preact-jsx-runtime.js";import{useState as y,useCallback as o,useEffect as I}from"@dropins/tools/preact-hooks.js";import{Picker as v}from"@dropins/tools/components.js";import{events as d}from"@dropins/tools/event-bus.js";import{g as x}from"../chunks/customerCompanies.js";import{setFetchGraphQlHeader as f,removeFetchGraphQlHeader as E}from"@dropins/tools/fetch-graphql.js";const a="X-Adobe-Company",r="DROPIN__COMPANYSWITCHER__COMPANY__CONTEXT",H=({children:A,initialData:O,...h})=>{const[s,c]=y([]),[g,m]=y({text:"",value:""}),i=o(async()=>{var t,n;try{const e=await x();c(e.customerCompanies),(t=e.currentCompany)!=null&&t.name&&((n=e.currentCompany)!=null&&n.id)&&m({text:e.currentCompany.name,value:e.currentCompany.id})}catch(e){console.error("Failed to load company data:",e)}},[]),_=o(t=>{const e=t.target.value;d.emit("companyContext/changed",e),sessionStorage.setItem(r,e),f(a,e)},[]),p=o(()=>{const t=sessionStorage.getItem(r);t&&f(a,t)},[]),C=o(()=>{E(a),sessionStorage.removeItem(r),c([]),m({text:"",value:""})},[]),u=o(async t=>{t?(p(),await i()):C()},[i,p,C]);return I(()=>{const t=d.on("authenticated",u,{eager:!0});return()=>{t&&typeof t=="function"&&t()}},[u]),s.length<2?null:l("div",{...h,children:l(v,{options:s,defaultOption:g,onChange:_})})};export{H as CompanySwitcher,H as default};
4
4
  //# sourceMappingURL=CompanySwitcher.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CompanySwitcher.js","sources":["/@dropins/storefront-company-switcher/src/containers/CompanySwitcher/CompanySwitcher.tsx"],"sourcesContent":["/********************************************************************\n * Copyright 2025 Adobe\n * All Rights Reserved.\n *\n * NOTICE: Adobe permits you to use, modify, and distribute this \n * file in accordance with the terms of the Adobe license agreement \n * accompanying it. \n *******************************************************************/\n \nimport { HTMLAttributes } from 'preact/compat';\nimport { useState, useEffect, useCallback } from 'preact/hooks';\nimport { Container } from '@adobe-commerce/elsie/lib';\nimport { Picker } from '@adobe-commerce/elsie/components';\nimport { events } from '@adobe-commerce/event-bus';\nimport { getCustomerCompanyInfo } from '@/company-switcher/api';\nimport { setFetchGraphQlHeader, removeFetchGraphQlHeader } from '@adobe-commerce/fetch-graphql';\n\nconst COMPANY_HEADER_KEY = 'X-Adobe-Company';\nconst STORAGE_KEY = 'DROPIN__COMPANYSWITCHER__COMPANY__CONTEXT';\n\ninterface Company {\n id: string;\n name: string;\n}\n\ninterface CompanyOption {\n text: string;\n value: string;\n}\n\ninterface CustomerCompanyInfo {\n currentCompany: Company | null;\n customerCompanies: CompanyOption[];\n}\n\nexport interface CompanySwitcherProps extends HTMLAttributes<HTMLDivElement> {}\n\n/**\n * CompanySwitcher component allows users to switch between companies they have access to.\n * It only renders when a user has access to multiple companies.\n */\nexport const CompanySwitcher: Container<CompanySwitcherProps> = ({ \n children, \n initialData: _initialData, \n ...props \n}) => {\n const [companies, setCompanies] = useState<CompanyOption[]>([]);\n const [currentCompany, setCurrentCompany] = useState<CompanyOption>({ text: '', value: '' });\n\n /**\n * Loads company information from the API and updates state\n */\n const loadCompanyData = useCallback(async (): Promise<void> => {\n try {\n const companyInfo: CustomerCompanyInfo = await getCustomerCompanyInfo();\n setCompanies(companyInfo.customerCompanies);\n \n // Set current company if it has valid name and id\n if (companyInfo.currentCompany?.name && companyInfo.currentCompany?.id) {\n setCurrentCompany({\n text: companyInfo.currentCompany.name, \n value: companyInfo.currentCompany.id\n });\n }\n } catch (error) {\n console.error('Failed to load company data:', error);\n // State already initialized with empty values, no additional action needed\n }\n }, []);\n\n /**\n * Handles company selection change\n */\n const handleCompanyChange = useCallback((event: Event): void => {\n const target = event.target as HTMLSelectElement;\n const companyId = target.value;\n \n // Emit event for other components to react to company change\n events.emit('companyContext/changed', companyId);\n \n // Trick cart drop into refreshing cart data.\n events.emit('checkout/updated', true);\n \n // Persist selection\n sessionStorage.setItem(STORAGE_KEY, companyId);\n \n // Update GraphQL header for subsequent requests\n setFetchGraphQlHeader(COMPANY_HEADER_KEY, companyId);\n }, []);\n\n /**\n * Restores company context from session storage\n */\n const restoreCompanyContext = useCallback((): void => {\n const storedCompanyId = sessionStorage.getItem(STORAGE_KEY);\n if (storedCompanyId) {\n setFetchGraphQlHeader(COMPANY_HEADER_KEY, storedCompanyId);\n }\n }, []);\n\n /**\n * Clears company context\n */\n const clearCompanyContext = useCallback((): void => {\n removeFetchGraphQlHeader(COMPANY_HEADER_KEY);\n sessionStorage.removeItem(STORAGE_KEY);\n setCompanies([]);\n setCurrentCompany({ text: '', value: '' });\n }, []);\n\n /**\n * Handles authentication state changes\n */\n const handleAuthenticationChange = useCallback(async (authenticated: boolean): Promise<void> => {\n if (authenticated) {\n restoreCompanyContext();\n await loadCompanyData();\n } else {\n clearCompanyContext();\n }\n }, [loadCompanyData, restoreCompanyContext, clearCompanyContext]);\n\n // Subscribe to authentication events\n useEffect(() => {\n const unsubscribe: any = events.on('authenticated', handleAuthenticationChange, { eager: true });\n \n // Cleanup subscription on unmount\n return () => {\n if (unsubscribe && typeof unsubscribe === 'function') {\n unsubscribe();\n }\n };\n }, [handleAuthenticationChange]);\n\n // Only render if user has access to multiple companies\n if (companies.length < 2) {\n return null;\n }\n\n return (\n <div {...props}>\n <Picker \n options={companies} \n defaultOption={currentCompany} \n onChange={handleCompanyChange}\n />\n </div>\n );\n};\n"],"names":["COMPANY_HEADER_KEY","STORAGE_KEY","CompanySwitcher","children","_initialData","props","companies","setCompanies","useState","currentCompany","setCurrentCompany","loadCompanyData","useCallback","companyInfo","getCustomerCompanyInfo","_a","_b","error","handleCompanyChange","event","companyId","events","setFetchGraphQlHeader","restoreCompanyContext","storedCompanyId","clearCompanyContext","removeFetchGraphQlHeader","handleAuthenticationChange","authenticated","useEffect","unsubscribe","jsx","Picker"],"mappings":"yZAiBA,MAAMA,EAAqB,kBACrBC,EAAc,4CAuBPC,EAAmD,CAAC,CAC/D,SAAAC,EACA,YAAaC,EACb,GAAGC,CACL,IAAM,CACJ,KAAM,CAACC,EAAWC,CAAY,EAAIC,EAA0B,CAAA,CAAE,EACxD,CAACC,EAAgBC,CAAiB,EAAIF,EAAwB,CAAE,KAAM,GAAI,MAAO,GAAI,EAKrFG,EAAkBC,EAAY,SAA2B,SAC7D,GAAI,CACF,MAAMC,EAAmC,MAAMC,EAAA,EAC/CP,EAA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eG,EACf,SAAUS,CAAA,CAAA,EAEd,CAEJ"}
1
+ {"version":3,"file":"CompanySwitcher.js","sources":["/@dropins/storefront-company-switcher/src/containers/CompanySwitcher/CompanySwitcher.tsx"],"sourcesContent":["/********************************************************************\n * Copyright 2025 Adobe\n * All Rights Reserved.\n *\n * NOTICE: Adobe permits you to use, modify, and distribute this \n * file in accordance with the terms of the Adobe license agreement \n * accompanying it. \n *******************************************************************/\n \nimport { HTMLAttributes } from 'preact/compat';\nimport { useState, useEffect, useCallback } from 'preact/hooks';\nimport { Container } from '@adobe-commerce/elsie/lib';\nimport { Picker } from '@adobe-commerce/elsie/components';\nimport { events } from '@adobe-commerce/event-bus';\nimport { getCustomerCompanyInfo } from '@/company-switcher/api';\nimport { setFetchGraphQlHeader, removeFetchGraphQlHeader } from '@adobe-commerce/fetch-graphql';\n\nconst COMPANY_HEADER_KEY = 'X-Adobe-Company';\nconst STORAGE_KEY = 'DROPIN__COMPANYSWITCHER__COMPANY__CONTEXT';\n\ninterface Company {\n id: string;\n name: string;\n}\n\ninterface CompanyOption {\n text: string;\n value: string;\n}\n\ninterface CustomerCompanyInfo {\n currentCompany: Company | null;\n customerCompanies: CompanyOption[];\n}\n\nexport interface CompanySwitcherProps extends HTMLAttributes<HTMLDivElement> {}\n\n/**\n * CompanySwitcher component allows users to switch between companies they have access to.\n * It only renders when a user has access to multiple companies.\n */\nexport const CompanySwitcher: Container<CompanySwitcherProps> = ({ \n children, \n initialData: _initialData, \n ...props \n}) => {\n const [companies, setCompanies] = useState<CompanyOption[]>([]);\n const [currentCompany, setCurrentCompany] = useState<CompanyOption>({ text: '', value: '' });\n\n /**\n * Loads company information from the API and updates state\n */\n const loadCompanyData = useCallback(async (): Promise<void> => {\n try {\n const companyInfo: CustomerCompanyInfo = await getCustomerCompanyInfo();\n setCompanies(companyInfo.customerCompanies);\n \n // Set current company if it has valid name and id\n if (companyInfo.currentCompany?.name && companyInfo.currentCompany?.id) {\n setCurrentCompany({\n text: companyInfo.currentCompany.name, \n value: companyInfo.currentCompany.id\n });\n }\n } catch (error) {\n console.error('Failed to load company data:', error);\n // State already initialized with empty values, no additional action needed\n }\n }, []);\n\n /**\n * Handles company selection change\n */\n const handleCompanyChange = useCallback((event: Event): void => {\n const target = event.target as HTMLSelectElement;\n const companyId = target.value;\n \n // Emit event for other components to react to company change\n events.emit('companyContext/changed', companyId);\n \n // Persist selection\n sessionStorage.setItem(STORAGE_KEY, companyId);\n \n // Update GraphQL header for subsequent requests\n setFetchGraphQlHeader(COMPANY_HEADER_KEY, companyId);\n }, []);\n\n /**\n * Restores company context from session storage\n */\n const restoreCompanyContext = useCallback((): void => {\n const storedCompanyId = sessionStorage.getItem(STORAGE_KEY);\n if (storedCompanyId) {\n setFetchGraphQlHeader(COMPANY_HEADER_KEY, storedCompanyId);\n }\n }, []);\n\n /**\n * Clears company context\n */\n const clearCompanyContext = useCallback((): void => {\n removeFetchGraphQlHeader(COMPANY_HEADER_KEY);\n sessionStorage.removeItem(STORAGE_KEY);\n setCompanies([]);\n setCurrentCompany({ text: '', value: '' });\n }, []);\n\n /**\n * Handles authentication state changes\n */\n const handleAuthenticationChange = useCallback(async (authenticated: boolean): Promise<void> => {\n if (authenticated) {\n restoreCompanyContext();\n await loadCompanyData();\n } else {\n clearCompanyContext();\n }\n }, [loadCompanyData, restoreCompanyContext, clearCompanyContext]);\n\n // Subscribe to authentication events\n useEffect(() => {\n const unsubscribe: any = events.on('authenticated', handleAuthenticationChange, { eager: true });\n \n // Cleanup subscription on unmount\n return () => {\n if (unsubscribe && typeof unsubscribe === 'function') {\n unsubscribe();\n }\n };\n }, [handleAuthenticationChange]);\n\n // Only render if user has access to multiple companies\n if (companies.length < 2) {\n return null;\n }\n\n return (\n <div {...props}>\n <Picker \n options={companies} \n defaultOption={currentCompany} \n onChange={handleCompanyChange}\n />\n </div>\n );\n};\n"],"names":["COMPANY_HEADER_KEY","STORAGE_KEY","CompanySwitcher","children","_initialData","props","companies","setCompanies","useState","currentCompany","setCurrentCompany","loadCompanyData","useCallback","companyInfo","getCustomerCompanyInfo","_a","_b","error","handleCompanyChange","event","companyId","events","setFetchGraphQlHeader","restoreCompanyContext","storedCompanyId","clearCompanyContext","removeFetchGraphQlHeader","handleAuthenticationChange","authenticated","useEffect","unsubscribe","jsx","Picker"],"mappings":"yZAiBA,MAAMA,EAAqB,kBACrBC,EAAc,4CAuBPC,EAAmD,CAAC,CAC/D,SAAAC,EACA,YAAaC,EACb,GAAGC,CACL,IAAM,CACJ,KAAM,CAACC,EAAWC,CAAY,EAAIC,EAA0B,CAAA,CAAE,EACxD,CAACC,EAAgBC,CAAiB,EAAIF,EAAwB,CAAE,KAAM,GAAI,MAAO,GAAI,EAKrFG,EAAkBC,EAAY,SAA2B,SAC7D,GAAI,CACF,MAAMC,EAAmC,MAAMC,EAAA,EAC/CP,EAAaM,EAAY,iBAAiB,GAGtCE,EAAAF,EAAY,iBAAZ,MAAAE,EAA4B,QAAQC,EAAAH,EAAY,iBAAZ,MAAAG,EAA4B,KAClEN,EAAkB,CAChB,KAAMG,EAAY,eAAe,KACjC,MAAOA,EAAY,eAAe,EAAA,CACnC,CAEL,OAASI,EAAO,CACd,QAAQ,MAAM,+BAAgCA,CAAK,CAErD,CACF,EAAG,CAAA,CAAE,EAKCC,EAAsBN,EAAaO,GAAuB,CAE9D,MAAMC,EADSD,EAAM,OACI,MAGzBE,EAAO,KAAK,yBAA0BD,CAAS,EAG/C,eAAe,QAAQnB,EAAamB,CAAS,EAG7CE,EAAsBtB,EAAoBoB,CAAS,CACrD,EAAG,CAAA,CAAE,EAKCG,EAAwBX,EAAY,IAAY,CACpD,MAAMY,EAAkB,eAAe,QAAQvB,CAAW,EACtDuB,GACFF,EAAsBtB,EAAoBwB,CAAe,CAE7D,EAAG,CAAA,CAAE,EAKCC,EAAsBb,EAAY,IAAY,CAClDc,EAAyB1B,CAAkB,EAC3C,eAAe,WAAWC,CAAW,EACrCM,EAAa,CAAA,CAAE,EACfG,EAAkB,CAAE,KAAM,GAAI,MAAO,GAAI,CAC3C,EAAG,CAAA,CAAE,EAKCiB,EAA6Bf,EAAY,MAAOgB,GAA0C,CAC1FA,GACFL,EAAA,EACA,MAAMZ,EAAA,GAENc,EAAA,CAEJ,EAAG,CAACd,EAAiBY,EAAuBE,CAAmB,CAAC,EAehE,OAZAI,EAAU,IAAM,CACd,MAAMC,EAAmBT,EAAO,GAAG,gBAAiBM,EAA4B,CAAE,MAAO,GAAM,EAG/F,MAAO,IAAM,CACPG,GAAe,OAAOA,GAAgB,YACxCA,EAAA,CAEJ,CACF,EAAG,CAACH,CAA0B,CAAC,EAG3BrB,EAAU,OAAS,EACd,KAIPyB,EAAC,MAAA,CAAK,GAAG1B,EACP,SAAA0B,EAACC,EAAA,CACC,QAAS1B,EACT,cAAeG,EACf,SAAUS,CAAA,CAAA,EAEd,CAEJ"}
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name": "@dropins/storefront-company-switcher", "version": "1.0.0-alpha2", "@dropins/tools": "^1.3.0", "license": "SEE LICENSE IN LICENSE.md"}
1
+ {"name": "@dropins/storefront-company-switcher", "version": "1.0.0-beta1", "@dropins/tools": "^1.3.0", "license": "SEE LICENSE IN LICENSE.md"}