Skip to content

Commit

Permalink
chore(error): move variable cheks into action-function for all settin…
Browse files Browse the repository at this point in the history
…gs, and move CountiesSelect out of MemberAdministration
  • Loading branch information
SelmaBergstrand committed Dec 3, 2024
1 parent a347a84 commit 1c0167c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 42 deletions.
13 changes: 7 additions & 6 deletions tavla/app/(admin)/edit/[id]/components/Footer/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import { handleError } from 'app/(admin)/utils/handleError'
import { firestore } from 'firebase-admin'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { TBoardID, TFooter } from 'types/settings'
import { TBoardID } from 'types/settings'

initializeAdminApp()

export async function setFooter(bid: TBoardID, footer?: TFooter) {
export async function setFooter(bid: TBoardID, data: FormData) {
const access = hasBoardEditorAccess(bid)
if (!access) return redirect('/')

const footerText = data.get('footer') as string
const override = (data.get('override') as string) === 'on'

try {
await firestore()
.collection('boards')
.doc(bid)
.update({
footer: {
footer: !isEmptyOrSpaces(footer?.footer)
? footer?.footer
: '',
override: footer?.override,
footer: !isEmptyOrSpaces(footerText) ? footerText : '',
override: override,
},
'meta.dateModified': Date.now(),
})
Expand Down
15 changes: 4 additions & 11 deletions tavla/app/(admin)/edit/[id]/components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ValidationInfoFilledIcon } from '@entur/icons'
import { Heading3 } from '@entur/typography'
import { SubmitButton } from 'components/Form/SubmitButton'
import { TBoardID, TFooter } from 'types/settings'
import { setFooter } from './actions'
import { setFooter as setFooterAction } from './actions'
import { useState } from 'react'
import { Tooltip } from '@entur/tooltip'
import ClientOnlyTextField from 'app/components/NoSSR/TextField'
Expand All @@ -23,20 +23,13 @@ function Footer({
const { addToast } = useToast()
const [override, setOverride] = useState(footer?.override ?? false)

const submitOrgBoard = async (data: FormData) => {
const footer = data.get('footer') as string
const override = data.get('override') as string
const overrideOrg = override === 'on'

const result = await setFooter(bid, {
footer: footer,
override: overrideOrg,
})
const setFooter = async (data: FormData) => {
const result = await setFooterAction(bid, data)
fireToastFeedback(addToast, result, 'Infomelding lagret!')
}

return (
<form className="box flex flex-col" action={submitOrgBoard}>
<form className="box flex flex-col" action={setFooter}>
<div className="flex flex-row items-center gap-2">
<Heading3 margin="bottom">Infomelding</Heading3>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export async function saveTitle(
}

export async function saveFont(bid: TBoardID, data: FormData) {
const font = data.get('font') as TFontSize

const access = await hasBoardEditorAccess(bid)
if (!access) return redirect('/')

const font = data.get('font') as TFontSize

try {
await firestore()
.collection('boards')
Expand Down
2 changes: 1 addition & 1 deletion tavla/app/(admin)/organizations/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Heading1, LeadParagraph } from '@entur/typography'
import { redirect } from 'next/navigation'
import { UploadLogo } from '../components/UploadLogo'
import { MemberAdministration } from '../components/MemberAdministration'
import { CountiesSelect } from '../components/MemberAdministration/CountiesSelect'
import { CountiesSelect } from '../components/CountiesSelect'
import { FontSelect } from '../components/FontSelect'
import { DefaultColumns } from '../components/DefaultColumns'
import { getOrganizationIfUserHasAccess } from 'app/(admin)/actions'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use server'
import { getFormFeedbackForError } from 'app/(admin)/utils'
import {
initializeAdminApp,
userCanEditOrganization,
Expand All @@ -7,20 +8,22 @@ import { handleError } from 'app/(admin)/utils/handleError'
import { firestore } from 'firebase-admin'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { TCountyID, TOrganizationID } from 'types/settings'
import { TOrganizationID } from 'types/settings'

initializeAdminApp()

export async function setCounties(
oid: TOrganizationID,
countiesList: TCountyID[],
oid: TOrganizationID | undefined,
data: FormData,
) {
if (!oid) return getFormFeedbackForError()
const access = userCanEditOrganization(oid)
if (!access) return redirect('/')
const counties = data.getAll('county') as string[]

try {
await firestore().collection('organizations').doc(oid).update({
'defaults.counties': countiesList,
'defaults.counties': counties,
})
revalidatePath(`/organizations/${oid}`)
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ function CountiesSelect({
const { addToast } = useToast()

const setCounties = async (data: FormData) => {
if (!oid) return
const counties = data.getAll('county') as string[]
const result = await setCountiesAction(oid, counties)
const result = await setCountiesAction(oid, data)
fireToastFeedback(addToast, result, 'Fylker lagret!')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ initializeAdminApp()

export async function saveColumns(
state: TFormFeedback | undefined,
oid: TOrganizationID,
columns: TColumn[],
oid: TOrganizationID | undefined,
data: FormData,
) {
if (!oid) return getFormFeedbackForError()
const access = userCanEditOrganization(oid)
if (!access) return redirect('/')

const columns = data.getAll('columns') as TColumn[]
if (columns.length === 0)
return getFormFeedbackForError('organization/invalid-columns')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ function DefaultColumns({
state: TFormFeedback | undefined,
data: FormData,
) => {
if (!oid) return
const columns = data.getAll('columns') as TColumn[]
const result = await saveColumnsAction(state, oid, columns)
const result = await saveColumnsAction(state, oid, data)
if (result === undefined) {
addToast('Kolonner lagret!')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use server'
import { getFormFeedbackForError } from 'app/(admin)/utils'
import {
initializeAdminApp,
userCanEditOrganization,
Expand All @@ -12,7 +13,12 @@ import { TOrganizationID } from 'types/settings'

initializeAdminApp()

export async function setFontSize(oid: TOrganizationID, fontSize: TFontSize) {
export async function setFontSize(
oid: TOrganizationID | undefined,
data: FormData,
) {
if (!oid) return getFormFeedbackForError()
const fontSize = data.get('font') as TFontSize
const access = await userCanEditOrganization(oid)
if (!access) return redirect('/')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ function FontSelect({
const { addToast } = useToast()

const setFontSize = async (data: FormData) => {
if (!oid) return
const font = data.get('font') as TFontSize
const result = await setFontSizeAction(oid, font)
const result = await setFontSizeAction(oid, data)
fireToastFeedback(addToast, result, 'Tekststørrelse lagret!')
}

Expand Down
9 changes: 8 additions & 1 deletion tavla/app/(admin)/organizations/components/Footer/actions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
'use server'
import { isEmptyOrSpaces } from 'app/(admin)/edit/utils'
import { getFormFeedbackForError } from 'app/(admin)/utils'
import { userCanEditOrganization } from 'app/(admin)/utils/firebase'
import { handleError } from 'app/(admin)/utils/handleError'
import { firestore } from 'firebase-admin'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { TOrganizationID } from 'types/settings'

export async function setFooter(oid: TOrganizationID, message?: string) {
export async function setFooter(
oid: TOrganizationID | undefined,
data: FormData,
) {
if (!oid) return getFormFeedbackForError()
const access = await userCanEditOrganization(oid)
if (!access) return redirect('/')

const message = data.get('footer') as string

try {
await firestore()
.collection('organizations')
Expand Down
8 changes: 3 additions & 5 deletions tavla/app/(admin)/organizations/components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import { fireToastFeedback } from 'app/(admin)/utils'
function Footer({ oid, footer }: { oid?: TOrganizationID; footer?: string }) {
const { addToast } = useToast()

const setFooter = async (data: FormData) => {
if (!oid) return
const message = data.get('footer') as string
const result = await setFooterAction(oid, message)
const setOrgFooter = async (data: FormData) => {
const result = await setFooterAction(oid, data)
fireToastFeedback(addToast, result, 'Infomelding lagret!')
}

return (
<form className="box flex flex-col gap-1 " action={setFooter}>
<form className="box flex flex-col gap-1 " action={setOrgFooter}>
<Heading2>Infomelding</Heading2>
<Paragraph>
Skriv en kort tekst som skal vises nederst i tavlen.
Expand Down

0 comments on commit 1c0167c

Please sign in to comment.