-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): multichannel Facebook text converter
- Loading branch information
1 parent
6a95efa
commit dfe2b33
Showing
7 changed files
with
220 additions
and
15 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/botonic-react/src/components/multichannel/facebook/facebook.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export const MAX_CHARACTERS_FACEBOOK = 640 | ||
|
||
export class MultichannelFacebook { | ||
constructor() {} | ||
|
||
convertText(props, originalText) { | ||
if (originalText.length > MAX_CHARACTERS_FACEBOOK) { | ||
const texts = this.splitText(originalText) | ||
const lastText = texts.pop() | ||
const { propsLastText, propsWithoutChildren } = this.getNewProps( | ||
props, | ||
lastText | ||
) | ||
return { texts, propsLastText, propsWithoutChildren } | ||
} | ||
return { propsLastText: props } | ||
} | ||
|
||
splitText(originalText) { | ||
const lines = originalText.split('\n') | ||
const initialText = lines.shift() | ||
const texts = [initialText] | ||
let index = 0 | ||
lines.forEach(currentText => { | ||
if (texts[index].length + currentText.length > MAX_CHARACTERS_FACEBOOK) { | ||
index++ | ||
texts.push(currentText) | ||
} else { | ||
texts[index] = texts[index].concat('\n', currentText) | ||
} | ||
}) | ||
return texts | ||
} | ||
|
||
// modifies the props to keep the children only for the last text message, this message will be the one with buttons and replies | ||
getNewProps(props, lastText) { | ||
const propsLastText = { ...props } | ||
propsLastText.children = [lastText] | ||
if (Array.isArray(props.children)) { | ||
props.children | ||
.filter(e => e.type) | ||
.forEach(e => propsLastText.children.push(e)) | ||
} | ||
const propsWithoutChildren = { ...props } | ||
delete propsWithoutChildren.children | ||
return { propsLastText, propsWithoutChildren } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/botonic-react/tests/components/multichannel/facebook/facebook.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { MultichannelFacebook } from '../../../../src/components/multichannel/facebook/facebook' | ||
|
||
describe('Multichannel Facebook text converter', () => { | ||
const multichannelFacebook = new MultichannelFacebook() | ||
const button = { | ||
key: '1', | ||
props: { | ||
children: 'Children', | ||
url: 'https://botonic.io/', | ||
}, | ||
type: 'Button', | ||
} | ||
|
||
const shortText = | ||
'Continetur devenietur id ex denegassem ponderibus imaginatio patrocinio.' | ||
|
||
const longText = | ||
'Continetur devenietur id ex denegassem ponderibus imaginatio patrocinio. Falso et se re entis quasi im. Ipsum ne opera at potui ipsis mo. Hic advertisse manifestum uti blandisque objectivae imaginaria assignetur. Re dare dari data ad ex isti ad meas quin. Aeque neque at multo coeco ac. Ullius habens longum necdum negans si ut.\nIm quia odor scio ea. Habet hic duo operi cum fas ullis. Dicunt at attigi re mentem eo longum ne creari videor. Credendam incurrant simplicia tantumque desumptam to de. Rationes ad re quanquam sensisse ac frigoris. Summam eo gustum seriem in vi. Credimus sorbonae ad ac in de cogitare.\nRem summum ope eae notatu sicuti calida causas. Machinam assidere circulum in facultas ab. Haberem volebam tur verarum mallent etc una seu referam ignotae. Heri sic rum ante sine quas fas modi. Nos creasse pendere crescit angelos etc. Is ii veat se suae admi nisi data meas an. Ei probent enatare et naturam. Igni bere meum at vi meae ob ente foco. Progressum expectanti deo advertebam confirmari objectivam age tractandae vix dem. Assentiar im singulari examinare voluntate inhaereat de si colligere me.\nAusi ente me idem utor adeo ob ille. Hominem inferri hos effugio una vel istarum. Gnum ii iste amen ab visu atra. Deo sic olim sese amen. Im co vereor opinio certas. Et legendo caetera disputo saporis exhibet ei. Propositio via explicetur ibi est designabam necessario quo.' | ||
|
||
const firstTexts = [ | ||
'Continetur devenietur id ex denegassem ponderibus imaginatio patrocinio. Falso et se re entis quasi im. Ipsum ne opera at potui ipsis mo. Hic advertisse manifestum uti blandisque objectivae imaginaria assignetur. Re dare dari data ad ex isti ad meas quin. Aeque neque at multo coeco ac. Ullius habens longum necdum negans si ut.\nIm quia odor scio ea. Habet hic duo operi cum fas ullis. Dicunt at attigi re mentem eo longum ne creari videor. Credendam incurrant simplicia tantumque desumptam to de. Rationes ad re quanquam sensisse ac frigoris. Summam eo gustum seriem in vi. Credimus sorbonae ad ac in de cogitare.', | ||
'Rem summum ope eae notatu sicuti calida causas. Machinam assidere circulum in facultas ab. Haberem volebam tur verarum mallent etc una seu referam ignotae. Heri sic rum ante sine quas fas modi. Nos creasse pendere crescit angelos etc. Is ii veat se suae admi nisi data meas an. Ei probent enatare et naturam. Igni bere meum at vi meae ob ente foco. Progressum expectanti deo advertebam confirmari objectivam age tractandae vix dem. Assentiar im singulari examinare voluntate inhaereat de si colligere me.', | ||
] | ||
|
||
const lastText = | ||
'Ausi ente me idem utor adeo ob ille. Hominem inferri hos effugio una vel istarum. Gnum ii iste amen ab visu atra. Deo sic olim sese amen. Im co vereor opinio certas. Et legendo caetera disputo saporis exhibet ei. Propositio via explicetur ibi est designabam necessario quo.' | ||
|
||
const expectedPropsWithoutChildren = { | ||
delay: '1', | ||
} | ||
|
||
test('TEST: Splits long text', () => { | ||
const props = { | ||
children: longText, | ||
delay: '1', | ||
} | ||
const { | ||
texts, | ||
propsLastText, | ||
propsWithoutChildren, | ||
} = multichannelFacebook.convertText(props, longText) | ||
|
||
const expectedPropsLastText = { | ||
children: [lastText], | ||
delay: '1', | ||
} | ||
|
||
expect(texts).toEqual(firstTexts) | ||
expect(propsLastText).toEqual(expectedPropsLastText) | ||
expect(propsWithoutChildren).toEqual(expectedPropsWithoutChildren) | ||
}) | ||
|
||
test('TEST: Splits long text with buttons', () => { | ||
const props = { | ||
children: [longText, button], | ||
delay: '1', | ||
} | ||
const { | ||
texts, | ||
propsLastText, | ||
propsWithoutChildren, | ||
} = multichannelFacebook.convertText(props, longText) | ||
|
||
const expectedPropsLastText = { | ||
children: [lastText, button], | ||
delay: '1', | ||
} | ||
|
||
expect(texts).toEqual(firstTexts) | ||
expect(propsLastText).toEqual(expectedPropsLastText) | ||
expect(propsWithoutChildren).toEqual(expectedPropsWithoutChildren) | ||
}) | ||
|
||
test('TEST: Does not split short text', () => { | ||
const props = { | ||
children: shortText, | ||
delay: '1', | ||
} | ||
const { | ||
texts, | ||
propsLastText, | ||
propsWithoutChildren, | ||
} = multichannelFacebook.convertText(props, shortText) | ||
expect(texts).toEqual(undefined) | ||
expect(propsLastText).toEqual(props) | ||
expect(propsWithoutChildren).toEqual(undefined) | ||
}) | ||
|
||
test('TEST: Does not split short text with buttons', () => { | ||
const props = { | ||
children: [shortText, button], | ||
delay: '1', | ||
} | ||
const { | ||
texts, | ||
propsLastText, | ||
propsWithoutChildren, | ||
} = multichannelFacebook.convertText(props, shortText) | ||
expect(texts).toEqual(undefined) | ||
expect(propsLastText).toEqual(props) | ||
expect(propsWithoutChildren).toEqual(undefined) | ||
}) | ||
}) |