Repository URL to install this package:
|
Version:
1.1.8 ▾
|
/**
* @file @todo there is a bug open for missing @description
* should pass `product` not `name`
*/
import { oneRouter } from 'uxui-modules/router'
import { isFunction, isString } from 'uxui-modules/exotic'
import { EventHandlerArg } from 'typings/generic'
import { ItemProps, ShareProps } from './typings'
import { copy } from './Copy'
// import { FACEBOOK_APP_ID } from 'uxui-modules/api/config'
// For temporary fix.. Needs to move this in uxui-modules config
const FACEBOOK_APP_ID = '174209059840584'
// export function copyToClipboard(urlToCopy: string): void {
// const listener = event => {
// event.clipboardData.setData('text/plain', urlToCopy)
// // alert('Hurray! Url copied to clipboard:\r\n' + urlToCopy)
// event.preventDefault()
// }
// document.addEventListener('copy', listener)
// document.execCommand('copy')
// document.removeEventListener('copy', listener)
// }
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',
},
{
label: 'Pinterest',
isSatisfiedBy(label: string): boolean {
return label === 'Pinterest'
},
fromPropsToUrl(props): string {
const { media, productName, item } = props
const fromUrl = item.url
const url = 'https://pinterest.com/pin/create/button/?url='
+ encodeURIComponent(fromUrl) + '&media='
+ encodeURIComponent(media) + '&description='
+ encodeURIComponent(productName)
return url
},
bgcolor: '#BD091D',
color: '#fff',
},
{
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/intent/tweet?text=' + encodeURIComponent(productName)
return url
},
bgcolor: '#50ABF1',
color: '#fff',
},
{
label: 'social-link',
isSatisfiedBy(label: string): boolean {
return label === 'social-link'
},
fromPropsToUrl(props): string {
const { productName, 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