Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracks companies for which the privacy policy was accepted by guest #13

Merged
merged 10 commits into from
May 20, 2020
7 changes: 7 additions & 0 deletions __tests__/checkin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ beforeEach(() => {
})

it('shows onboarding the first time', async () => {
fetchMock.get(`path:/areas/${areaId}`, {
id: areaId,
name: 'Test Tisch',
menuLink: null,
companyId: 'som-uuid',
})

render(withTestRouter(<Checkin />, route))
await screen.findByLabelText('Name')
await screen.findByLabelText('Telefon')
Expand Down
3 changes: 2 additions & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ type AreaPost = {
companyId: string
}

type AreaResponse = {
export type AreaResponse = {
id: string
name: string
menuLink?: string
companyId: string
}

const api = ky.create({ prefixUrl: process.env.apiBase, timeout: false })
Expand Down
17 changes: 16 additions & 1 deletion lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Guest {
name: string
phone: string
address: string
checkedInCompanyIds?: string[]
}

export interface GuestChangeset {
Expand Down Expand Up @@ -68,7 +69,7 @@ export async function getGuest(): Promise<Guest> {
}

export async function updateGuest(changes: GuestChangeset): Promise<Guest> {
let guest = await db.guests.toCollection().first()
let guest = await getGuest()
await db.guests.update(guest.id, changes)
guest = await getGuest()
return guest
Expand Down Expand Up @@ -176,3 +177,17 @@ export async function updateCheckin(
const checkin = await getCheckin(id)
return checkin
}

export async function setCheckedInCompanyIds(
companyId: string
): Promise<Guest> {
let guest = await getGuest()
if (!guest) return

if (!guest.checkedInCompanyIds) guest.checkedInCompanyIds = []
if (guest.checkedInCompanyIds.indexOf(companyId) < 0)
guest.checkedInCompanyIds.push(companyId)
await db.guests.update(guest.id, guest)
guest = await getGuest()
return guest
}
23 changes: 19 additions & 4 deletions pages/checkin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const CheckingPage: React.FC<{}> = () => {
})
const [doCheckout] = useMutation(api.checkoutTicket)

const [area, setArea] = React.useState<api.AreaResponse>()
React.useEffect(() => {
areaId &&
api.fetchArea(areaId).then((area) => {
setArea(area)
})
salimhb marked this conversation as resolved.
Show resolved Hide resolved
}, [areaId])

const performCheckin = React.useCallback(
async (guest: db.Guest) => {
const lastCheckin = await db.getLastCheckin()
Expand All @@ -41,9 +49,10 @@ const CheckingPage: React.FC<{}> = () => {
},
guest,
})
await db.setCheckedInCompanyIds(area?.companyId)
router.replace('/my-checkins')
},
[doCheckin, doCheckout, publicKey, areaId, router]
[doCheckin, doCheckout, publicKey, areaId, router, area]
)

// The loading spinner should only become visible after a small amount of time
Expand Down Expand Up @@ -83,17 +92,23 @@ const CheckingPage: React.FC<{}> = () => {
React.useEffect(() => {
// Disallow empty data. This is the case on initial mount due to next's
// static optimization
if (!publicKey || !areaId) return
if (!publicKey || !areaId || !area?.companyId) return

// Check if a guest was already created, then do the checkin cha cha cha.
db.getGuest().then((guest) => {
if (guest && guest.name && guest.phone && guest.address) {
if (
guest &&
guest.name &&
guest.phone &&
guest.address &&
guest.checkedInCompanyIds?.indexOf(area?.companyId) >= 0
) {
performCheckin(guest)
} else {
setShowOnboarding(true)
}
})
}, [performCheckin, publicKey, areaId])
}, [performCheckin, publicKey, areaId, area])

const showLoading = isDelayedLoading && !showOnboarding && !error

Expand Down
4 changes: 4 additions & 0 deletions ui/base/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const Input: React.FC<InputProps> = ({
const active = ownValue !== '' || focused
const underline = focused || Boolean(error)

React.useEffect(() => {
setOwnValue(value)
}, [value])

function handleChange(event): void {
setOwnValue(event.target.value)
rest?.onChange(event)
Expand Down
15 changes: 15 additions & 0 deletions ui/blocks/Onboarding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ const Onboarding: React.FC<OnboardingProps> = ({ onFinish }) => {
const [phoneError, setPhoneError] = React.useState(false)
const [addressError, setAddressError] = React.useState(false)

React.useEffect(() => {
/*
Check if a guest was already created, then prefill form
This occurs if guest is checking in a new company
*/
db.getGuest().then((guest) => {
if (guest) {
setName(guest.name)
setPhone(guest.phone)
setAddress(guest.address)
setRememberMe(true)
}
})
}, [])

function handleSubmit(event): void {
event.preventDefault()
if (!name || !phone || !address) {
Expand Down