Skip to content

Commit

Permalink
Merge branch 'master' into global-preview
Browse files Browse the repository at this point in the history
  • Loading branch information
natashatikhonova authored Nov 13, 2023
2 parents 4159201 + f04b646 commit 1734f96
Show file tree
Hide file tree
Showing 32 changed files with 672 additions and 68 deletions.
42 changes: 42 additions & 0 deletions next-tavla/pages/api/organization/[oid]/members/[uid].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { verifyUserSession } from 'Admin/utils/auth'
import {
getOrganizationById,
removeUserFromOrganization,
} from 'Admin/utils/firebase'
import { NextApiRequest, NextApiResponse } from 'next'

export default async function handler(
request: NextApiRequest,
response: NextApiResponse,
) {
try {
const { oid, uid } = request.query as { oid: string; uid: string }

const organization = await getOrganizationById(oid)
if (!organization) {
return response
.status(404)
.json({ message: 'Organization not found' })
}

const requester = await verifyUserSession(request)

if (!requester || !organization.owners?.includes(requester.uid)) {
return response.status(401).json({ message: 'Unauthorized' })
}

switch (request.method) {
case 'DELETE':
await removeUserFromOrganization(oid, uid)
return response.status(200).json({ message: 'User removed' })
default:
return response
.status(405)
.json({ message: 'Method not allowed' })
}
} catch (e) {
if (e instanceof Error) {
response.status(400).json({ error: e.message })
}
}
}
45 changes: 45 additions & 0 deletions next-tavla/pages/api/organization/[oid]/members/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { verifyUserSession } from 'Admin/utils/auth'
import {
getOrganizationById,
getUsersWithEmailsByUids,
} from 'Admin/utils/firebase'
import { concat } from 'lodash'
import { NextApiRequest, NextApiResponse } from 'next'

export default async function handler(
request: NextApiRequest,
response: NextApiResponse,
) {
try {
const { oid } = request.query

const user = await verifyUserSession(request)
const organization = await getOrganizationById(oid as string)

if (!user || !organization.owners?.includes(user.uid)) {
return response.status(401).json({ message: 'Unauthorized' })
}

if (!organization) {
return response
.status(404)
.json({ message: 'Organization not found' })
}

switch (request.method) {
case 'GET':
const members = await getUsersWithEmailsByUids(
concat(organization.owners, organization.editors ?? []),
)
return response.status(200).json({ members })
default:
return response
.status(405)
.json({ message: 'Method not allowed' })
}
} catch (e) {
if (e instanceof Error) {
response.status(400).json({ error: e.message })
}
}
}
54 changes: 54 additions & 0 deletions next-tavla/pages/api/organization/[oid]/members/invite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { TavlaError } from 'Admin/types/error'
import { verifyUserSession } from 'Admin/utils/auth'
import {
getUserByEmail,
initializeAdminApp,
inviteUserToOrganization,
userCanEditOrganization,
} from 'Admin/utils/firebase'
import { FeedbackCode } from 'hooks/useFormFeedback'
import { NextApiRequest, NextApiResponse } from 'next'
import { TOrganizationID } from 'types/settings'

initializeAdminApp()

