import api from "@flatfile/api";
export default function flatfileEventListener(listener) {
// Part 1: Setup a listener (https://flatfile.com/docs/apps/custom/meet-the-listener)
//note: listening to all events with a wildcard can be used while testing but is not
//recommended for production, as it will capture all events and may cause performance issues
listener.on("**", (event) => {
// Log all events
console.log(`Received event: ${event.topic}`);
});
listener.namespace(["space:red"], (red) => {
// Part 2: Configure a Space (https://flatfile.com/docs/apps/custom)
red.on(
"job:ready",
{ job: "space:configure" },
async ({ context: { spaceId, environmentId, jobId } }) => {
try {
await api.jobs.ack(jobId, {
info: "Gettin started.",
progress: 10,
});
await api.workbooks.create({
spaceId,
environmentId,
name: "All Data",
labels: ["pinned"],
sheets: [
{
name: "Contacts",
slug: "contacts",
fields: [
{
key: "firstName",
type: "string",
label: "First Name",
},
{
key: "lastName",
type: "string",
label: "Last Name",
},
{
key: "email",
type: "string",
label: "Email",
},
],
},
],
actions: [
{
operation: "submitAction",
mode: "foreground",
label: "Submit foreground",
description: "Submit data to webhook.site",
primary: true,
},
],
});
await api.documents.create(spaceId, {
title: "Getting Started",
body:
"# Welcome\n" +
"### Say hello to your first customer Space in the new Flatfile!\n" +
"Let's begin by first getting acquainted with what you're seeing in your Space initially.\n" +
"---\n",
});
await api.spaces.update(spaceId, {
environmentId,
metadata: {
theme: {
root: {
primaryColor: "red",
},
sidebar: {
backgroundColor: "red",
textColor: "white",
activeTextColor: "midnightblue",
},
// See reference for all possible variables
},
},
});
await api.jobs.complete(jobId, {
outcome: {
message: "Your Space was created.",
acknowledge: true,
},
});
} catch (error) {
console.error("Error:", error.stack);
await api.jobs.fail(jobId, {
outcome: {
message: "Creating a Space encountered an error. See Event Logs.",
acknowledge: true,
},
});
}
}
);
});
}