Repository URL to install this package:
|
Version:
4.0.61 ▾
|
/**
* @file @todo there is a bug open for missing @description
* should pass `product` not `name`
*/
import { oneRouter } from '@skava/router'
import { isFunction, isString } from 'exotic'
import { EventHandlerArg } from 'typings/generic'
import { ItemProps, ShareProps } from './typings'
import { copy } from './Copy'
// For temporary fix.. Needs to move this in uxui-modules config
const FACEBOOK_APP_ID = '174209059840584'
const pluginsList = [
{
label: 'Facebook',
isSatisfiedBy(label: string): boolean {
return label === 'Facebook'
},
fromPropsToUrl(props): string {
const { productName, item } = props
const fromUrl = item.url
const appid = FACEBOOK_APP_ID
const url =
'https://www.facebook.com/dialog/share?' +
'app_id=' +
encodeURIComponent(appid) +
'&display=popup&caption=' +
encodeURIComponent(productName) +
'&href=' +
encodeURIComponent(fromUrl) +
'&redirect_uri=' +
encodeURIComponent('https://www.facebook.com/')
return url
},
bgColor: '#3A559F',
color: '#fff',
dataQa: 'qa-facebook',
},
{
label: 'Pinterest',
isSatisfiedBy(label: string): boolean {
return label === 'Pinterest'
},
fromPropsToUrl(props): string {
const { productName, item } = props
const fromUrl = item.url
const url =
'https://pinterest.com/pin/create/button/?url=' +
encodeURIComponent(fromUrl) +
'&description=' +
encodeURIComponent(productName)
return url
},
bgColor: '#BD091D',
color: '#fff',
dataQa: 'qa-pinterest',
},
{
label: 'Twitter',
isSatisfiedBy(label: string): boolean {
return label === 'Twitter'
},
fromPropsToUrl(props): string {
const { productName, item } = props
const fromUrl = item.url
const url =
'https://twitter.com/share?url=' +
fromUrl +
'&text=' +
encodeURIComponent(productName)
return url
},
bgColor: '#50ABF1',
color: '#fff',
dataQa: 'qa-twitter',
},
{
label: 'social-link',
isSatisfiedBy(label: string): boolean {
return label === 'social-link'
},
fromPropsToUrl(props): string {
const { item } = props
item.onClick = (args: EventHandlerArg) => {
const currentUrl = oneRouter.get('href')
if (currentUrl) {
copy(currentUrl)
}
if (isFunction(props.onClick)) {
props.onClick(event, item)
}
}
return ''
},
bgColor: '#5F6C88',
color: '#fff',
},
]
export function fromListOfTemplateStringsToRendered(
list: Array<ItemProps>,
props: ShareProps
) {
const toLinkAttributes = args => {
const { item, found } = args
item.url = oneRouter.get('href')
const mergedProps = { ...props, ...item, item }
const url = found.fromPropsToUrl(mergedProps)
const attributes = { ...found, url }
return attributes
}
const toAttributes = args => {
const { found, item } = args
if (isFunction(found.fromPropsToUrl) === true) {
const { isSatisfiedBy, ...remainingAttributes } = toLinkAttributes(args)
return remainingAttributes
} else {
const { isSatisfiedBy, ...remainingAttributes } = found
return remainingAttributes
}
}
const updated = (item: ItemProps, index: number) => {
const isSatisfiedBy = plugin => plugin.isSatisfiedBy(item.label)
const found = pluginsList.find(isSatisfiedBy)
const attributes = toAttributes({ found, item })
return attributes
}
const updatedList = pluginsList.map(updated)
return updatedList
}
export const isNonEmptyString = (x: string): boolean =>
isString(x) && x.length > 0