diff --git a/components/slack/join-form.js b/components/slack/join-form.js index f846f6e3..c8afaccb 100644 --- a/components/slack/join-form.js +++ b/components/slack/join-form.js @@ -1,9 +1,11 @@ import { Card, Label, Input, Checkbox, Textarea } from 'theme-ui' +import { useRouter } from 'next/router' import useForm from '../../lib/use-form' import Submit from '../submit' const JoinForm = ({ sx = {} }) => { - const { status, formProps, useField } = useForm('/api/join/') + const router = useRouter() + const { status, formProps, useField } = useForm('/api/join/', null, { clearOnSubmit: 5000, method: 'POST', initData: router.query.club ? {club: router.query.club} : {} }) return ( diff --git a/lib/use-form.js b/lib/use-form.js index 2b5f2e91..d659f6bd 100644 --- a/lib/use-form.js +++ b/lib/use-form.js @@ -3,7 +3,7 @@ import { useState, useEffect } from 'react' const useForm = ( submitURL = '/', callback, - options = { clearOnSubmit: 5000, method: 'POST' } + options = { clearOnSubmit: 5000, method: 'POST', initData: {} } ) => { const [status, setStatus] = useState('default') const [data, setData] = useState({}) @@ -12,7 +12,7 @@ const useForm = ( const onFieldChange = (e, name, type) => { e.persist() const value = e.target[type === 'checkbox' ? 'checked' : 'value'] - setData(data => ({ ...data, [name]: value })) + setData(data => ({ ...data, ...options.initData, [name]: value })) } useEffect(() => { diff --git a/pages/api/join.js b/pages/api/join.js index 1d8fe7ed..808f84e0 100644 --- a/pages/api/join.js +++ b/pages/api/join.js @@ -47,6 +47,7 @@ export default async (req, res) => { Student: data.teen, Reason: data.reason, Invited: true, + Club: data.club ? data.club : '', IP: req.headers['x-forwarded-for'] || req.socket.remoteAddress })