Skip to content

Commit

Permalink
Frontend error handling for openai api failing
Browse files Browse the repository at this point in the history
  • Loading branch information
Keskimaki committed Mar 17, 2023
1 parent 0254e8a commit d00ac68
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const App = () => {

return (
<ThemeProvider theme={theme}>
<SnackbarProvider>
<SnackbarProvider preventDuplicate>
<Box
sx={{
minHeight: '100vh',
Expand Down
23 changes: 21 additions & 2 deletions src/client/components/ResultPage/Openai/CourseCompletion.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import React, { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Box, Button, Typography, TextField } from '@mui/material'
import { enqueueSnackbar } from 'notistack'

import useOpenaiCompletion from '../../../hooks/useOpenaiCompletion'
import LoadingProgress from './LoadingProgress'
import styles from '../styles'

const classes = styles.cardStyles

const CompletionResult = ({ courseName }: { courseName: string }) => {
const CompletionResult = ({
courseName,
setShowCompletion,
}: {
courseName: string
setShowCompletion: React.Dispatch<React.SetStateAction<boolean>>
}) => {
const { t } = useTranslation()
const prompt = t('openai:courseCompletionPrompt', { courseName })

const { completion, isLoading } = useOpenaiCompletion(prompt, 'course')

if (isLoading) return <LoadingProgress />

if (!completion) {
enqueueSnackbar(t('openai:apiErrorMessage'), { variant: 'error' })
setShowCompletion(false)

return null
}

return (
<Box sx={classes.outerBox}>
<Typography variant="body1" p={3} whiteSpace="pre-line">
Expand Down Expand Up @@ -52,7 +66,12 @@ const CourseCompletion = () => {
{t('openai:send')}
</Button>
</Box>
{showCompletion && <CompletionResult courseName={courseName} />}
{showCompletion && (
<CompletionResult
courseName={courseName}
setShowCompletion={setShowCompletion}
/>
)}
</Box>
)
}
Expand Down
12 changes: 12 additions & 0 deletions src/client/components/ResultPage/Openai/DimensionCompletion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
MenuItem,
} from '@mui/material'
import { UseFormWatch, FieldValues } from 'react-hook-form'
import { enqueueSnackbar } from 'notistack'

import useOpenaiCompletion from '../../../hooks/useOpenaiCompletion'
import useSurvey from '../../../hooks/useSurvey'
import LoadingProgress from './LoadingProgress'
Expand All @@ -21,8 +23,10 @@ const classes = styles.cardStyles

const CompletionResult = ({
dimension,
setShowCompletion,
}: {
dimension: DimensionSelectionData
setShowCompletion: React.Dispatch<React.SetStateAction<boolean>>
}) => {
const { t, i18n } = useTranslation()

Expand All @@ -44,6 +48,13 @@ const CompletionResult = ({

if (isLoading) return <LoadingProgress />

if (!completion) {
enqueueSnackbar(t('openai:apiErrorMessage'), { variant: 'error' })
setShowCompletion(false)

return null
}

return (
<Box sx={classes.outerBox}>
<Typography variant="body1" p={3} whiteSpace="pre-line">
Expand Down Expand Up @@ -107,6 +118,7 @@ const DimensionCompletion = ({
{showCompletion && (
<CompletionResult
dimension={dimensions.find(({ id }) => id === dimensionId)}
setShowCompletion={setShowCompletion}
/>
)}
</Box>
Expand Down
3 changes: 2 additions & 1 deletion src/client/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"dimensionSelect": "Dimension",
"dimensionCompletionPrompt": "How can I support the topic \"{{dimensionName}}\" in a university course? Give 3 examples using the software {{recommendations}}?",
"send": "Send",
"or": " or "
"or": " or ",
"apiErrorMessage": "No response from the server"
},
"admin": {
"selectDimension": "Valitse oppimismuoto",
Expand Down
3 changes: 2 additions & 1 deletion src/client/locales/fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"dimensionSelect": "Oppimismuoto",
"dimensionCompletionPrompt": "Kuinka voin tukea aihetta \"{{dimensionName}}\" yliopistokurssilla? Anna 3 esimerkkiä käyttäen ohjelmistoa {{recommendations}}?",
"send": "Lähetä",
"or": " tai "
"or": " tai ",
"apiErrorMessage": "Palvelimeen ei saatu yhteyttä"
},
"admin": {
"selectDimension": "Valitse oppimismuoto",
Expand Down
3 changes: 2 additions & 1 deletion src/client/locales/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"dimensionSelect": "Oppimismuoto",
"dimensionCompletionPrompt": "Kuinka voin tukea aihetta \"{{dimensionName}}\" yliopistokurssilla? Anna 3 esimerkkiä käyttäen ohjelmistoa {{recommendations}}?",
"send": "Lähetä",
"or": " tai "
"or": " tai ",
"apiErrorMessage": "Palvelimeen ei saatu yhteyttä"
},
"admin": {
"selectDimension": "Valitse oppimismuoto",
Expand Down

0 comments on commit d00ac68

Please sign in to comment.