Skip to content

Commit

Permalink
adds loadById for views
Browse files Browse the repository at this point in the history
  • Loading branch information
alaister authored and joshenlim committed Mar 23, 2023
1 parent 3d56fad commit 7888148
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
14 changes: 2 additions & 12 deletions studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,10 @@ const TableEditorLayout = ({
}
}, [ui.selectedProject?.ref])

// useEffect(() => {
// if (selectedSchema && ui.selectedProject?.ref) {
// meta.tables.loadBySchema(selectedSchema)
// meta.views.loadBySchema(selectedSchema)
// }
// }, [ui.selectedProject?.ref, selectedSchema])

useEffect(() => {
if (ui.selectedProject?.ref && id) {
// [Joshen] This is a little silly, but because fetching tables/views/foreign-tables
// are all through different endpoints, we need to discern them
if (type !== 'view' && type !== 'foreign') {
meta.tables.loadById(Number(id))
}
meta.tables.loadById(Number(id))
meta.views.loadById(Number(id))
}
}, [ui.selectedProject?.ref, id])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ const EntityListItem = ({

return (
<ProductMenuItem
url={`/project/${projectRef}/editor/${entity.id}?type=${entity.type}`}
url={`/project/${projectRef}/editor/${entity.id}`}
name={entity.name}
hoverText={entity.comment ? entity.comment : entity.name}
isActive={isActive}
Expand Down
6 changes: 3 additions & 3 deletions studio/stores/pgmeta/MetaStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
import { IS_PLATFORM, API_URL } from 'lib/constants'
import { post } from 'lib/common/fetch'
import { timeout } from 'lib/helpers'
import { ResponseError, SchemaView } from 'types'
import { ResponseError } from 'types'

import { IRootStore } from '../RootStore'
import ColumnStore from './ColumnStore'
Expand Down Expand Up @@ -43,7 +43,7 @@ import HooksStore from './HooksStore'
import ExtensionsStore from './ExtensionsStore'
import TypesStore from './TypesStore'
import ForeignTableStore from './ForeignTableStore'
import ViewStore from './ViewStore'
import ViewStore, { IViewStore } from './ViewStore'
import { FOREIGN_KEY_DELETION_ACTION } from 'data/database/database-query-constants'

const BATCH_SIZE = 1000
Expand All @@ -56,7 +56,7 @@ export interface IMetaStore {
tables: ITableStore
columns: IPostgresMetaInterface<PostgresColumn>
schemas: IPostgresMetaInterface<PostgresSchema>
views: IPostgresMetaInterface<SchemaView>
views: IViewStore
foreignTables: IPostgresMetaInterface<Partial<PostgresTable>>

hooks: IPostgresMetaInterface<any>
Expand Down
24 changes: 22 additions & 2 deletions studio/stores/pgmeta/ViewStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { SchemaView } from 'types'
import PostgresMetaInterface from '../common/PostgresMetaInterface'
import { ResponseError, SchemaView } from 'types'
import PostgresMetaInterface, { IPostgresMetaInterface } from '../common/PostgresMetaInterface'
import { IRootStore } from '../RootStore'
import { get } from 'lib/common/fetch'

export interface IViewStore extends IPostgresMetaInterface<SchemaView> {
loadById: (id: number | string) => Promise<Partial<SchemaView> | { error: ResponseError }>
}

export default class ViewStore extends PostgresMetaInterface<SchemaView> {
constructor(
Expand All @@ -13,4 +18,19 @@ export default class ViewStore extends PostgresMetaInterface<SchemaView> {
) {
super(rootStore, dataUrl, headers, options)
}

async loadById(id: number | string) {
try {
const url = `${this.url}?id=${id}`
const response = await get(url, { headers: this.headers })
if (response.error) throw response.error

const data = response as Partial<SchemaView>
// @ts-ignore
this.data[id] = data
return data
} catch (error: any) {
return { error }
}
}
}

0 comments on commit 7888148

Please sign in to comment.