Skip to content

Commit

Permalink
Merge branch 'main' into feat/compute-variogram
Browse files Browse the repository at this point in the history
  • Loading branch information
mheggelund authored Oct 2, 2023
2 parents f2e3d92 + abd307b commit 3b452b2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/auth/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
},
},
})
22 changes: 2 additions & 20 deletions src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
/* eslint-disable max-lines-per-function */
import { useEffect, useState } from 'react'
import { Button, Table, Typography } from '@equinor/eds-core-react'
import { useParams } from 'react-router-dom'
import { useAnalogueModels } from '../../../hooks/useAnalogueModels'

import { Button, Table, Typography } from '@equinor/eds-core-react'
import { ModelType } from '../../../pages/ModelPages/Model/Model'

export const ModelMetadataView = () => {
const [model, setModel] = useState<ModelType>()
const { id } = useParams()
const { fetchModel } = useAnalogueModels()

useEffect(() => {
async function getModel() {
return await fetchModel({
params: { path: { id: id ?? '' } },
})
.then((response) => response?.data)
.then((model) => model && setModel(model as ModelType))
}
if (!model) {
getModel()
}
}, [id, model, fetchModel])
const { model } = useAnalogueModels(id)

if (!model) return <p>Loading ...</p>

Expand Down
3 changes: 1 addition & 2 deletions src/features/ModelView/ModelNameFrame/ModelNameFrame.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ModelType } from '../../../pages/ModelPages/Model/Model'
import * as Styled from './ModelNameFrame.styled'

export const ModelNameFrame = ({ model }: { model: ModelType }) => {
export const ModelNameFrame = ({ model }: { model: AnalogueModel }) => {
return (
<Styled.NameFrame className="metadata-name-frame">
<h1>{model.name}</h1>
Expand Down
21 changes: 2 additions & 19 deletions src/features/ModelView/ModelSourceView/ModelSourceView.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { useEffect, useState } from 'react'
import { Table, Typography } from '@equinor/eds-core-react'
import { useParams } from 'react-router-dom'
import { useAnalogueModels } from '../../../hooks/useAnalogueModels'

import { Table, Typography } from '@equinor/eds-core-react'
import { ModelType } from '../../../pages/ModelPages/Model/Model'

export const ModelSourceView = () => {
const [model, setModel] = useState<ModelType>()
const { id } = useParams()
const { fetchModel } = useAnalogueModels()

useEffect(() => {
async function getModel() {
return await fetchModel({
params: { path: { id: id ?? '' } },
})
.then((response) => response?.data)
.then((model) => model && setModel(model as ModelType))
}
if (!model) {
getModel()
}
}, [id, model, fetchModel])
const { model } = useAnalogueModels(id)

if (!model) return <p>Loading ...</p>

Expand Down
36 changes: 16 additions & 20 deletions src/hooks/useAnalogueModels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,24 @@ import { useAccessToken } from './useAccessToken'
type UseQueryOptions<T> = ParamsOption<T> &
RequestBodyOption<T> & {
// custom options
params?: {
path: {
id: string
}
}
params?: { path: { id: string } }
}

const ANALOGUEMODELS_KEY = '/api/analogue-models'
const ANALOGUEMODEL_KEY = '/api/analogue-models/{id}'
const NC_FILE_KEY = '/api/analogue-models/{id}/input-models'

export function useAnalogueModels() {
export function useAnalogueModels(id?: string) {
const apiClient = useApiClient()
const token = useAccessToken()
const headers = new Headers({ Authorization: `Bearer ${token}` })

async function fetchModels() {
async function fetchModels(): AnalogueModelResponse {
const { data } = await apiClient.GET(ANALOGUEMODELS_KEY, {
headers: headers,
})
return data
}

async function fetchModel({
params,
}: UseQueryOptions<paths[typeof ANALOGUEMODEL_KEY]['get']>) {
const { data } = await apiClient.GET(ANALOGUEMODEL_KEY, {
params,
headers: new Headers({ Authorization: `Bearer ${token}` }),
})
return data
}

async function createModel({
body,
}: UseQueryOptions<paths[typeof ANALOGUEMODELS_KEY]['post']>) {
Expand Down Expand Up @@ -68,7 +53,18 @@ export function useAnalogueModels() {
return data
}

const models = useQuery([ANALOGUEMODELS_KEY, token], fetchModels)
const models = useQuery(['models', token], fetchModels, { enabled: !!token })
// TODO: might want to add queryFn to this:
const model: AnalogueModel = useQuery([
'models',
token,
{ analogueModelId: id },
])

return { fetchModels, fetchModel, createModel, models, uploadNCFile }
return {
model,
createModel,
models,
uploadNCFile,
}
}
5 changes: 5 additions & 0 deletions src/models/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type AnalogueModel = Partial<
components['schemas']['GetAnalogueModelQueryResponse']
>

type AnalogueModelResponse = AnalogueModel['data']
28 changes: 5 additions & 23 deletions src/pages/ModelPages/Model/Model.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
import { useEffect, useState } from 'react'
import { Outlet, useParams } from 'react-router-dom'
import { useAnalogueModels } from '../../../hooks/useAnalogueModels'
import { components } from '../../../models/schema'
import * as Styled from './Model.styled'

import { ModelNameFrame } from '../../../features/ModelView/ModelNameFrame/ModelNameFrame'
import { ModelNavigationBar } from '../../../features/ModelView/ModelNavigationBar/ModelNavigationBar'

export type ModelType = Partial<
components['schemas']['GetAnalogueModelQueryResponse']['data']
>
import { useAnalogueModels } from '../../../hooks/useAnalogueModels'
import * as Styled from './Model.styled'

export const Model = () => {
const [model, setModel] = useState<ModelType>()
const { id } = useParams()
const { fetchModel } = useAnalogueModels()

useEffect(() => {
async function getModel() {
return await fetchModel({
params: { path: { id: id ?? '' } },
}).then((response) => response?.data)
}
if (!model) {
getModel().then((model) => model && setModel(model as ModelType))
}
}, [id, model, fetchModel])
const { id } = useParams<{ id: string }>()
const { model } = useAnalogueModels(id)

return (
<>
<ModelNameFrame model={model} />
<Styled.Wrapper>
<Styled.SidebarWrapper>
<ModelNavigationBar />
Expand Down

0 comments on commit 3b452b2

Please sign in to comment.