Skip to content

Commit

Permalink
add validation to most apis, drop roles from edit form, add neighborh…
Browse files Browse the repository at this point in the history
…ood input to all profile apis/forms
  • Loading branch information
lyoshenka committed Mar 26, 2024
1 parent f7de57d commit b7624e9
Show file tree
Hide file tree
Showing 40 changed files with 426 additions and 334 deletions.
4 changes: 2 additions & 2 deletions components/directory/CensusView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useProfile } from '../auth/useProfile'
import { List } from '../core/List'
import { useBackend } from '@/components/hooks/useBackend'
import {
ProfileListParams,
ProfileListParamsType,
ProfileListResponse,
ProfileSort,
ProfileListFragment,
Expand All @@ -48,7 +48,7 @@ export const CensusView = () => {
const { user } = useProfile({ redirectTo: '/' })
const { useGetPaginated } = useBackend()

const input = useMemo<ProfileListParams>(() => {
const input = useMemo<ProfileListParamsType>(() => {
// Only search if there are at least 2 characters
const searchQuery = searchValue.length >= 2 ? searchValue : ''

Expand Down
4 changes: 2 additions & 2 deletions components/landing/NeighborhoodShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Link from 'next/link'
import Image from 'next/image'
import { useBackend } from '@/components/hooks/useBackend'
import {
LocationListParams,
LocationListParamsType,
LocationListResponse,
} from '@/utils/types/location'
import { getImageUrlByIpfsHash } from '@/lib/image'
Expand All @@ -15,7 +15,7 @@ export const NeighborhoodShowcase = () => {
const { useGet } = useBackend()
const { data } = useGet<LocationListResponse>('LOCATION_LIST', {
sort: 'votesDesc',
} as LocationListParams)
} as LocationListParamsType)

const locations = !data || 'error' in data ? [] : data.locations.slice(0, 4)

Expand Down
4 changes: 2 additions & 2 deletions components/landing/NeighborhoodsTop6List.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from 'next/link'
import { useBackend } from '@/components/hooks/useBackend'
import {
LocationListParams,
LocationListParamsType,
LocationListResponse,
LocationType,
} from '@/utils/types/location'
Expand All @@ -17,7 +17,7 @@ export const NeighborhoodsTop6List = () => {
'LOCATION_LIST',
{
locationType: LocationType.Neighborhood,
} as LocationListParams
} as LocationListParamsType
)
const { voteForLocation } = useLocationVote(refetchLocations)

Expand Down
4 changes: 2 additions & 2 deletions components/neighborhoods/LocationsByVoteCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from 'react'
import InfiniteScroll from 'react-infinite-scroll-component'
import {
LocationFragment,
LocationListParams,
LocationListParamsType,
LocationListResponse,
} from '@/utils/types/location'
import { useLocationVote } from '../hooks/useLocationVote'
Expand All @@ -21,7 +21,7 @@ export const LocationsByVoteCount = () => {
mutate: refetchLocations,
} = useGetPaginated<LocationListResponse>('LOCATION_LIST', {
sort: 'votesDesc',
} as LocationListParams)
} as LocationListParamsType)

const locations = data
? data.reduce(
Expand Down
9 changes: 6 additions & 3 deletions components/neighborhoods/edit-location/BasicDetailStep.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React, { useState } from 'react'
import { useUpdateLocation } from '../useUpdateLocation'
import { AddressFragmentType, LocationEditParams } from '@/utils/types/location'
import {
AddressFragmentType,
LocationEditParamsType,
} from '@/utils/types/location'
import { useError } from '@/components/hooks/useError'
import {
MAX_LOCATION_BIO_LENGTH,
Expand Down Expand Up @@ -43,7 +46,7 @@ export const BasicDetailStep = ({

const { showError } = useError()

const [locationInput, setLocationInput] = useState<LocationEditParams>({
const [locationInput, setLocationInput] = useState<LocationEditParamsType>({
name: location.name,
caretakerEmail: location.caretakerEmail,
tagline: location.tagline,
Expand All @@ -57,7 +60,7 @@ export const BasicDetailStep = ({

const handleOnChange = (
e: React.ChangeEvent<HTMLInputElement>,
field: keyof LocationEditParams
field: keyof LocationEditParamsType
) => {
const intFields = ['sleepCapacity', 'internetSpeedMbps']

Expand Down
4 changes: 2 additions & 2 deletions components/neighborhoods/edit-location/DescriptionStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
REQUIRED_FIELD_ERROR,
} from '@/utils/validate'
import { useError } from '@/components/hooks/useError'
import { LocationEditParams } from '@/utils/types/location'
import { LocationEditParamsType } from '@/utils/types/location'

export const DescriptionStep = ({
name,
Expand All @@ -22,7 +22,7 @@ export const DescriptionStep = ({
steps,
}: StepProps) => {
const { updateLocation } = useUpdateLocation(location.externId)
const [locationInput, setLocationInput] = useState<LocationEditParams>({
const [locationInput, setLocationInput] = useState<LocationEditParamsType>({
description: location.description,
})

Expand Down
4 changes: 2 additions & 2 deletions components/neighborhoods/edit-location/OffersStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRouter } from 'next/router'
import { useBackend } from '@/components/hooks/useBackend'
import {
OfferType,
OfferListParams,
OfferListParamsType,
OfferListResponse,
OfferNewParams,
OfferNewResponse,
Expand Down Expand Up @@ -36,7 +36,7 @@ export const OffersStep = ({
location ? 'OFFER_LIST' : null,
{
locationId: location.externId,
} as OfferListParams
} as OfferListParamsType
)
const offerList =
!offersData || 'error' in offersData ? [] : offersData.offers
Expand Down
4 changes: 2 additions & 2 deletions components/neighborhoods/edit-location/PhotoGalleryStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useError } from '@/components/hooks/useError'
import { useUpdateLocation } from '../useUpdateLocation'
import { FileNameIpfsHashMap } from '@/lib/file-storage/types'
import {
LocationEditParams,
LocationEditParamsType,
LocationMediaCategory,
} from '@/utils/types/location'
import {
Expand Down Expand Up @@ -35,7 +35,7 @@ export const PhotoGalleryStep = ({
[LocationMediaCategory.Features]: false,
})

const [locationInput, setLocationInput] = useState<LocationEditParams>({
const [locationInput, setLocationInput] = useState<LocationEditParamsType>({
bannerImageIpfsHash: location.bannerImageIpfsHash,
mediaItems: location.mediaItems?.map((mediaItem) => ({
ipfsHash: mediaItem?.ipfsHash,
Expand Down
4 changes: 2 additions & 2 deletions components/neighborhoods/useUpdateLocation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useBackend } from '@/components/hooks/useBackend'
import {
LocationEditParams,
LocationEditParamsType,
LocationEditResponse,
} from '@/utils/types/location'

Expand All @@ -10,7 +10,7 @@ export function useUpdateLocation(locationId: string | undefined) {
locationId ? ['LOCATION', { externId: locationId }] : null
)

const updateLocation = async (inputData: LocationEditParams = {}) => {
const updateLocation = async (inputData: LocationEditParamsType = {}) => {
if (locationId) {
const data = await mutateLocation(inputData)

Expand Down
8 changes: 4 additions & 4 deletions components/neighborhoods/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
truthyString,
} from '@/utils/validate'
import { emptyEditorValue } from '../core/slate/slate-utils'
import { OfferEditParams, OfferType } from '@/utils/types/offer'
import { LocationEditParams } from '@/utils/types/location'
import { OfferEditParamsType, OfferType } from '@/utils/types/offer'
import { LocationEditParamsType } from '@/utils/types/location'

export type ValidationType = 'missing' | 'invalid' | 'valid'

Expand All @@ -17,7 +17,7 @@ type ValidationResult = {
valid: boolean
}

export const validateLocationInput = (values: LocationEditParams) => {
export const validateLocationInput = (values: LocationEditParamsType) => {
const {
name,
tagline,
Expand Down Expand Up @@ -45,7 +45,7 @@ export const validateLocationInput = (values: LocationEditParams) => {

export const validateOfferInput = (
type: OfferType,
newValues: OfferEditParams
newValues: OfferEditParamsType
) => {
const invalid =
!validateTitle(newValues.title).valid ||
Expand Down
6 changes: 3 additions & 3 deletions components/offers/EditOfferPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useModal } from '../hooks/useModal'
import { useNavigation } from '../hooks/useNavigation'
import { useGetOffer } from './useGetOffer'
import { useBackend } from '@/components/hooks/useBackend'
import { OfferEditParams, OfferType } from '@/utils/types/offer'
import { OfferEditParamsType, OfferType } from '@/utils/types/offer'
import { SingleColumnLayout } from '../layouts/SingleColumnLayout'
import { EditOfferView } from './EditOfferView'
import { DiscardChangesModal } from '../core/DiscardChangesModal'
Expand Down Expand Up @@ -34,7 +34,7 @@ export const EditOfferPageView = () => {

const [highlightErrors, setHighlightErrors] = useState(false)
const [unsavedChanges, setUnsavedChanges] = useState(false)
const [newValues, setNewValues] = useState<OfferEditParams>({})
const [newValues, setNewValues] = useState<OfferEditParamsType>({})

useEffect(() => {
if (offer) {
Expand Down Expand Up @@ -78,7 +78,7 @@ export const EditOfferPageView = () => {
}
}

const handleOnEdit = (updateOfferInput: OfferEditParams) => {
const handleOnEdit = (updateOfferInput: OfferEditParamsType) => {
setUnsavedChanges(true)

setNewValues((prev) => ({
Expand Down
8 changes: 4 additions & 4 deletions components/offers/EditOfferView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react'
import { useRouter } from 'next/router'
import { useModal } from '../hooks/useModal'
import { OfferEditParams, OfferFragment } from '@/utils/types/offer'
import { OfferEditParamsType, OfferFragment } from '@/utils/types/offer'
import styled from 'styled-components'
import { ContentCard } from '../core/ContentCard'
import { TitleCard } from '../core/TitleCard'
Expand All @@ -10,8 +10,8 @@ import { DiscardChangesModal } from '../core/DiscardChangesModal'

interface EditOfferViewProps {
offer: OfferFragment
updateOfferInput: OfferEditParams
onEdit: (updateOfferInput: OfferEditParams) => void
updateOfferInput: OfferEditParamsType
onEdit: (updateOfferInput: OfferEditParamsType) => void
highlightErrors?: boolean
}

Expand All @@ -25,7 +25,7 @@ export const EditOfferView = ({
const { showModal } = useModal()
const router = useRouter()

const handleEdit = (updateOfferInput: OfferEditParams) => {
const handleEdit = (updateOfferInput: OfferEditParamsType) => {
setUnsavedChanges(true)
onEdit(updateOfferInput)
}
Expand Down
4 changes: 2 additions & 2 deletions components/offers/OfferTabList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useProfile } from '../auth/useProfile'
import { useBackend } from '@/components/hooks/useBackend'
import {
OfferFragment,
OfferListParams,
OfferListParamsType,
OfferListResponse,
OfferType,
} from '@/utils/types/offer'
Expand All @@ -19,7 +19,7 @@ export const OfferTabList = ({ offerType }: { offerType?: OfferType }) => {
useGetPaginated<OfferListResponse>('OFFER_LIST', {
offerType: offerType ?? undefined,
publiclyVisibleOnly: 'true',
} as OfferListParams)
} as OfferListParamsType)

const offers = data
? data.reduce(
Expand Down
10 changes: 7 additions & 3 deletions components/offers/edit-offer/EditOfferForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { useState } from 'react'
import { useRouter } from 'next/router'
import { useModal } from '@/components/hooks/useModal'
import { OfferEditParams, OfferFragment, OfferType } from '@/utils/types/offer'
import {
OfferEditParamsType,
OfferFragment,
OfferType,
} from '@/utils/types/offer'
import {
MAX_OFFER_TITLE_LENGTH,
PHOTO_UPLOAD_INSTRUCTIONS,
Expand Down Expand Up @@ -32,8 +36,8 @@ import { useBackend } from '@/components/hooks/useBackend'

interface EditOfferFormProps {
offer: OfferFragment
updateOfferInput: OfferEditParams
onEdit: (updateOfferInput: OfferEditParams) => void
updateOfferInput: OfferEditParamsType
onEdit: (updateOfferInput: OfferEditParamsType) => void
highlightErrors?: boolean
}

Expand Down
10 changes: 5 additions & 5 deletions components/offers/edit-offer/Pricing.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeEvent } from 'react'
import { OfferEditParams, OfferPriceInterval } from '@/utils/types/offer'
import { OfferEditParamsType, OfferPriceInterval } from '@/utils/types/offer'
import { REQUIRED_FIELD_ERROR } from '@/utils/validate'
import styled from 'styled-components'
import { SelectOption } from '@/components/hooks/useDropdownLogic'
Expand Down Expand Up @@ -30,11 +30,11 @@ const options = Object.values(OfferPriceInterval).map((interval) => ({
}))

interface PricingProps {
price?: OfferEditParams['price']
priceInterval?: OfferEditParams['priceInterval']
price?: OfferEditParamsType['price']
priceInterval?: OfferEditParamsType['priceInterval']
onPriceChange?: (
price: OfferEditParams['price'],
priceInterval: OfferEditParams['priceInterval']
price: OfferEditParamsType['price'],
priceInterval: OfferEditParamsType['priceInterval']
) => void
highlightErrors?: boolean
}
Expand Down
10 changes: 10 additions & 0 deletions components/profile/AboutInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AvatarFragmentType } from '@/utils/types/profile'
import { LocationAutocompleteInput } from '@/components/core/LocationAutocompleteInput'
import { ADDRESS_ERROR } from '@/utils/validate'
import { AddressFragmentType } from '@/utils/types/location'
import { NeighborhoodSelect } from '@/components/profile/RegistrationForm'

interface AboutInputProps {
bio: string
Expand All @@ -16,6 +17,8 @@ interface AboutInputProps {
onAddressChange: (location: AddressFragmentType) => void
avatar?: AvatarFragmentType
onAvatarChange?: (avatar: AvatarFragmentType | undefined) => void
neighborhoodExternId: string | null | undefined
onNeighborhoodChange: (n: string | null | undefined) => void
}

export const AboutInput = ({
Expand All @@ -25,6 +28,8 @@ export const AboutInput = ({
onAddressChange,
avatar,
onAvatarChange,
neighborhoodExternId,
onNeighborhoodChange,
}: AboutInputProps) => {
const handleBioChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
onBioChange(e.target.value)
Expand All @@ -51,6 +56,11 @@ export const AboutInput = ({
error={!isValidAddress(address)}
errorMessage={ADDRESS_ERROR}
/>
<NeighborhoodSelect
address={address}
neighborhoodExternId={neighborhoodExternId}
onNeighborhoodChange={onNeighborhoodChange}
/>
</SetupStepContainer>
)
}
Expand Down
8 changes: 5 additions & 3 deletions components/profile/AvatarSetup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react'
import { Network, OwnedNft } from 'alchemy-sdk'
import { ProfileEditParams } from '@/utils/types/profile'
import { ProfileEditParamsType } from '@/utils/types/profile'
import { getImageUrlFromNft } from '@/lib/image'
import { FileNameIpfsHashMap } from '@/lib/file-storage/types'
import { getImageUrlByIpfsHash } from '@/lib/image'
Expand All @@ -16,8 +16,10 @@ export type ExtendedOwnedNft = OwnedNft & {
}

interface AvatarSetupProps {
onNftSelected: (nft: ProfileEditParams['data']['avatar'] | undefined) => void
avatar?: ProfileEditParams['data']['avatar'] | undefined | null
onNftSelected: (
nft: ProfileEditParamsType['data']['avatar'] | undefined
) => void
avatar?: ProfileEditParamsType['data']['avatar'] | undefined | null
}

export const AvatarSetup = ({ onNftSelected, avatar }: AvatarSetupProps) => {
Expand Down
6 changes: 3 additions & 3 deletions components/profile/ContactInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ContactFieldType,
ContactFragment,
ContactFragmentType,
MeFragment,
} from '@/utils/types/profile'
import { SetStateAction, useEffect } from 'react'
Expand All @@ -23,8 +23,8 @@ const contactOptions = Object.values(ContactFieldType).map(

interface ContactInputProps {
profile: MeFragment
contactList: ContactFragment[]
setContactList: (contactList: SetStateAction<ContactFragment[]>) => void
contactList: ContactFragmentType[]
setContactList: (contactList: SetStateAction<ContactFragmentType[]>) => void
}

export const ContactInput = ({
Expand Down
Loading

0 comments on commit b7624e9

Please sign in to comment.