Skip to content

Commit 6ca0f0e

Browse files
Address review comments
1 parent 6144a49 commit 6ca0f0e

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
import { useGetModel } from 'actions/model'
12
import _ from 'lodash-es'
23
import { useRouter } from 'next/router'
4+
import Loading from 'src/common/Loading'
35
import Title from 'src/common/Title'
6+
import MultipleErrorWrapper from 'src/errors/MultipleErrorWrapper'
47
import SchemaSelect from 'src/schemas/SchemaSelect'
5-
import { SchemaKind } from 'types/types'
8+
import { EntryKind, SchemaKind } from 'types/types'
69

710
export default function AccessRequestSchema() {
811
const router = useRouter()
912
const { modelId }: { modelId?: string } = router.query
13+
const { model, isModelLoading, isModelError } = useGetModel(modelId, EntryKind.MODEL)
14+
15+
const error = MultipleErrorWrapper(`Unable to load schema page`, {
16+
isModelError,
17+
})
18+
19+
if (error) return error
1020

1121
return (
1222
<>
1323
<Title text='Select a schema' />
14-
{<SchemaSelect schemaKind={SchemaKind.ACCESS_REQUEST} id={modelId} />}
24+
{isModelLoading && <Loading />}
25+
{model && <SchemaSelect schemaKind={SchemaKind.ACCESS_REQUEST} entry={model} />}
1526
</>
1627
)
1728
}

frontend/src/schemas/SchemaSelect.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,30 @@ import Loading from 'src/common/Loading'
1212
import Link from 'src/Link'
1313
import MessageAlert from 'src/MessageAlert'
1414
import SchemaButton from 'src/schemas/SchemaButton'
15-
import { EntryInterface, EntryKind, SchemaInterface, SchemaKind, SchemaKindKeys, SchemaKindLabel } from 'types/types'
15+
import {
16+
EntryInterface,
17+
EntryKind,
18+
EntryKindLabel,
19+
SchemaInterface,
20+
SchemaKind,
21+
SchemaKindKeys,
22+
SchemaKindLabel,
23+
} from 'types/types'
1624

1725
type SchemaSelectProps = {
1826
schemaKind: SchemaKindKeys
19-
entry?: EntryInterface
20-
id?: string
27+
entry: EntryInterface
2128
}
2229

23-
export default function SchemaSelect({ schemaKind, entry, id }: SchemaSelectProps) {
30+
export default function SchemaSelect({ schemaKind, entry }: SchemaSelectProps) {
2431
const router = useRouter()
2532
const [loading, setLoading] = useState(false)
2633
const [errorMessage, setErrorMessage] = useState('')
2734

2835
const { schemas, isSchemasLoading, isSchemasError } = useGetSchemas(schemaKind)
2936
const { currentUser, isCurrentUserLoading, isCurrentUserError } = useGetCurrentUser()
3037

31-
const { mutateModel: mutateEntry } = useGetModel(id, EntryKind[schemaKind])
38+
const { mutateModel: mutateEntry } = useGetModel(entry.id, EntryKind[schemaKind])
3239

3340
const isLoadingData = useMemo(
3441
() => isSchemasLoading || isCurrentUserLoading,
@@ -41,9 +48,9 @@ export default function SchemaSelect({ schemaKind, entry, id }: SchemaSelectProp
4148
const accessRequestCallback = useCallback(
4249
async (newSchema: SchemaInterface) => {
4350
setLoading(true)
44-
router.push(`/model/${id}/access-request/new?schemaId=${newSchema.id}`)
51+
router.push(`/model/${entry.id}/access-request/new?schemaId=${newSchema.id}`)
4552
},
46-
[id, router],
53+
[entry.id, router],
4754
)
4855

4956
const entryCallback = useCallback(
@@ -66,11 +73,10 @@ export default function SchemaSelect({ schemaKind, entry, id }: SchemaSelectProp
6673
[currentUser, entry, mutateEntry, router],
6774
)
6875

69-
let selectionCallback = entryCallback
70-
71-
if (schemaKind === SchemaKind.ACCESS_REQUEST) {
72-
selectionCallback = accessRequestCallback
73-
}
76+
const selectionCallback = useMemo(
77+
() => (schemaKind === SchemaKind.ACCESS_REQUEST ? accessRequestCallback : entryCallback),
78+
[schemaKind, accessRequestCallback, entryCallback],
79+
)
7480

7581
const activeSchemaButtons = useMemo(
7682
() =>
@@ -106,8 +112,6 @@ export default function SchemaSelect({ schemaKind, entry, id }: SchemaSelectProp
106112
[inactiveSchemas, selectionCallback, loading],
107113
)
108114

109-
const link = schemaKind === SchemaKind.ACCESS_REQUEST ? `/model/${id}` : `/${schemaKind}/${id}`
110-
111115
if (isSchemasError) {
112116
return <MessageAlert message={isSchemasError.info.message} severity='error' />
113117
}
@@ -122,9 +126,9 @@ export default function SchemaSelect({ schemaKind, entry, id }: SchemaSelectProp
122126
{!isLoadingData && (
123127
<Container maxWidth='md'>
124128
<Card sx={{ mx: 'auto', my: 4, p: 4 }}>
125-
<Link href={link}>
129+
<Link href={`/${entry.kind}/${entry.id}`}>
126130
<Button sx={{ width: 'fit-content' }} startIcon={<ArrowBack />}>
127-
{`Back to ${SchemaKindLabel[schemaKind]}`}
131+
{`Back to ${EntryKindLabel[entry.kind]}`}
128132
</Button>
129133
</Link>
130134
<Stack spacing={2} justifyContent='center' alignItems='center'>

0 commit comments

Comments
 (0)