diff --git a/next-tavla/app/(admin)/actions.ts b/next-tavla/app/(admin)/actions.ts index adb003174..4ee376f15 100644 --- a/next-tavla/app/(admin)/actions.ts +++ b/next-tavla/app/(admin)/actions.ts @@ -75,7 +75,7 @@ export async function getBoardsForOrganization(oid: TOrganizationID) { .flat() } -export async function getBoardsForUser() { +export async function getPrivateBoardsForUser() { const user = await getUser() if (!user) throw new TavlaError({ @@ -104,3 +104,32 @@ export async function getBoardsForUser() { ) .flat() } + +export async function getAllBoardsForUser() { + const user = await getUser() + if (!user) return redirect('/') + + const privateBoardIDs = concat(user.owner ?? [], user.editor ?? []) + + const organizations = await getOrganizationsForUser() + const organizationBoardIDs = organizations.map((o) => o.boards) + + const boardIDs = concat(privateBoardIDs, organizationBoardIDs.flat()) + + const batchedBoardIDs = chunk(boardIDs, 20) + + const boardQueries = batchedBoardIDs.map((batch) => + firestore() + .collection('boards') + .where(firestore.FieldPath.documentId(), 'in', batch) + .get(), + ) + + const boardRefs = await Promise.all(boardQueries) + + return boardRefs + .map((ref) => + ref.docs.map((doc) => ({ id: doc.id, ...doc.data() } as TBoard)), + ) + .flat() +} diff --git a/next-tavla/app/(admin)/boards/[[...id]]/page.tsx b/next-tavla/app/(admin)/boards/[[...id]]/page.tsx index 113f083a4..56959ca81 100644 --- a/next-tavla/app/(admin)/boards/[[...id]]/page.tsx +++ b/next-tavla/app/(admin)/boards/[[...id]]/page.tsx @@ -1,80 +1,59 @@ import { permanentRedirect } from 'next/navigation' -import { SelectOrganization } from '../components/SelectOrganization' import { Search } from '../components/Search' import { FilterButton } from '../components/FilterButton' -import { ToggleBoardsColumns } from '../components/ToggleBoardsColumns' import { BoardTable } from '../components/BoardTable' import { Metadata } from 'next' import React from 'react' import { - getBoardsForOrganization, - getBoardsForUser, - getOrganizationIfUserHasAccess, + getAllBoardsForUser, getOrganizationsForUser, } from 'app/(admin)/actions' import { initializeAdminApp } from 'app/(admin)/utils/firebase' import { getUserFromSessionCookie } from 'app/(admin)/utils/server' +import { Heading1 } from '@entur/typography' +import { TBoard, TBoardWithOrganization } from 'types/settings' initializeAdminApp() -type TProps = { - params: { id: string[] } +export const metadata: Metadata = { + title: `Tavler | Entur Tavla`, } -export async function generateMetadata({ params }: TProps): Promise { - const { id } = params - - const organization = id - ? await getOrganizationIfUserHasAccess(id[0] ?? '') - : { name: 'Mine' } - - return { - title: `${organization?.name} tavler | Entur Tavla`, - } -} - -async function OrganizationsBoardsPage({ params }: TProps) { - const { id } = params +async function OrganizationsBoardsPage() { const user = await getUserFromSessionCookie() if (!user) permanentRedirect('/') - const organizations = await getOrganizationsForUser() - const activeOrganization = await getOrganizationIfUserHasAccess( - (id ?? '')[0], - ) - - const hasAccess = - activeOrganization && - (activeOrganization.owners?.includes(user.uid) || - activeOrganization.editors?.includes(user.uid)) - if (id && !hasAccess) { - return
Du har ikke tilgang til denne organisasjonen
- } + const boards = await getAllBoardsForUser() - const boards = id - ? await getBoardsForOrganization(id[0] ?? '') - : await getBoardsForUser() + const boardsWithOrganization = await getBoardsWithOrganization(boards) return ( -
- -
-
- -
- - -
-
-
- -
+
+ Tavler +
+ +
+
) } export default OrganizationsBoardsPage + +async function getBoardsWithOrganization(boards: TBoard[]) { + const organizations = await getOrganizationsForUser() + + const boardsWithOrganization = boards.map((board: TBoard) => { + const org = organizations.find((org) => + org.boards?.includes(board.id ?? ''), + ) + + return { + board: { ...board }, + organization: { ...org }, + } as TBoardWithOrganization + }) + + return boardsWithOrganization +} diff --git a/next-tavla/app/(admin)/boards/components/BoardTable/index.tsx b/next-tavla/app/(admin)/boards/components/BoardTable/index.tsx index 918bb4db9..bb74d7590 100644 --- a/next-tavla/app/(admin)/boards/components/BoardTable/index.tsx +++ b/next-tavla/app/(admin)/boards/components/BoardTable/index.tsx @@ -1,17 +1,20 @@ 'use client' -import { TableHeader } from 'app/(admin)/boards/components/TableHeader' import { TableRows } from 'app/(admin)/boards/components/TableRows' -import { TBoard } from 'types/settings' +import { TBoardWithOrganization } from 'types/settings' import { isEmpty } from 'lodash' -import { useSearchParam } from '../../hooks/useSearchParam' import { IllustratedInfo } from 'app/(admin)/components/IllustratedInfo' -import { DEFAULT_BOARD_COLUMNS, TBoardsColumn } from 'app/(admin)/utils/types' +import { Table, useSortableData } from '@entur/table' +import { TableHeader } from '../TableHeader' -function BoardTable({ boards }: { boards: TBoard[] }) { - const value = useSearchParam('columns') - const columns = value?.split(',') ?? DEFAULT_BOARD_COLUMNS +function BoardTable({ + boardsWithOrgs, +}: { + boardsWithOrgs: TBoardWithOrganization[] +}) { + const { sortedData, getSortableHeaderProps, getSortableTableProps } = + useSortableData(boardsWithOrgs) - if (isEmpty(boards)) + if (isEmpty(boardsWithOrgs)) return ( - - -
+ + + +
) } export { BoardTable } diff --git a/next-tavla/app/(admin)/boards/components/Column/Column.tsx b/next-tavla/app/(admin)/boards/components/Column/Column.tsx index 5670e2bbf..08ea79c68 100644 --- a/next-tavla/app/(admin)/boards/components/Column/Column.tsx +++ b/next-tavla/app/(admin)/boards/components/Column/Column.tsx @@ -8,7 +8,7 @@ function Column({ children: React.ReactNode }) { return ( -
+
{children}
) diff --git a/next-tavla/app/(admin)/boards/components/Column/Link.tsx b/next-tavla/app/(admin)/boards/components/Column/Link.tsx deleted file mode 100644 index c7b7f2439..000000000 --- a/next-tavla/app/(admin)/boards/components/Column/Link.tsx +++ /dev/null @@ -1,11 +0,0 @@ -'use client' -import { useLink } from '../../../../../src/Shared/hooks/useLink' -import { Column } from './Column' - -function Link({ bid }: { bid?: string }) { - const link = useLink(bid) - if (!link) return
- - return {link} -} -export { Link } diff --git a/next-tavla/app/(admin)/boards/components/Column/Name.tsx b/next-tavla/app/(admin)/boards/components/Column/Name.tsx index fc385ff9c..0bf6a3bfc 100644 --- a/next-tavla/app/(admin)/boards/components/Column/Name.tsx +++ b/next-tavla/app/(admin)/boards/components/Column/Name.tsx @@ -1,8 +1,16 @@ import { DEFAULT_BOARD_NAME } from 'app/(admin)/utils/constants' import { Column } from './Column' +import { TBoard } from 'types/settings' +import Link from 'next/link' -function Name({ name = DEFAULT_BOARD_NAME }: { name?: string }) { - return {name} +function Name({ board }: { board: TBoard }) { + return ( + + + {board.meta?.title ?? DEFAULT_BOARD_NAME} + + + ) } export { Name } diff --git a/next-tavla/app/(admin)/boards/components/Column/Organization.tsx b/next-tavla/app/(admin)/boards/components/Column/Organization.tsx new file mode 100644 index 000000000..1e8245518 --- /dev/null +++ b/next-tavla/app/(admin)/boards/components/Column/Organization.tsx @@ -0,0 +1,24 @@ +import Link from 'next/link' +import { Column } from './Column' +import { TOrganization } from 'types/settings' + +function Organization({ organization }: { organization: TOrganization }) { + const orgName = organization.name ?? 'Privat' + + return ( + + {organization.id ? ( + + {orgName} + + ) : ( +

{orgName}

+ )} +
+ ) +} + +export { Organization } diff --git a/next-tavla/app/(admin)/boards/components/Column/index.tsx b/next-tavla/app/(admin)/boards/components/Column/index.tsx deleted file mode 100644 index 100f61a13..000000000 --- a/next-tavla/app/(admin)/boards/components/Column/index.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import { TBoard } from 'types/settings' -import { Actions } from './Actions' -import { LastModified } from './LastModified' -import { Name } from './Name' -import { Tags } from './Tags' -import { Link } from './Link' -import { TTag } from 'types/meta' -import { TBoardsColumn } from 'app/(admin)/utils/types' - -function Column({ - board, - column, - tags, -}: { - board: TBoard - column: TBoardsColumn - tags: TTag[] -}) { - switch (column) { - case 'name': - return - case 'url': - return - case 'actions': - return - case 'lastModified': - return - case 'tags': - return - default: - return
Ukjent kolonne
- } -} - -export { Column } diff --git a/next-tavla/app/(admin)/boards/components/Search/index.tsx b/next-tavla/app/(admin)/boards/components/Search/index.tsx index aefb45cb6..b77d89d99 100644 --- a/next-tavla/app/(admin)/boards/components/Search/index.tsx +++ b/next-tavla/app/(admin)/boards/components/Search/index.tsx @@ -7,7 +7,6 @@ function Search() { const [value, replace] = useSearchParamReplacer('search') return (