For synchronous data import/exchange completed in one session, create a new Space each time Flatfile is opened. This suits situations where a clean slate for every interaction is preferred.
Now, let’s build a Workbook inside the Space for next time.
Add your config.ts file, and place the following code in it. After you have done so, import the configuration to app.component.ts, and update spaceProps to have the value of config under workbook. This config file has the configuration settings for your workbook, so feel free to edit however necessary to meet your needs.
Copy
import{ Flatfile }from"@flatfile/api";exportconst config: Pick< Flatfile.CreateWorkbookConfig,"name"|"sheets"|"actions">={ name:"Employees workbook", sheets:[{ name:"TestSheet", slug:"TestSheet", fields:[{ key:"first_name", type:"string", label:"First name", constraints:[{ type:"required",},],},{ key:"last_name", type:"string", label:"last name",},{ key:"email", type:"string", label:"Email",},], actions:[{ label:"Join fields", operation:"TestSheet:join-fields", description:"Would you like to join fields?", mode:"foreground", confirm:true,},],},], actions:[{ label:"Submit", operation:"TestSheet:submit", description:"Would you like to submit your workbook?", mode:"foreground", primary:true, confirm:true,},],};
Next, we’ll listen for data changes and respond using an event listener.
Add a src/listeners/listener.ts file with this simple recordHook.
Update app.component.ts to import the listener.
Once you add this code, when a change occurs, we’ll log the entered first name and update the last name to “Rock.” You’ll immediately see this begin to work when you add or update any records. Learn more about Handling Data
Copy
import api from"@flatfile/api";import{ FlatfileListener }from"@flatfile/listener";import{ recordHook }from"@flatfile/plugin-record-hook";/*** Example Listener*/exportconst listener = FlatfileListener.create((listener)=>{ listener.on("**",(event)=>{console.log(`Received event:`, event);}); listener.use(recordHook("contacts",(record)=>{const firstName = record.get("firstName");console.log({ firstName }); record.set("lastName","Rock");return record;})); listener.filter({ job:"workbook:submitActionFg"},(configure)=>{ configure.on("job:ready",async({ context:{ jobId }})=>{try{await api.jobs.ack(jobId,{ info:"Getting started.", progress:10,});// Make changes after cells in a Sheet have been updatedconsole.log("Make changes here when an action is clicked");await api.jobs.complete(jobId,{ outcome:{ acknowledge:true, message:"This is now complete.", next:{ type:"wait",},},});}catch(error:any){console.error("Error:", error.stack);await api.jobs.fail(jobId,{ outcome:{ message:"This job encountered an error.",},});}});});});
By attaching a themeConfig to spaceProps, we will now override colors in your Space to match your brand. See all of the options here in the Theming Reference.