Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cleanup and add tests #82

Merged
merged 13 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
153 changes: 79 additions & 74 deletions apps/react/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
import React, { useState } from 'react'
import styles from './page.module.css'
import { recordHook } from '@flatfile/plugin-record-hook'
import api from '@flatfile/api'
import { config } from '../../../packages/cli/src/config'

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

Expand All @@ -24,13 +22,13 @@ const App = () => {

const [label, setLabel] = useState('Rock')

useListener((listener) => {
// currentListener
listener.on('**', (event) => {
console.log('FFApp useListener Event => ', event.topic)
// Handle the workbook:deleted event
})
}, [])
// useListener((listener) => {
// // currentListener
// listener.on('**', (event) => {
// console.log('FFApp useListener Event => ', event.topic)
// // Handle the workbook:deleted event
// })
// }, [])

// Both of these also work:
// FlatfileListener.create((client) => {
Expand All @@ -39,68 +37,68 @@ const App = () => {
// (listener: FlatfileListener) => {
// useListener(plainListener, [])

useListener((client) => {
client.use(
recordHook('contacts', (record) => {
const firstName = record.get('firstName')
console.log({ firstName })
// Gettign the real types here would be nice but seems tricky
record.set('email', 'Rock')
return record
})
)
}, [])

usePlugin(
recordHook('contacts', (record, event) => {
console.log('recordHook', { event })
record.set('lastName', label)
return record
}),
[label]
)

useEvent('workbook:created', (event) => {
console.log('workbook:created', { event })
})

useEvent('*:created', (event) => {
console.log({ topic: event.topic })
})

useEvent('job:ready', { job: 'sheet:submitActionFg' }, async (event) => {
const { jobId } = event.context
try {
await api.jobs.ack(jobId, {
info: 'Getting started.',
progress: 10,
})

// Make changes after cells in a Sheet have been updated
console.log('Make changes here when an action is clicked')
const records = await event.data

console.log({ records })

await api.jobs.complete(jobId, {
outcome: {
message: 'This is now complete.',
},
})

// Probably a bad idea to close the portal here but just as an example
await sleep(3000)
closePortal()
} catch (error: any) {
console.error('Error:', error.stack)

await api.jobs.fail(jobId, {
outcome: {
message: 'This job encountered an error.',
},
})
}
})
// useListener((client) => {
// client.use(
// recordHook('contacts', (record) => {
// const firstName = record.get('firstName')
// console.log({ firstName })
// // Gettign the real types here would be nice but seems tricky
// record.set('email', 'Rock')
// return record
// })
// )
// }, [])

// usePlugin(
// recordHook('contacts', (record, event) => {
// console.log('recordHook', { event })
// record.set('lastName', label)
// return record
// }),
// [label]
// )

// useEvent('workbook:created', (event) => {
// console.log('workbook:created', { event })
// })

// useEvent('**', (event) => {
// console.log({ event })
// })

// useEvent('job:ready', { job: 'sheet:submitActionFg' }, async (event) => {
// const { jobId } = event.context
// try {
// await api.jobs.ack(jobId, {
// info: 'Getting started.',
// progress: 10,
// })

// // Make changes after cells in a Sheet have been updated
// console.log('Make changes here when an action is clicked')
// const records = await event.data

// console.log({ records })

// await api.jobs.complete(jobId, {
// outcome: {
// message: 'This is now complete.',
// },
// })

// // Probably a bad idea to close the portal here but just as an example
// await sleep(3000)
// closePortal()
// } catch (error: any) {
// console.error('Error:', error.stack)

// await api.jobs.fail(jobId, {
// outcome: {
// message: 'This job encountered an error.',
// },
// })
// }
// })

const listenerConfig = (label: string) => {
setLabel(label)
Expand Down Expand Up @@ -130,14 +128,21 @@ const App = () => {
<Document config={document} />
<Workbook
config={workbook}
onSubmit={(sheet) => {
onSubmit={async (sheet) => {
console.log('onSubmit', { sheet })
}}
onRecordHooks={[
[
'**',
'contacts',
(record) => {
record.set('email', 'SHEET 1')
return record
},
],
[
'contacts',
(record) => {
record.set('email', 'TEST SHEET RECORD')
record.set('lastName', 'SHEET 3')
return record
},
],
Expand Down
22 changes: 1 addition & 21 deletions 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 packages/embedded-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@flatfile/api": "^1.7.11",
"@flatfile/listener": "^1.0.0",
"@flatfile/listener": "^1.0.1",
"@flatfile/util-common": "^1.1.0",
"pubnub": "^7.2.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/embedded-utils/src/types/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export interface SimpleOnboarding extends NewSpaceFromPublishableKey {
sheet?: any
job?: any
event?: FlatfileEvent
}) => void
}) => Promise<void>
onRecordHook?: (
record: FlatfileRecord<TRecordDataWithLinks<TPrimitive>>,
event?: FlatfileEvent
Expand Down
2 changes: 1 addition & 1 deletion packages/listener/src/events/event.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class EventHandler extends AuthenticatedClient {
event: FlatfileEvent | Flatfile.Event | any
): Promise<void> {
if (!event) return
const eventPayload = event.src ? event.src : event
const eventPayload = event.src || event

event = new FlatfileEvent(eventPayload, this._accessToken, this._apiUrl)

Expand Down
18 changes: 0 additions & 18 deletions packages/react/index.html

This file was deleted.

6 changes: 5 additions & 1 deletion packages/react/src/components/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { useCallback, useContext } from 'react'
import type { Flatfile } from '@flatfile/api'
import { useDeepCompareEffect } from '../utils/useDeepCompareEffect'

export const Document = (props: { config: Flatfile.DocumentConfig }) => {
type DocumentProps = {
config: Flatfile.DocumentConfig
}

export const Document = (props: DocumentProps) => {
const { config } = props
const { updateDocument } = useContext(FlatfileContext)

Expand Down
16 changes: 15 additions & 1 deletion packages/react/src/components/FlatfileContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ export const FlatfileContext = createContext<FlatfileContextType>({
updateSheet: () => {},
updateWorkbook: () => {},
updateDocument: () => {},
createSpace: undefined,
createSpace: {
document: undefined,
workbook: {
name: 'Embedded Workbook',
sheets: [],
},
space: {
name: 'Embedded Space',
labels: ['embedded'],
namespace: 'portal',
metadata: {
sidebarConfig: { showSidebar: false },
},
},
},
setCreateSpace: () => {},
updateSpace: () => {},
})
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/components/FlatfileProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const FlatfileProvider: React.FC<ExclusiveFlatfileProviderProps> = ({
setCreateSpace((prevSpace) => {
// Check if the sheet already exists
const sheetExists = prevSpace.workbook.sheets?.some(
(sheet: any) => sheet.slug === newSheet.slug
(sheet) => sheet.slug === newSheet.slug
)
if (sheetExists) {
return prevSpace // Return the state unchanged if the sheet exists
Expand Down Expand Up @@ -118,13 +118,15 @@ export const FlatfileProvider: React.FC<ExclusiveFlatfileProviderProps> = ({
listener.dispatchEvent(flatfileEvent)
}

// Listen to the postMessage event from the created iFrame
useEffect(() => {
window.addEventListener('message', handlePostMessage, false)
return () => {
window.removeEventListener('message', handlePostMessage)
}
}, [listener])

// Mount the event listener to the FlatfileProvider
useEffect(() => {
if (listener && internalAccessToken) {
listener.mount(
Expand Down
Loading
Loading