export default async function handler(
request: NextApiRequest,
response: NextApiResponse,
) {
const withFeedback = (status: number, feedbackCode: FeedbackCode) => {
return response.status(status).json({ feedbackCode })
}

try {
const { oid } = request.query as { oid: TOrganizationID }
const { email } = JSON.parse(request.body) as {
email: string
}

const user = await verifyUserSession(request)
if (!user || !userCanEditOrganization(user.uid, oid))
return withFeedback(401, 'auth/not-allowed')

const inviteeId = (await getUserByEmail(email))?.uid
if (!inviteeId) {
return withFeedback(404, 'invite/user-not-found')
}

return await inviteUserToOrganization(inviteeId, oid)
.then(() => withFeedback(200, 'invite/success'))
.catch((e) => {
if (e instanceof TavlaError) {
switch (e.code) {
case 'ORGANIZATION':
return withFeedback(409, 'invite/already-invited')
}
}
return withFeedback(500, 'error')
})
} catch (e) {
if (e instanceof Error) {
return withFeedback(500, 'error')
}
}
}
File renamed without changes.
33 changes: 28 additions & 5 deletions next-tavla/pages/organizations/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { ToastProvider } from '@entur/alert'
import { IncomingNextMessage } from 'types/next'
import { verifyUserSession } from 'Admin/utils/auth'
import { AdminHeader } from 'Admin/components/AdminHeader'
import { TOrganizationID, TUserID } from 'types/settings'
import { getOrganizationById } from 'Admin/utils/firebase'
import { Heading1 } from '@entur/typography'
import { MemberAdministration } from 'Admin/scenarios/Organizations/components/MemberAdministration'

