Skip to content

Commit

Permalink
fix(edit): display name error handling (#1632)
Browse files Browse the repository at this point in the history
* fix(edit): display name error handling

* chore(edit): refactor tilecard error handling

* chore(edit): refactor

* fix(edit): error handling in server
  • Loading branch information
purusott authored Sep 5, 2024
1 parent 83bd687 commit 4ddb2bb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function saveTitle(bid: TBoardID, name: string) {
.collection('boards')
.doc(bid)
.update({
'meta.title': name.substring(0, 30),
'meta.title': name.substring(0, 50),
'meta.dateModified': Date.now(),
})
revalidatePath(`/edit/${bid}`)
Expand Down
11 changes: 10 additions & 1 deletion tavla/app/(admin)/edit/[id]/components/TileCard/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ export async function saveTile(bid: TBoardID, tile: TTile) {
'meta.dateModified': Date.now(),
})
const index = doc.tiles.indexOf(oldTile)
doc.tiles[index] = tile

if (tile.displayName !== undefined) {
doc.tiles[index] = {
...tile,
displayName: tile.displayName?.substring(0, 50),
}
} else {
doc.tiles[index] = tile
}

docRef.update({ tiles: doc.tiles, 'meta.dateModified': Date.now() })

revalidatePath(`/edit/${bid}`)
Expand Down
96 changes: 54 additions & 42 deletions tavla/app/(admin)/edit/[id]/components/TileCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ import { TransportModeAndLines } from './TransportModeAndLines'
import { deleteTile, getOrganizationForBoard, saveTile } from './actions'
import { useLines } from './useLines'
import { sortLineByPublicCode } from './utils'
import { isOnlyWhiteSpace } from 'app/(admin)/edit/utils'
import {
TFormFeedback,
getFormFeedbackForError,
getFormFeedbackForField,
} from 'app/(admin)/utils'
import { useFormState } from 'react-dom'

function TileCard({
bid,
Expand Down Expand Up @@ -78,6 +85,49 @@ function TileCard({

const [offset, setOffset] = useState(tile.offset ?? '')

const submit = async (
prevState: TFormFeedback | undefined,
data: FormData,
) => {
const columns = data.getAll('columns') as TColumn[]
data.delete('columns')
const count = data.get('count') as number | null
data.delete('count')
const distance = data.get('showDistance') as string
data.delete('showDistance')
const offset = data.get('offset') as number | null
data.delete('offset')
const displayName = data.get('displayName') as string
data.delete('displayName')
if (isOnlyWhiteSpace(displayName)) {
return getFormFeedbackForError('board/tiles-name-missing')
}

let lines: string[] = []
for (const line of data.values()) {
lines.push(line as string)
}
// If the length of lines equals all the lines, we don't want to include any
lines = lines.length == count ? [] : lines

if (columns !== tile.columns) await captureColumnChangeEvent()

const newTile = {
...tile,
columns: columns,
whitelistedLines: lines,
walkingDistance: {
visible: distance === 'on',
distance: tile.walkingDistance?.distance,
},
offset: Number(offset) || undefined,
displayName: displayName.substring(0, 50) || undefined,
} as TTile

bid === 'demo' ? saveTileToDemoBoard(newTile) : saveTile(bid, newTile)
reset()
}
const [state, action] = useFormState(submit, undefined)
useEffect(() => {
if (!address) {
setOffsetBasedOnWalkingDistance(false)
Expand All @@ -86,8 +136,8 @@ function TileCard({

const reset = () => {
setConfirmOpen(false)
setIsOpen(false)
setChanged(false)
setIsOpen(false)
}

const lines = useLines(tile)
Expand Down Expand Up @@ -213,47 +263,7 @@ function TileCard({
>
<form
id={tile.uuid}
action={async (data: FormData) => {
const columns = data.getAll('columns') as TColumn[]
data.delete('columns')
const count = data.get('count') as number | null
data.delete('count')
const distance = data.get('showDistance') as string
data.delete('showDistance')
const offset = data.get('offset') as number | null
data.delete('offset')
const displayName = data.get(
'displayName',
) as string
data.delete('displayName')

let lines: string[] = []
for (const line of data.values()) {
lines.push(line as string)
}
// If the length of lines equals all the lines, we don't want to include any
lines = lines.length == count ? [] : lines

if (columns !== tile.columns)
captureColumnChangeEvent()

const newTile = {
...tile,
columns: columns,
whitelistedLines: lines,
walkingDistance: {
visible: distance === 'on',
distance: tile.walkingDistance?.distance,
},
offset: Number(offset) || undefined,
displayName: displayName || undefined,
} as TTile

bid === 'demo'
? saveTileToDemoBoard(newTile)
: saveTile(bid, newTile)
}}
onSubmit={reset}
action={action}
onInput={() => setChanged(true)}
>
<div className="flex flex-col gap-2">
Expand All @@ -268,6 +278,8 @@ function TileCard({
className="!w-2/5"
name="displayName"
defaultValue={tile.displayName}
maxLength={50}
{...getFormFeedbackForField('name', state)}
/>
</div>
<Heading4>Gåavstand</Heading4>
Expand Down
5 changes: 5 additions & 0 deletions tavla/app/(admin)/edit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,8 @@ export function getIcons(layer?: string, category?: TCategory[]) {
export function isEmptyOrSpaces(str?: string) {
return str === undefined || str.match(/^ *$/) !== null
}
export function isOnlyWhiteSpace(str: string) {
if (str === undefined || str === '') return false

return str.trim() === ''
}
13 changes: 6 additions & 7 deletions tavla/app/(admin)/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export type TFormFeedback = {
}

export type TError = FirebaseError | string
export function getFormFeedbackForField(
form_type: InputType,
feedback?: TFormFeedback,
) {
if (form_type === feedback?.form_type) return feedback
}

export function getFormFeedbackForError(
e?: TError,
Expand Down Expand Up @@ -230,13 +236,6 @@ export function getFormFeedbackForError(
}
}

export function getFormFeedbackForField(
form_type: InputType,
feedback?: TFormFeedback,
) {
if (form_type === feedback?.form_type) return feedback
}

export function userInOrganization(
uid?: TUserID,
organization?: TOrganization,
Expand Down

0 comments on commit 4ddb2bb

Please sign in to comment.