Skip to content

Commit

Permalink
fix: misc data portal fixes/tweaks (#1203)
Browse files Browse the repository at this point in the history
* switch out "published" for "notVisitable"

* sonar issues / cleanup

* notify on save & close drawer

* cleanup interface

* show option to add when none exist in edit mode

* fix phone number display in field for init data

* whoops, better add that back..

* cleanup sonarlint issues

* cleanup sonarlint issues

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
JoeKarow and kodiakhq[bot] authored Apr 4, 2024
1 parent b7bfa8e commit 54c5a3b
Show file tree
Hide file tree
Showing 12 changed files with 532 additions and 436 deletions.
4 changes: 2 additions & 2 deletions packages/api/router/location/mutation.update.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export const update = async ({ ctx, input }: TRPCHandlerParams<TUpdateSchema, 'p
const prisma = getAuditedClient(ctx.actorId)
const { where, data } = input

const update = await prisma.orgLocation.update({
const result = await prisma.orgLocation.update({
where,
data,
select: { id: true },
})

return update
return result
}
export default update
46 changes: 24 additions & 22 deletions packages/api/router/location/mutation.update.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const ZUpdateSchema = z
countryId: prefixedId('country').nullable(),
govDistId: prefixedId('govDist').nullable(),
services: z.string().array(),
notVisitable: z.boolean(),
})
.partial(),
})
Expand All @@ -40,32 +41,33 @@ export const ZUpdateSchema = z

const updateAccessibility = accessible?.boolean !== undefined && accessibleAttrId

const attributes =
accessible?.boolean !== null
? {
attributes: {
upsert: {
where: { id: accessible?.supplementId ?? '' },
create: {
attribute: { connect: { id: accessibleAttrId } },
boolean: accessible?.boolean,
},
update: {
boolean: accessible?.boolean,
},
},
},
}
: {
attributes: {
delete: { id: accessible.supplementId ?? '' },
},
}

