"use strict";
// I M P O R T S
import got from "got";
import html from "choo/html";
// U T I L S
import fetchMetadata from "./helpers/fetch-metadata";
import { generateGitHubFeed } from "./helpers/github";
import messageSlack from "./helpers/slack";
// P R O G R A M
export default (socket, action) => {
if (typeof socket !== "object" && typeof action !== "object") return;
switch(true) {
case action.message === "fetch metadata":
fetchMetadata(action, socket);
break;
case action.message === "landed on homepage":
generateGitHubFeed(result => {
socket.send(JSON.stringify({
html: result,
message: "updated html",
selector: "#github-feed"
}));
});
break;
case action.message === "landed on playground":
generateContent(1, result => {
socket.send(JSON.stringify({
html: result,
message: "updated html",
selector: "#playground-loader"
}));
});
break;
case action.message === "request for playground, example 1":
generateContent(1, result => {
socket.send(JSON.stringify({
html: result,
message: "updated html",
selector: "#playground-loader"
}));
});
break;
case action.message === "request for playground, example 2":
generateMemeCreator(socket);
break;
case action.message === "request for playground, example 3":
generateContent(3, result => {
socket.send(JSON.stringify({
html: result,
message: "updated html",
selector: "#playground-loader"
}));
});
break;
case action.message === "subscribe":
newsletterSubscribe(action, socket);
break;
default:
break;
}
};
// H E L P E R S
function generateContent(exampleNumber, displayTrendingContent) {
if (exampleNumber === 1) {
return getTrendingContent().then(response => {
if (!response || !response.success || response.success !== true || !response.data) return "";
const rawContentCollection = [];
const renderedContentCollection = [];
const trendingContentData = response.data;
for (const data of trendingContentData) {
rawContentCollection.push(fetchMetadata({ claim: data.url, method: "resolve", example: exampleNumber }));
}
Promise.all(rawContentCollection).then(collection => {
for (const part of collection) {
try {
renderedContentCollection.push(`