Skip to content

Commit

Permalink
fix: valid json for empty favorite deletes. (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
morganney authored Dec 12, 2023
1 parent e578cc7 commit e77efce
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
15 changes: 11 additions & 4 deletions packages/api/src/handlers/favorite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import {
} from '../queries/favorite.js'

import type { Request, Response } from 'express'
import type { Favorite, RiderFavoriteItem } from '@busmap/common/types/favorites'
import type { RiderFavorite } from '../types.js'
import type {
Favorite,
RiderFavorite,
RiderFavoriteItem
} from '@busmap/common/types/favorites'
import type { HttpError } from 'http-errors'

const debug = makeDebug('busmap')
Expand Down Expand Up @@ -57,8 +60,12 @@ const favorite = {
const removed = await removeRiderFavorite(req.session.userId, favorite)

debug('removed rider favorite', removed)

return res.json(removed[0])
/**
* Guard against returning `undefined`
* if `removed` is an empty array because
* no rows were actually deleted.
*/
return res.json(removed[0] ?? null)
} catch (err) {
return res.status(500).json(new errors.InternalServerError())
}
Expand Down
7 changes: 5 additions & 2 deletions packages/api/src/queries/favorite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { sql } from '../db.js'

import type { Favorite, RiderFavoriteRow } from '@busmap/common/types/favorites'
import type { RiderFavorite } from '../types.js'
import type {
Favorite,
RiderFavorite,
RiderFavoriteRow
} from '@busmap/common/types/favorites'

const addRiderFavorite = async (favorite: Favorite, userId: number) => {
const { agency, route, stop } = favorite
Expand Down
8 changes: 1 addition & 7 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
interface RiderFavorite {
rider: number
favorite: number
rank: number
created: string
}
interface AddRiderFavorite {
created: string
}

export type { RiderFavorite, AddRiderFavorite }
export type { AddRiderFavorite }
8 changes: 7 additions & 1 deletion packages/common/src/types/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ interface Favorite {

// API models

interface RiderFavorite {
rider: number
favorite: number
rank: number
created: string
}
interface RiderFavoriteRow {
created: string
rank: number
Expand All @@ -29,4 +35,4 @@ interface RiderFavoriteItem {
favorite: Favorite
}

export type { RouteMeta, Favorite, RiderFavoriteRow, RiderFavoriteItem }
export type { RouteMeta, Favorite, RiderFavorite, RiderFavoriteRow, RiderFavoriteItem }
3 changes: 2 additions & 1 deletion packages/ui/src/modules/favorites/api/delete.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { transport } from '@core/api/transport.js'
import { errors } from '@core/api/errors.js'

import type { RiderFavorite } from '@busmap/common/types/favorites'
import type { Favorite } from '../types.js'

const remove = async (favorite: Favorite) => {
if (!favorite) {
throw errors.create('GET', 400, 'Bad Request')
}

const resp = await transport.fetch('/favorite/remove', {
const resp = await transport.fetch<RiderFavorite>('/favorite/remove', {
method: 'DELETE',
body: JSON.stringify({
favorite
Expand Down
7 changes: 5 additions & 2 deletions packages/ui/src/modules/favorites/components/favoriteStop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ const FavoriteStop: FC<FavoriteStopProps> = ({ selection, size = 'medium' }) =>

if (user) {
try {
await removal.mutateAsync(favorite)
toast({ type: 'info', message: 'Favorite removed.' })
const removed = await removal.mutateAsync(favorite)

if (removed) {
toast({ type: 'info', message: 'Favorite removed.' })
}
} catch (err) {
toast({ type: 'error', message: 'Error removing favorite.' })
}
Expand Down
7 changes: 5 additions & 2 deletions packages/ui/src/modules/favorites/components/favorites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ const Favorites = memo(function Favorites() {

if (user) {
try {
await remove(fav)
toast({ type: 'info', message: 'Favorite removed.' })
const removed = await remove(fav)

if (removed) {
toast({ type: 'info', message: 'Favorite removed.' })
}
} catch (err) {
toast({ type: 'error', message: 'Error removing favorite.' })
}
Expand Down

0 comments on commit e77efce

Please sign in to comment.