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

chore(edit): refactor three getBoard-functions to one common #1703

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions tavla/app/(admin)/boards/components/TagModal/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { TBoardID } from 'types/settings'
import { firestore } from 'firebase-admin'
import { TTag } from 'types/meta'
import { isEmptyOrSpaces } from 'app/(admin)/edit/utils'
import { getBoard } from 'Board/scenarios/Board/firebase'

async function fetchTags({ bid }: { bid: TBoardID }) {
const board = await firestore().collection('boards').doc(bid).get()
if (!board.exists) throw 'board/not-found'
const board = await getBoard(bid)

const access = await hasBoardEditorAccess(board.id)
if (!access) throw 'auth/operation-not-allowed'

return (board.data()?.meta?.tags as TTag[]) ?? []
return (board?.meta?.tags as TTag[]) ?? []
}

export async function removeTag(
Expand Down
8 changes: 1 addition & 7 deletions tavla/app/(admin)/edit/[id]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ import {
} from 'app/(admin)/utils/firebase'
import { firestore } from 'firebase-admin'
import { redirect } from 'next/navigation'
import { TBoard, TBoardID } from 'types/settings'
import { TTile } from 'types/tile'
import { getWalkingDistance } from 'app/(admin)/components/TileSelector/utils'
import { TLocation } from 'types/meta'
import { revalidatePath } from 'next/cache'
import { makeBoardCompatible } from './compatibility'
import { TBoardID } from 'types/settings'

initializeAdminApp()

export async function getBoard(bid: TBoardID) {
const board = await firestore().collection('boards').doc(bid).get()
return makeBoardCompatible({ id: board.id, ...board.data() } as TBoard)
}

export async function addTile(bid: TBoardID, tile: TTile) {
const access = await hasBoardEditorAccess(bid)
if (!access) return redirect('/')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { TFontSize, TLocation } from 'types/meta'
import { TBoard, TBoardID, TOrganizationID } from 'types/settings'
import { getBoard, getWalkingDistanceTile } from '../../actions'
import { getWalkingDistanceTile } from '../../actions'
import { getUserFromSessionCookie } from 'app/(admin)/utils/server'
import { getBoard } from 'Board/scenarios/Board/firebase'

initializeAdminApp()

Expand Down
3 changes: 2 additions & 1 deletion tavla/app/(admin)/edit/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { redirect } from 'next/navigation'
import { TBoardID } from 'types/settings'
import { addTile, getBoard, getWalkingDistanceTile } from './actions'
import { addTile, getWalkingDistanceTile } from './actions'
import { Heading1, Heading2 } from '@entur/typography'
import { MetaSettings } from './components/MetaSettings'
import { TileSelector } from 'app/(admin)/components/TileSelector'
Expand All @@ -18,6 +18,7 @@ import { Preview } from './components/Preview'
import { ActionsMenu } from './components/ActionsMenu'
import { ThemeSelect } from './components/ThemeSelect'
import { TileList } from './components/TileList'
import { getBoard } from 'Board/scenarios/Board/firebase'

export type TProps = {
params: { id: TBoardID }
Expand Down
13 changes: 1 addition & 12 deletions tavla/app/(admin)/utils/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
'use server'
import admin, { auth, firestore } from 'firebase-admin'
import {
TBoard,
TBoardID,
TOrganization,
TOrganizationID,
TUser,
} from 'types/settings'
import { TBoardID, TOrganization, TOrganizationID, TUser } from 'types/settings'
import { getUserFromSessionCookie } from './server'
import {
getBoardsForOrganization,
Expand Down Expand Up @@ -39,11 +33,6 @@ export async function verifySession(session?: string) {
}
}

export async function getBoard(bid: TBoardID) {
const board = await firestore().collection('boards').doc(bid).get()
return { id: board.id, ...board.data() } as TBoard
}

export async function getUser() {
const user = await getUserFromSessionCookie()
if (!user) return null
Expand Down
7 changes: 6 additions & 1 deletion tavla/src/Board/scenarios/Board/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { makeBoardCompatible } from 'app/(admin)/edit/[id]/compatibility'
import admin, { firestore } from 'firebase-admin'
import { notFound } from 'next/navigation'
import { TBoard, TBoardID, TOrganization } from 'types/settings'

initializeAdminApp()
Expand All @@ -14,7 +16,10 @@ async function initializeAdminApp() {

export async function getBoard(bid: TBoardID) {
const board = await firestore().collection('boards').doc(bid).get()
return { id: board.id, ...board.data() } as TBoard
if (!board.exists) {
notFound()
}
return makeBoardCompatible({ id: board.id, ...board.data() } as TBoard)
}

export async function getOrganizationWithBoard(bid: TBoardID) {
Expand Down