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

feat(boards): visual overhaul #1532

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 30 additions & 1 deletion next-tavla/app/(admin)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function getBoardsForOrganization(oid: TOrganizationID) {
.flat()
}

export async function getBoardsForUser() {
export async function getPrivateBoardsForUser() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brukes denne funksjonen til noe nå eller kan den fjernes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nei, men jeg lurer på om det er greit at den bare ligger hvis vi skal vise alle private tavler til en person en gang? Det har vært litt snakk om å kanskje filterer på organisasjoner uansett på tavler-siden, og da skal vi også kunne filtrere på private tavler

const user = await getUser()
if (!user)
throw new TavlaError({
Expand Down Expand Up @@ -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()
}
83 changes: 31 additions & 52 deletions next-tavla/app/(admin)/boards/[[...id]]/page.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Denne filen sin path burde endres, vi trenger ikke lenger organization id som param

Original file line number Diff line number Diff line change
@@ -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<Metadata> {
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 <div>Du har ikke tilgang til denne organisasjonen</div>
}
const boards = await getAllBoardsForUser()

const boards = id
? await getBoardsForOrganization(id[0] ?? '')
: await getBoardsForUser()
const boardsWithOrganization = await getBoardsWithOrganization(boards)

return (
<div className="flex flex-col md:flex-row gap-3 justify-center">
<SelectOrganization
organizations={organizations}
active={activeOrganization}
/>
<div className="flex flex-col mt-4 md:mt-8 gap-3 grow">
<div className="flex flex-col sm:flex-row md:items-center gap-3">
<Search />
<div className="flex flex-col md:flex-row gap-3">
<FilterButton boards={boards} />
<ToggleBoardsColumns />
</div>
</div>
<div className="overflow-x-auto">
<BoardTable boards={boards} />
</div>
<div className="flex flex-col gap-8">
<Heading1>Tavler</Heading1>
<div className="flex flex-col sm:flex-row md:items-center gap-3">
<Search />
<FilterButton boards={boards} />
</div>
<BoardTable boardsWithOrgs={boardsWithOrganization} />
</div>
)
}

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
}
Comment on lines +44 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Denne funksjonen trenger vi ikke hvis vi skriver om getAllBoardsForUser til å inkludere org direkte. Nå kjører vi getOrganizationsForUser to ganger nesten rett etter hverandre (første i getAllBoardsForUser på linje 26 og deretter i denne funksjonen)

32 changes: 15 additions & 17 deletions next-tavla/app/(admin)/boards/components/BoardTable/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<IllustratedInfo
title="Her var det tomt"
Expand All @@ -20,15 +23,10 @@ function BoardTable({ boards }: { boards: TBoard[] }) {
)

return (
<div
className="grid items-center"
style={{
gridTemplateColumns: `repeat(${columns.length},auto)`,
}}
>
<TableHeader columns={columns as TBoardsColumn[]} />
<TableRows boards={boards} />
</div>
<Table {...getSortableTableProps}>
<TableHeader getSortableHeaderProps={getSortableHeaderProps} />
<TableRows boardsWithOrgs={sortedData} />
</Table>
Comment on lines +26 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeg får opp flere problemer i node, virker som det skjer noe greier i Table komponenten som krasjer serveren. Prøv å import den uten ssr (lazy) og se om det funker da

)
}
export { BoardTable }
2 changes: 1 addition & 1 deletion next-tavla/app/(admin)/boards/components/Column/Column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Column({
children: React.ReactNode
}) {
return (
<div id={column} className="min-h-9 flex flex-row items-center">
<div id={column} className="pl-2 flex flex-row items-center">
{children}
</div>
)
Expand Down
11 changes: 0 additions & 11 deletions next-tavla/app/(admin)/boards/components/Column/Link.tsx

This file was deleted.

12 changes: 10 additions & 2 deletions next-tavla/app/(admin)/boards/components/Column/Name.tsx
Original file line number Diff line number Diff line change
@@ -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 <Column column="name">{name}</Column>
function Name({ board }: { board: TBoard }) {
return (
<Column column="name">
<Link href={`/edit/${board.id}`} className="hover:underline">
{board.meta?.title ?? DEFAULT_BOARD_NAME}
</Link>
</Column>
)
}

export { Name }
24 changes: 24 additions & 0 deletions next-tavla/app/(admin)/boards/components/Column/Organization.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Column column="organization">
{organization.id ? (
<Link
href={`/organizations/${organization.id}`}
className="hover:underline"
>
{orgName}
</Link>
) : (
<p>{orgName}</p>
)}
</Column>
)
}

export { Organization }
35 changes: 0 additions & 35 deletions next-tavla/app/(admin)/boards/components/Column/index.tsx

This file was deleted.

1 change: 0 additions & 1 deletion next-tavla/app/(admin)/boards/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function Search() {
const [value, replace] = useSearchParamReplacer('search')
return (
<TextField
className="w-full md:w-1/2"
label="Søk på navn på tavle"
prepend={<SearchIcon inline aria-hidden="true" />}
defaultValue={value}
Expand Down

This file was deleted.

Loading