Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@quintype/amp",
"version": "2.19.0",
"version": "2.20.0-tiktok-task.0",
"description": "Quintype's AMP component library for publisher apps to create amp layouts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/__fixtures__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { seo } from "./seo.fixture";
import { allElementsStory } from "./all-element-story.fixture";
import { liveBlogStory } from "./live-blog-story.fixture";
import { visualStory, card } from "./visual-story.fixture";
import { tiktokElement } from "./tiktok.fixture";

export {
allElementsStory,
Expand All @@ -19,5 +20,6 @@ export {
seo,
liveBlogStory,
visualStory,
card
card,
tiktokElement
};
16 changes: 16 additions & 0 deletions src/__fixtures__/tiktok.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const tiktokElement = {
description: "",
"embed-js":
"PGJsb2NrcXVvdGUgY2xhc3M9InRpa3Rvay1lbWJlZCIgY2l0ZT0iaHR0cHM6Ly93d3cudGlrdG9rLmNvbS9AZGhhcndtYW5zL3ZpZGVvLzczNjE2MzE3OTAwNTE2NDI2NTciIGRhdGEtdmlkZW8taWQ9IjczNjE2MzE3OTAwNTE2NDI2NTciIGRhdGEtZW1iZWQtZnJvbT0ib2VtYmVkIiBzdHlsZT0ibWF4LXdpZHRoOjYwNXB4OyBtaW4td2lkdGg6MzI1cHg7Ij4gPHNlY3Rpb24+IDxhIHRhcmdldD0iX2JsYW5rIiB0aXRsZT0iQGRoYXJ3bWFucyIgaHJlZj0iaHR0cHM6Ly93d3cudGlrdG9rLmNvbS9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0PSJfYmxhbmsiIGhyZWY9Imh0dHBzOi8vd3d3LnRpa3Rvay5jb20vdGFnL2RoYXJtYW5uP3JlZmVyPWVtYmVkIj4jZGhhcm1hbm48L2E+PC9wPiA8YSB0YXJnZXQ9Il9ibGFuayIgdGl0bGU9IuKZrCBvcmlnaW5hbCBzb3VuZCAtIERoYXJXb21hbiAtIFRoZSBWaXJhbCBWaWRlb3MiIGhyZWY9Imh0dHBzOi8vd3d3LnRpa3Rvay5jb20vbXVzaWMvb3JpZ2luYWwtc291bmQtRGhhcldvbWFuLTczNjE2MzE4Njk0ODc1NDkyMTY/cmVmZXI9ZW1iZWQiPuKZrCBvcmlnaW5hbCBzb3VuZCAtIERoYXJXb21hbiAtIFRoZSBWaXJhbCBWaWRlb3M8L2E+IDwvc2VjdGlvbj4gPC9ibG9ja3F1b3RlPiA8c2NyaXB0IGFzeW5jIHNyYz0iaHR0cHM6Ly93d3cudGlrdG9rLmNvbS9lbWJlZC5qcyI+PC9zY3JpcHQ+",
"page-url": "/story/872a1a37-47dd-4c66-88c2-7fa87887652f/element/55cc4513-d602-4d8f-abbd-e8804fa6ad81",
type: "jsembed",
"family-id": "f764a562-a7a2-41b3-8d76-2fe4e0c123a5",
title: "",
id: "c4fba672-a3c8-462c-9cd5-0a341bf681d9",
metadata: {
"tiktok-video-url": "https://www.tiktok.com/@dharwmans/video/7361631790051642657",
provider: "tiktok",
"tiktok-video-id": "7361631790051642657"
},
subtype: "tiktok-video"
};
1 change: 1 addition & 0 deletions src/atoms/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare global {
"amp-story-auto-ads";
"amp-story-page-outlink";
"amp-brightcove";
"amp-tiktok";
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/atoms/story-elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ImageGalleryElement } from "./image-gallery-element";
import { Attachment } from "./attachment";
import { TableElement } from "./table-element";
import { Unsupported } from "./unsupported";
import { TiktokElement } from "./tiktok-element";

const StoryElements = {
Text,
Expand All @@ -47,7 +48,8 @@ const StoryElements = {
Attachment,
TableElement,
Unsupported,
Brightcove
Brightcove,
TiktokElement
};
export {
Text,
Expand All @@ -73,6 +75,7 @@ export {
Attachment,
TableElement,
Unsupported,
Brightcove
Brightcove,
TiktokElement
};
export { StoryElements };
2 changes: 2 additions & 0 deletions src/atoms/story-elements/tiktok-element/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { TiktokElement } from "./tiktok-element";
export { TiktokElement };
19 changes: 19 additions & 0 deletions src/atoms/story-elements/tiktok-element/tiktok-element.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import { storiesOf } from "@storybook/react";
import { config, textStory, tiktokElement } from "../../../__fixtures__";
import { Layout } from "../../layout";
import { TiktokElement } from "./tiktok-element";

const { metadata, ...sampleTiktokElementWithoutMetadata } = tiktokElement;

storiesOf("Tiktok Element", module)
.addDecorator((story) => (
<Layout story={textStory} config={config}>
{story()}
</Layout>
))
.add("Default", () => <TiktokElement element={tiktokElement} />)
.add("With a square different layout", () => (
<TiktokElement element={tiktokElement} layout="fixed" width="400" height="400" />
))
.add("Tiktok without metadata", () => <TiktokElement element={sampleTiktokElementWithoutMetadata} />);
24 changes: 24 additions & 0 deletions src/atoms/story-elements/tiktok-element/tiktok-element.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import { mount, shallow } from "enzyme";
import { config, textStory, tiktokElement } from "../../../__fixtures__";
import { TiktokElement, TiktokElementBase } from "./tiktok-element";

const { metadata, ...sampleTiktokElementWithoutMetadata } = tiktokElement;

describe("Tiktok Element", () => {
it("should render Tiktok", () => {
const wrapper = mount(<TiktokElement element={tiktokElement} />);
expect(wrapper.find("amp-tiktok").length).toBe(1);
});
it("should call tiktokElementRender prop when passed to opts", () => {
const tiktokElementRender = jest.fn();
const modifiedConfig = { ...config, opts: { render: { storyElementRender: { tiktokElementRender } } } };
const wrapper = shallow(<TiktokElementBase element={tiktokElement} story={textStory} config={modifiedConfig} />);
expect(tiktokElementRender.mock.calls.length).toBe(1);
expect(wrapper.find(TiktokElement).length).toBe(0);
});
it("shouldn't render Tiktok", () => {
const wrapper = mount(<TiktokElement element={sampleTiktokElementWithoutMetadata} />);
expect(wrapper.find("amp-tiktok").length).toBe(0);
});
});
66 changes: 66 additions & 0 deletions src/atoms/story-elements/tiktok-element/tiktok-element.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { Fragment } from "react";
import get from "lodash.get";
import Helmet from "react-helmet";
import { withStoryAndConfig } from "../../../context";
import { StoryElementProps } from "../types";
import { TiktokTypes } from "./types";
type TiktokElementProps = StoryElementProps & Omit<TiktokTypes, "data-src">;

export const Tiktok = (props: TiktokTypes) => {
const { width, height } = props;
const setDefaultLayout = !width || !height;
const componentProps: TiktokTypes = setDefaultLayout
? {
...props,
width: "325",
height: "575"
Comment on lines +15 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we give 9/16 instead of this?

}
: props;

return (
<Fragment>
<Helmet>
<script async={undefined} custom-element="amp-tiktok" src="https://cdn.ampproject.org/v0/amp-tiktok-0.1.js" />
</Helmet>
<amp-tiktok {...componentProps} />
</Fragment>
);
};

export const TiktokElementBase = ({ element, story, config, ...props }: TiktokElementProps) => {
const { metadata } = element;
const tiktokElementRender = get(config, ["opts", "render", "storyElementRender", "tiktokElementRender"], null);
if (!(metadata && metadata["tiktok-video-id"])) return null;

return tiktokElementRender ? (
tiktokElementRender({ story, config, element })
) : (
<Tiktok data-src={metadata["tiktok-video-id"]} {...props} />
);
};
/**
* TiktokElement is a story element.
* The look of the TiktokElement can be changed using the render prop tiktokElementRender. In case tiktokElementRender is passed in the config, it is rendered. Otherwise a default TiktokElement is rendered.
*
* @param {Object} params object containing parameters passed to the render prop
* @param {Object} params.story story object
* @param {Object} params.config config object
* @param {Object} params.element the story element. This is same as the story element found in the story API response
*
* ```javascript
* ...
* ampRoutes(app, {
* render: {
* storyElementRender: {
* tiktokElementRender: ({ story, config, element }) => <MyCustomTiktokElement story={story} config={config} storyElement={element} />
* }
* }
* })
* ...
* ```
*
* @category StoryElements
* @module TiktokElement
* @component
*/
export const TiktokElement = withStoryAndConfig(TiktokElementBase);
6 changes: 6 additions & 0 deletions src/atoms/story-elements/tiktok-element/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Common } from "../../../atoms/common-types";

export interface TiktokTypes extends Common {
"data-src": string;
children?: any;
}
4 changes: 3 additions & 1 deletion src/helpers/match-story-element/match-story-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
Attachment,
TableElement,
Unsupported,
Brightcove
Brightcove,
TiktokElement
} from "../../atoms/story-elements";

const anyType = "any";
Expand Down Expand Up @@ -51,6 +52,7 @@ const storyElementsTable = [
["jsembed", "social-media", Unsupported],
["jsembed", "tweet", TwitterElement],
["jsembed", "vidible-video", VidibleElement],
["jsembed", "tiktok-video", TiktokElement],
["jsembed", anyType, Embed],
["polltype", "opinion-poll", Unsupported],
["soundcloud-audio", none, Unsupported],
Expand Down
1 change: 1 addition & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export interface ConfigOpts {
alsoReadRender?: (props: CommonRenderPropTypes) => any;
tableElementRender?: (props: CommonRenderPropTypes) => any;
brightcoveElementRender?: (props: CommonRenderPropTypes) => any;
tiktokElementRender?: (props: CommonRenderPropTypes) => any;
};
};
featureConfig?: FeatureConfigTypes;
Expand Down