-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(create): add organization to create board modal (#1631)
* feat(create): add organization to create board modal * fix(create): disable org field if no org is present * fix(create): fetch latest organizations in dropdown * feat(create): own component for name and org selector in create board modal * refactor(create): own component for name and organization selector in create board modal * feat(edit): title on board can be 50 chars * refactor(create): check disabled organization to own variable * chore(create): handle form with useFormState * chore(create): error handling on create board modal * fix(crate): error handling in create and move duplicateBoard function to own actions file * fix(create): org set to null when private is selected * refactor(create): rename variable
- Loading branch information
Showing
7 changed files
with
178 additions
and
70 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
tavla/app/(admin)/components/CreateBoard/NameAndOrganizationSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use client' | ||
import { Dropdown } from '@entur/dropdown' | ||
import { TextField, Checkbox } from '@entur/form' | ||
import { AddIcon } from '@entur/icons' | ||
import { Heading3, Paragraph, Label } from '@entur/typography' | ||
import { useOrganizations } from 'app/(admin)/hooks/useOrganizations' | ||
import { getFormFeedbackForField } from 'app/(admin)/utils' | ||
import { HiddenInput } from 'components/Form/HiddenInput' | ||
import { SubmitButton } from 'components/Form/SubmitButton' | ||
import { useState } from 'react' | ||
import { useFormState } from 'react-dom' | ||
import { createBoard } from './actions' | ||
import { FormError } from '../FormError' | ||
|
||
function NameAndOrganizationSelector() { | ||
const [state, action] = useFormState(createBoard, undefined) | ||
|
||
const { organizations, selectedOrganization, setSelectedOrganization } = | ||
useOrganizations() | ||
|
||
const [isPersonal, setIsPersonal] = useState<boolean>(false) | ||
|
||
const disableOrg = isPersonal || organizations().length == 0 | ||
|
||
return ( | ||
<form action={action} className="md:px-10"> | ||
<Heading3>Velg navn og organisasjon for tavlen</Heading3> | ||
<Paragraph className="!mb-4"> | ||
Gi tavlen et navn og legg den til i en organisasjon. Velger du | ||
en organisasjon vil alle i organisasjonen ha tilgang til tavlen. | ||
</Paragraph> | ||
<Label>Gi tavlen et navn</Label> | ||
<TextField | ||
size="medium" | ||
label="Navn" | ||
id="name" | ||
name="name" | ||
maxLength={50} | ||
required | ||
{...getFormFeedbackForField('name', state)} | ||
/> | ||
|
||
<div className="mt-4"> | ||
<Label>Legg til i en organisasjon</Label> | ||
<Dropdown | ||
items={organizations} | ||
label="Dine organisasjoner" | ||
selectedItem={isPersonal ? null : selectedOrganization} | ||
onChange={setSelectedOrganization} | ||
clearable | ||
aria-required="true" | ||
className="mb-4" | ||
disabled={disableOrg} | ||
{...getFormFeedbackForField('organization', state)} | ||
/> | ||
<Checkbox | ||
checked={disableOrg} | ||
onChange={() => { | ||
setSelectedOrganization(null) | ||
setIsPersonal(!isPersonal) | ||
}} | ||
name="personal" | ||
> | ||
Privat tavle | ||
</Checkbox> | ||
<HiddenInput | ||
id="organization" | ||
value={selectedOrganization?.value.id} | ||
/> | ||
</div> | ||
<div className="mt-4"> | ||
<FormError {...getFormFeedbackForField('general', state)} /> | ||
</div> | ||
|
||
<div className="flex flex-row mt-8 justify-end"> | ||
<SubmitButton variant="primary" className="max-sm:w-full"> | ||
Opprett tavle | ||
<AddIcon /> | ||
</SubmitButton> | ||
</div> | ||
</form> | ||
) | ||
} | ||
|
||
export { NameAndOrganizationSelector } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tavla/app/(admin)/edit/[id]/components/DuplicateBoard/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use server' | ||
import { getOrganizationIfUserHasAccess } from 'app/(admin)/actions' | ||
import { getFormFeedbackForError } from 'app/(admin)/utils' | ||
import { initializeAdminApp } from 'app/(admin)/utils/firebase' | ||
import { getUserFromSessionCookie } from 'app/(admin)/utils/server' | ||
import admin, { firestore } from 'firebase-admin' | ||
import { redirect } from 'next/navigation' | ||
import { TBoard, TOrganization, TOrganizationID } from 'types/settings' | ||
|
||
initializeAdminApp() | ||
|
||
export async function duplicateBoard(board: TBoard, oid?: TOrganizationID) { | ||
const user = await getUserFromSessionCookie() | ||
if (!user) return getFormFeedbackForError('auth/operation-not-allowed') | ||
|
||
let organization: TOrganization | undefined | ||
if (oid) organization = await getOrganizationIfUserHasAccess(oid) | ||
|
||
const createdBoard = await firestore() | ||
.collection('boards') | ||
.add({ | ||
...board, | ||
meta: { | ||
...board.meta, | ||
fontSize: | ||
board.meta?.fontSize ?? | ||
organization?.defaults?.font ?? | ||
'medium', | ||
created: Date.now(), | ||
dateModified: Date.now(), | ||
}, | ||
}) | ||
|
||
firestore() | ||
.collection(oid ? 'organizations' : 'users') | ||
.doc(oid ? String(oid) : String(user.uid)) | ||
.update({ | ||
[oid ? 'boards' : 'owner']: admin.firestore.FieldValue.arrayUnion( | ||
createdBoard.id, | ||
), | ||
}) | ||
|
||
redirect(`/edit/${createdBoard.id}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters