feat(integration): ♿️ Improve feedback on GSheets errors
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Input } from '@chakra-ui/react'
|
import { Input, Tooltip, useToast } from '@chakra-ui/react'
|
||||||
import { SearchableDropdown } from 'components/shared/SearchableDropdown'
|
import { SearchableDropdown } from 'components/shared/SearchableDropdown'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import { useSpreadsheets } from 'services/integrations'
|
import { useSpreadsheets } from 'services/integrations'
|
||||||
@@ -14,7 +14,14 @@ export const SpreadsheetsDropdown = ({
|
|||||||
spreadsheetId,
|
spreadsheetId,
|
||||||
onSelectSpreadsheetId,
|
onSelectSpreadsheetId,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const { spreadsheets, isLoading } = useSpreadsheets({ credentialsId })
|
const toast = useToast({
|
||||||
|
position: 'top-right',
|
||||||
|
status: 'error',
|
||||||
|
})
|
||||||
|
const { spreadsheets, isLoading } = useSpreadsheets({
|
||||||
|
credentialsId,
|
||||||
|
onError: (e) => toast({ title: e.name, description: e.message }),
|
||||||
|
})
|
||||||
const currentSpreadsheet = useMemo(
|
const currentSpreadsheet = useMemo(
|
||||||
() => spreadsheets?.find((s) => s.id === spreadsheetId),
|
() => spreadsheets?.find((s) => s.id === spreadsheetId),
|
||||||
[spreadsheetId, spreadsheets]
|
[spreadsheetId, spreadsheets]
|
||||||
@@ -25,7 +32,14 @@ export const SpreadsheetsDropdown = ({
|
|||||||
if (id) onSelectSpreadsheetId(id)
|
if (id) onSelectSpreadsheetId(id)
|
||||||
}
|
}
|
||||||
if (isLoading) return <Input value="Loading..." isDisabled />
|
if (isLoading) return <Input value="Loading..." isDisabled />
|
||||||
if (!spreadsheets) return <Input value="No spreadsheets found" isDisabled />
|
if (!spreadsheets || spreadsheets.length === 0)
|
||||||
|
return (
|
||||||
|
<Tooltip label="No spreadsheets found, make sure you have at least one spreadsheet that contains a header row">
|
||||||
|
<span>
|
||||||
|
<Input value="No spreadsheets found" isDisabled />
|
||||||
|
</span>
|
||||||
|
</Tooltip>
|
||||||
|
)
|
||||||
return (
|
return (
|
||||||
<SearchableDropdown
|
<SearchableDropdown
|
||||||
selectedItem={currentSpreadsheet?.name}
|
selectedItem={currentSpreadsheet?.name}
|
||||||
|
|||||||
@@ -13,10 +13,11 @@ export const oauth2Client = new OAuth2Client(
|
|||||||
export const getAuthenticatedGoogleClient = async (
|
export const getAuthenticatedGoogleClient = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
credentialsId: string
|
credentialsId: string
|
||||||
): Promise<OAuth2Client> => {
|
): Promise<OAuth2Client | undefined> => {
|
||||||
const credentials = (await prisma.credentials.findFirst({
|
const credentials = (await prisma.credentials.findFirst({
|
||||||
where: { id: credentialsId, ownerId: userId },
|
where: { id: credentialsId, ownerId: userId },
|
||||||
})) as CredentialsFromDb
|
})) as CredentialsFromDb | undefined
|
||||||
|
if (!credentials) return
|
||||||
const data = decrypt(
|
const data = decrypt(
|
||||||
credentials.data,
|
credentials.data,
|
||||||
credentials.iv
|
credentials.iv
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
const credentialsId = req.query.credentialsId.toString()
|
const credentialsId = req.query.credentialsId.toString()
|
||||||
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
|
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
|
||||||
|
if (!auth)
|
||||||
|
return res.status(404).send("Couldn't find credentials in database")
|
||||||
const response = await drive({
|
const response = await drive({
|
||||||
version: 'v3',
|
version: 'v3',
|
||||||
auth,
|
auth,
|
||||||
|
|||||||
@@ -19,9 +19,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
|
|
||||||
const spreadsheetId = req.query.id.toString()
|
const spreadsheetId = req.query.id.toString()
|
||||||
const doc = new GoogleSpreadsheet(spreadsheetId)
|
const doc = new GoogleSpreadsheet(spreadsheetId)
|
||||||
doc.useOAuth2Client(
|
const client = await getAuthenticatedGoogleClient(user.id, credentialsId)
|
||||||
await getAuthenticatedGoogleClient(user.id, credentialsId)
|
if (!client)
|
||||||
)
|
return res
|
||||||
|
.status(404)
|
||||||
|
.send({ message: "Couldn't find credentials in database" })
|
||||||
|
doc.useOAuth2Client(client)
|
||||||
await doc.loadInfo()
|
await doc.loadInfo()
|
||||||
return res.send({
|
return res.send({
|
||||||
sheets: (
|
sheets: (
|
||||||
|
|||||||
@@ -12,14 +12,14 @@ export const oauth2Client = new OAuth2Client(
|
|||||||
|
|
||||||
export const getAuthenticatedGoogleClient = async (
|
export const getAuthenticatedGoogleClient = async (
|
||||||
credentialsId: string
|
credentialsId: string
|
||||||
): Promise<OAuth2Client> => {
|
): Promise<OAuth2Client | undefined> => {
|
||||||
const credentials = (await prisma.credentials.findFirst({
|
const credentials = (await prisma.credentials.findFirst({
|
||||||
where: { id: credentialsId },
|
where: { id: credentialsId },
|
||||||
})) as CredentialsFromDb
|
})) as CredentialsFromDb
|
||||||
const data = decrypt(
|
const data = decrypt(credentials.data, credentials.iv) as
|
||||||
credentials.data,
|
| GoogleSheetsCredentialsData
|
||||||
credentials.iv
|
| undefined
|
||||||
) as GoogleSheetsCredentialsData
|
if (!data) return
|
||||||
oauth2Client.setCredentials(data)
|
oauth2Client.setCredentials(data)
|
||||||
oauth2Client.on('tokens', updateTokens(credentialsId))
|
oauth2Client.on('tokens', updateTokens(credentialsId))
|
||||||
return oauth2Client
|
return oauth2Client
|
||||||
|
|||||||
@@ -23,7 +23,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
)
|
)
|
||||||
if (!extractingColumns) return badRequest(res)
|
if (!extractingColumns) return badRequest(res)
|
||||||
const doc = new GoogleSpreadsheet(spreadsheetId)
|
const doc = new GoogleSpreadsheet(spreadsheetId)
|
||||||
doc.useOAuth2Client(await getAuthenticatedGoogleClient(credentialsId))
|
const client = await getAuthenticatedGoogleClient(credentialsId)
|
||||||
|
if (!client)
|
||||||
|
return res.status(404).send("Couldn't find credentials in database")
|
||||||
|
doc.useOAuth2Client(client)
|
||||||
await doc.loadInfo()
|
await doc.loadInfo()
|
||||||
const sheet = doc.sheetsById[sheetId]
|
const sheet = doc.sheetsById[sheetId]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
@@ -48,7 +51,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
values: { [key: string]: string }
|
values: { [key: string]: string }
|
||||||
}
|
}
|
||||||
const doc = new GoogleSpreadsheet(spreadsheetId)
|
const doc = new GoogleSpreadsheet(spreadsheetId)
|
||||||
doc.useOAuth2Client(await getAuthenticatedGoogleClient(credentialsId))
|
const auth = await getAuthenticatedGoogleClient(credentialsId)
|
||||||
|
if (!auth)
|
||||||
|
return res.status(404).send("Couldn't find credentials in database")
|
||||||
|
doc.useOAuth2Client(auth)
|
||||||
await doc.loadInfo()
|
await doc.loadInfo()
|
||||||
const sheet = doc.sheetsById[sheetId]
|
const sheet = doc.sheetsById[sheetId]
|
||||||
await sheet.addRow(values)
|
await sheet.addRow(values)
|
||||||
@@ -65,7 +71,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
values: { [key: string]: string }
|
values: { [key: string]: string }
|
||||||
}
|
}
|
||||||
const doc = new GoogleSpreadsheet(spreadsheetId)
|
const doc = new GoogleSpreadsheet(spreadsheetId)
|
||||||
doc.useOAuth2Client(await getAuthenticatedGoogleClient(credentialsId))
|
const auth = await getAuthenticatedGoogleClient(credentialsId)
|
||||||
|
if (!auth)
|
||||||
|
return res.status(404).send("Couldn't find credentials in database")
|
||||||
|
doc.useOAuth2Client(auth)
|
||||||
await doc.loadInfo()
|
await doc.loadInfo()
|
||||||
const sheet = doc.sheetsById[sheetId]
|
const sheet = doc.sheetsById[sheetId]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
|
|||||||
Reference in New Issue
Block a user