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

BAI-600 Refactored MetadataDisplay to use 'useGetSchema' #572

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 16 additions & 18 deletions frontend/src/MetadataDisplay.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { ThemeProvider } from '@mui/material/styles'
import { render, screen, waitFor } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'

import { useGetSchemas } from '../data/schema'
import { useGetSchema } from '../data/schema'
import MetadataDisplay from './MetadataDisplay'
import { lightTheme } from './theme'

vi.mock('../data/schema', () => ({
useGetSchemas: vi.fn(),
useGetSchema: vi.fn(),
}))

describe('MetadataDisplay', () => {
Expand All @@ -19,27 +19,25 @@ describe('MetadataDisplay', () => {
}

const mockedSchema: any = {
schemas: [
{
name: 'upload-schema',
reference: 'test-schema',
use: 'UPLOAD',
schema: {
type: 'object',
properties: {
question: {
type: 'string',
title: 'This is a test question',
},
schema: {
name: 'upload-schema',
reference: 'test-schema',
use: 'UPLOAD',
schema: {
type: 'object',
properties: {
question: {
type: 'string',
title: 'This is a test question',
},
},
},
],
isSchemasLoading: false,
isSchemasError: false,
},
isSchemaLoading: false,
isSchemaError: false,
}

vi.mocked(useGetSchemas).mockReturnValueOnce(mockedSchema).mockReturnValueOnce(mockedSchema)
vi.mocked(useGetSchema).mockReturnValueOnce(mockedSchema).mockReturnValueOnce(mockedSchema)

render(
<ThemeProvider theme={lightTheme}>
Expand Down
26 changes: 13 additions & 13 deletions frontend/src/MetadataDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTheme } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
import React, { useEffect, useState } from 'react'

import { useGetSchemas } from '../data/schema'
import { useGetSchema } from '../data/schema'
import { printProperty } from '../utils/propertyUtils'
import CommonTabs from './common/CommonTabs'

Expand All @@ -17,37 +17,37 @@ function MetadataDisplay({
tabsDisplaySequentially: boolean
use: any
}) {
const { schemas, isSchemasLoading, isSchemasError } = useGetSchemas(use)
const { schema, isSchemaLoading, isSchemaError } = useGetSchema(item.schemaRef)

const [schema, setSchema] = useState<any | undefined>(undefined)
const [currentSchema, setCurrentSchema] = useState<any | undefined>(undefined)
const [sectionKeys, setSectionKeys] = useState<string[]>([])

const theme = useTheme()

useEffect(() => {
if (!schemas) return
if (!schema) return

const propertiesToIgnore = ['id', 'timeStamp', 'schemaRef', 'schemaVersion', 'user', 'contacts']

const currentSchema = schemas.filter(({ reference }) => reference === item.schemaRef)[0].schema
const currentSchema = schema.schema
const keys = Object.keys(currentSchema.properties).filter(
(sectionName) =>
!propertiesToIgnore.includes(sectionName) && currentSchema.properties[sectionName].displayModelCard !== false
)

setSchema(currentSchema)
setCurrentSchema(currentSchema)
setSectionKeys(keys)
}, [schemas, setSchema, setSectionKeys, item])
}, [schema, setCurrentSchema, setSectionKeys, item])

if (isSchemasLoading) {
if (isSchemaLoading) {
return (
<Typography variant='body1' component='p'>
Loading Schemas
</Typography>
)
}

if (isSchemasError) {
if (isSchemaError) {
return (
<Typography variant='body1' component='p'>
Error Loading Schemas
Expand Down Expand Up @@ -135,18 +135,18 @@ function MetadataDisplay({
}

const printSections = () => {
if (!schema || !schema.properties || !item) {
if (!currentSchema || !currentSchema.properties || !item) {
return null
}

return sectionKeys.map((key, i) => {
const divider = i + 1 < sectionKeys.length ? <Divider variant='middle' sx={{ mt: 2, mb: 4 }} /> : null

return schema.properties[key] ? (
return currentSchema.properties[key] ? (
<div key={key}>
<div id={`${key}-section-id`}>
{heading(`${schema.properties[key].title}`)}
{printProps(schema.properties[key], item[key])}
{heading(`${currentSchema.properties[key].title}`)}
{printProps(currentSchema.properties[key], item[key])}
</div>
{divider}
</div>
Expand Down