export async function getServerSideProps({
params,
Expand All @@ -14,30 +18,49 @@ export async function getServerSideProps({
}) {
const { id } = params

const loggedIn = (await verifyUserSession(req)) !== null
const user = await verifyUserSession(req)

if (!loggedIn)
if (!user)
return {
redirect: {
destination: '/#login',
permanent: false,
},
}

const organization = await getOrganizationById(id)
if (
!organization ||
!organization.id ||
!organization?.owners?.includes(user.uid)
)
return { notFound: true }

return {
props: {
id,
oid: organization.id,
uid: user.uid,
name: organization.name,
},
}
}

function EditOrganizationPage({ id }: { id: string }) {
function EditOrganizationPage({
oid,
uid,
name,
}: {
oid: TOrganizationID
uid: TUserID
name: string
}) {
return (
<div className={classes.root}>
<AdminHeader loggedIn />
<Contrast>
<ToastProvider>
<div>{id}</div>
<Heading1>{name}</Heading1>
<MemberAdministration uid={uid} oid={oid} />
</ToastProvider>
</Contrast>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Button, PrimaryButton } from '@entur/button'
import { Heading3, Paragraph } from '@entur/typography'
import { StopPlaceChip } from './StopPlaceChip'
import { Heading3, Heading4, Paragraph } from '@entur/typography'
import { TBoard } from 'types/settings'
import { useCreateBoardDispatch } from '../utils/context'
import { AddTile } from 'Admin/scenarios/Edit/components/AddTile'
import { TCreatePage } from 'Admin/types/createBoard'
import { useToast } from '@entur/alert'
import { StopPlaceList } from './StopPlaceList'

export function AddStops({
board,
Expand Down Expand Up @@ -46,20 +46,17 @@ export function AddStops({

return (
<div>
<Heading3>Legg til holdeplasser i Tavla </Heading3>
<Heading3>Legg til stoppesteder i Tavla </Heading3>
<Paragraph>
Søk etter stoppesteder og bestem om tavla skal vise alle
retninger, eller flere enkelte retninger.
</Paragraph>

<AddTile addTile={addTile} flexDirection="flexColumn" />
<div className="flexRow g-2 pt-2 pb-2">
{board.tiles.map((tile) => (
<StopPlaceChip tile={tile} key={tile.uuid} />
))}
</div>
<Heading4>Stoppesteder lagt til i Tavla</Heading4>
<StopPlaceList board={board} />
<div className="flexRow justifyBetween">
<Button variant="secondary" onClick={popPage}>
<Button className="w-30" variant="secondary" onClick={popPage}>
Tilbake
</Button>
<PrimaryButton onClick={nextStep}>Neste</PrimaryButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function CreateBoardButton({
}
}
return (
<PrimaryButton onClick={handleCreateBoard}>Opprett tavle</PrimaryButton>
<PrimaryButton className="w-30" onClick={handleCreateBoard}>
Opprett tavle
</PrimaryButton>
)
}

Expand Down
12 changes: 4 additions & 8 deletions next-tavla/src/Admin/scenarios/CreateBoard/components/Name.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PrimaryButton } from '@entur/button'
import { TextField } from '@entur/form'
import { Heading3, Paragraph } from '@entur/typography'
import { TCreatePage } from 'Admin/types/createBoard'
import { TBoard } from 'types/settings'
import { useCreateBoardDispatch } from '../utils/context'
import { selectInput } from 'Admin/utils/selectInput'
import { NextPage } from './NextPage'
function Name({
board,
pushPage,
Expand Down Expand Up @@ -33,13 +33,9 @@ function Name({
dispatch({ type: 'setTitle', title: e.target.value })
}}
/>
<PrimaryButton
onClick={() => {
pushPage('addStops')
}}
>
Neste
</PrimaryButton>
<div className="flexRow justifyEnd w-100">
<NextPage nextPage="addStops" pushPage={pushPage} />
</div>
</div>
)
}
Expand Down
18 changes: 18 additions & 0 deletions next-tavla/src/Admin/scenarios/CreateBoard/components/NextPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PrimaryButton } from '@entur/button'
import { TCreatePage } from 'Admin/types/createBoard'

function NextPage({
nextPage,
pushPage,
}: {
nextPage: TCreatePage
pushPage: (page: TCreatePage) => void
}) {
return (
<PrimaryButton className="w-30" onClick={() => pushPage(nextPage)}>
Neste
</PrimaryButton>
)
}

export { NextPage }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Paragraph } from '@entur/typography'
import { TBoard } from 'types/settings'
import { StopPlaceChip } from './StopPlaceChip'

function StopPlaceList({ board }: { board: TBoard }) {
if (board.tiles.length === 0) {
return (
<Paragraph>
Du har ikke lagt til noen stoppesteder i tavla enda.
</Paragraph>
)
}

return (
<div className="flexRow g-2">
{board.tiles.map((tile) => (
<StopPlaceChip tile={tile} key={tile.uuid} />
))}
</div>
)
}

export { StopPlaceList }
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function AddTile({
/>
<SearchableDropdown
items={stopPlaceItems}
label="Søk etter holdeplass..."
label="Søk etter stoppested..."
clearable
prepend={<SearchIcon />}
selectedItem={selectedStopPlace}
Expand All @@ -80,7 +80,7 @@ function AddTile({
onChange={setSelectedQuay}
/>
<Button variant="primary" onClick={handleAddTile}>
Legg til
Legg til stoppested
</Button>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classes from './styles.module.css'
import { Heading4 } from '@entur/typography'
import { TLinesFragment } from 'graphql/index'
import { transportModeNames } from './utils'
import { TransportIcon } from 'Board/scenarios/Table/components/TransportIcon'
import { TransportIcon } from 'components/TransportIcon'
import { useToggledLines } from './hooks/useToggledLines'
import { Checkbox } from '@entur/form'

Expand All @@ -29,7 +29,10 @@ function SelectLines({
{linesByMode.map(({ transportMode, lines }) => (
<div key={transportMode}>
<div className={classes.transportTitle}>
<TransportIcon transport={transportMode} />
<TransportIcon
transport={transportMode}
className={classes.icon}
/>
{transportModeNames[transportMode]}
</div>
<div className="flexRow alignCenter">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
padding: 0.35em 0.5em;
border-radius: 0.5em;
}

.icon {
height: 2.25em;
width: 2.25em;
}
2 changes: 1 addition & 1 deletion next-tavla/src/Admin/scenarios/Edit/hooks/useQuaySearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TDirectionType } from 'types/graphql-schema'
import { NormalizedDropdownItemType } from '@entur/dropdown'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { countBy } from 'lodash'
import { getTransportIcon } from 'Board/scenarios/Table/components/TransportIcon'
import { getTransportIcon } from 'components/TransportIcon'

function getPlatformLabel(
index: number,
Expand Down
Loading

0 comments on commit 1734f96

Please sign in to comment.