return Prisma.validator<Prisma.OrgLocationUpdateArgs>()({
where: { id },
data: {
...rest,
...(updateAccessibility
? accessible.boolean !== null
? {
attributes: {
upsert: {
where: { id: accessible.supplementId ?? '' },
create: {
attribute: { connect: { id: accessibleAttrId } },
boolean: accessible.boolean,
},
update: {
boolean: accessible.boolean,
},
},
},
}
: {
attributes: {
delete: { id: accessible.supplementId ?? '' },
},
}
: {}),
...(updateAccessibility ? attributes : {}),
...(countryId
? {
country: { connect: { id: countryId } },
Expand Down
1 change: 1 addition & 0 deletions packages/api/router/location/query.getAddress.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const getAddress = async ({ input }: TRPCHandlerParams<TGetAddressSchema>
longitude: true,
mailOnly: true,
published: true,
notVisitable: true,
services: { select: { serviceId: true } },
},
})
Expand Down
42 changes: 14 additions & 28 deletions packages/api/router/orgPhone/query.forEditDrawer.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,17 @@ import { type TRPCHandlerParams } from '~api/types/handler'
import { type TForEditDrawerSchema } from './query.forEditDrawer.schema'

const getOrgId = async (phoneId: string) => {
const result = await prisma.orgPhone.findUniqueOrThrow({
where: { id: phoneId },
select: {
organization: { select: { organizationId: true } },
locations: { select: { location: { select: { orgId: true } } } },
services: { select: { service: { select: { organizationId: true } } } },
const org = await prisma.organization.findFirstOrThrow({
where: {
OR: [
{ phones: { some: { phoneId } } },
{ locations: { some: { phones: { some: { phoneId } } } } },
{ services: { some: { phones: { some: { orgPhoneId: phoneId } } } } },
],
},
select: { id: true },
})

switch (true) {
case !!result.organization?.organizationId: {
return result.organization.organizationId
}
case result.locations.length !== 0 &&
result.locations.filter((loc) => !!loc.location.orgId).length !== 0: {
const filtered = result.locations.filter((loc) => !!loc.location.orgId)
return filtered[0]!.location.orgId
}
case result.services.length !== 0 &&
result.services.filter((serv) => !!serv.service.organizationId).length !== 0: {
const filtered = result.services.filter((serv) => !!serv.service.organizationId)
return filtered[0]!.service.organizationId!
}
default: {
throw new Error('Unable to get organizationId')
}
}
return org.id
}

export const forEditDrawer = async ({ input }: TRPCHandlerParams<TForEditDrawerSchema>) => {
Expand All @@ -55,7 +39,9 @@ export const forEditDrawer = async ({ input }: TRPCHandlerParams<TForEditDrawerS
country: { select: { cca2: true } },
},
})
if (!result) return null
if (!result) {
return null
}
const orgId = await getOrgId(input.id)
const { country, description, number, ext, ...rest } = result

Expand All @@ -68,7 +54,7 @@ export const forEditDrawer = async ({ input }: TRPCHandlerParams<TForEditDrawerS

return {
...rest,
number: parsedPhone.formatNational(),
number: parsedPhone.format('E.164'),
ext,
description: description ? description?.tsKey?.text : null,
orgId,
Expand All @@ -85,7 +71,7 @@ export const forEditDrawer = async ({ input }: TRPCHandlerParams<TForEditDrawerS
}
}
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default forEditDrawer
50 changes: 37 additions & 13 deletions packages/ui/components/data-display/Hours.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createStyles, List, rem, Skeleton, Stack, Table, Text, Title } from '@mantine/core'
import { createStyles, Group, List, rem, Skeleton, Stack, Table, Text, Title } from '@mantine/core'
import { Interval } from 'luxon'
import { useTranslation } from 'next-i18next'
import { type TFunction, useTranslation } from 'next-i18next'

import { type ApiOutput } from '@weareinreach/api'
import { HoursDrawer } from '~ui/components/data-portal/HoursDrawer'
import { useCustomVariant } from '~ui/hooks/useCustomVariant'
import { useLocalizedDays } from '~ui/hooks/useLocalizedDays'
import { Icon } from '~ui/icon'
import { trpc as api } from '~ui/lib/trpcClient'

const labelKeys = {
Expand Down Expand Up @@ -40,6 +41,15 @@ const nullObj = {
6: [],
}

const formatHourLine = ({ closed, interval, t }: FormatHourLineProps) => {
if (closed) {
return t('hours.closed')
}
return interval.toDuration('hours').valueOf() === OPEN_24_MILLISECONDS
? t('hours.open24')
: interval.toFormat('hh:mm a')
}

export const Hours = ({ parentId, label = 'regular', edit, data: passedData }: HoursProps) => {
const { t, i18n } = useTranslation('common')
const variants = useCustomVariant()
Expand All @@ -48,31 +58,29 @@ export const Hours = ({ parentId, label = 'regular', edit, data: passedData }: H
? { data: passedData, isLoading: false }
: api.orgHours.forHoursDisplay.useQuery(parentId)
const dayMap = useLocalizedDays(i18n.resolvedLanguage)
if (!data && !isLoading) return null
if (!data && !isLoading && !edit) {
return null
}

const labelKey = labelKeys[label]
const timezone: string | null = null

const hourTable = Object.entries(data ?? nullObj).map(([dayIdx, data]) => {
const hourTable = Object.entries(data ?? nullObj).map(([dayIdx, hourRecord]) => {
return (
<tr key={dayIdx}>
<td className={classes.dow}>
<Text>{dayMap.get(parseInt(dayIdx))}</Text>
</td>
<td className={classes.hours}>
<List listStyleType='none'>
{data.map(({ id, interval: intervalISO, closed }) => {
{hourRecord.map(({ id, interval: intervalISO, closed }) => {
const interval = Interval.fromISO(intervalISO)

const textToDisplay = formatHourLine({ interval, closed, t })

return (
<List.Item key={id}>
<Text>
{closed
? t('hours.closed')
: interval.toDuration('hours').valueOf() === OPEN_24_MILLISECONDS
? t('hours.open24')
: interval.toFormat('hh:mm a')}
</Text>
<Text>{textToDisplay}</Text>
</List.Item>
)
})}
Expand All @@ -82,6 +90,16 @@ export const Hours = ({ parentId, label = 'regular', edit, data: passedData }: H
)
})

const body =
edit && !data ? (
<Group noWrap>
<Icon icon='carbon:add-filled' />
<Text>Add opening hours</Text>
</Group>
) : (
<tbody>{hourTable}</tbody>
)

return (
<Skeleton visible={isLoading}>
<Stack spacing={12}>
Expand All @@ -92,7 +110,7 @@ export const Hours = ({ parentId, label = 'regular', edit, data: passedData }: H
{edit ? (
<Table>
<HoursDrawer locationId={parentId} component='a'>
<tbody>{hourTable}</tbody>
{body}
</HoursDrawer>
</Table>
) : (
Expand All @@ -111,3 +129,9 @@ export interface HoursProps {
edit?: boolean
data?: ApiOutput['orgHours']['forHoursDisplay']
}

interface FormatHourLineProps {
closed: boolean
interval: Interval
t: TFunction
}
Loading

0 comments on commit 54c5a3b

Please sign in to comment.