Skip to content

Commit

Permalink
fix: DOB parsing and some spacing
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Coufal <[email protected]>
  • Loading branch information
tumido committed Jun 1, 2022
1 parent 07125a9 commit 06a0199
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
5 changes: 4 additions & 1 deletion components/ListRegistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ const Row = ({ row, onDeleteEntry }: RowProps) => {
Datum narození
</TableCell>
<TableCell>
{(row.dob.toDate() as Date).toLocaleDateString()}
{(row.dob.toDate() as Date).toLocaleDateString(
'cs-CZ',
{ timeZone: 'UTC' }
)}
</TableCell>
</TableRow>
<TableRow>
Expand Down
1 change: 0 additions & 1 deletion components/Wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const Wizard = ({ children, initialValues, onSubmit }: WizardProps) => {

const handleSubmit = async (values: object, bag: FormikHelpers<object>) => {
if (isLastStep) {
console.log('here')
return onSubmit(values, bag)
} else {
bag.setTouched({})
Expand Down
4 changes: 2 additions & 2 deletions containers/Co.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Co = () => {
<Typography variant="h2" textAlign="center" color="primary" gutterBottom>
C<Logo size="uppercase" />?
</Typography>
<Grid container>
<Grid container spacing={2}>
<Grid item xs={12} lg={6}>
<OrderedList>
<li>
Expand Down Expand Up @@ -68,7 +68,7 @@ const Co = () => {
<Image src="/smr_zakladna.jpg" />
</Grid>
</Grid>
<Typography variant="body1" gutterBottom>
<Typography variant="body1" gutterBottom sx={{ mt: 2 }}>
Letošní tábor se bude konat v trochu jiném středisku než jste zvyklí.
Základna se nachází u lesa u obce Křekov, v malebné přírodě v srdci
Valašska. Na základně je tekoucí pitná voda, sprcha, splachovací WC.
Expand Down
41 changes: 36 additions & 5 deletions containers/Jak.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ type Step = {
note?: string
}

type Registration = {
address: string
allergies?: string
dob: Date
email?: string
insurance: string
name: string
nick?: string
parent_address?: string
parent_email: string
parent_name: string
parent_phone: string
phone?: string
terms: boolean
}

type Layout = {
sizing: {
xs: number
Expand All @@ -46,6 +62,7 @@ type Layout = {
formControl?: object
textField?: object
mask?: string
disableFuture?: boolean
}
inputVariant?: string
options?: {
Expand Down Expand Up @@ -77,7 +94,13 @@ const steps: Step[] = [
excludeEmptyString: true,
}),
insurance: Yup.string().required('Povinný údaj'),
dob: Yup.date().typeError('Toto není datum').required('Povinný údaj'),
dob: Yup.date()
.typeError('Toto není datum')
.required('Povinný údaj')
.max(
new Date(new Date().setFullYear(new Date().getFullYear() - 12)),
'Účastník musí být starší 12let'
),
allergies: Yup.string(),
}),
layout: [
Expand Down Expand Up @@ -168,6 +191,7 @@ const steps: Step[] = [
mask: '__.__.____',
openTo: 'year',
views: ['year', 'month', 'day'],
disableFuture: true,
},
},
{
Expand Down Expand Up @@ -255,7 +279,6 @@ const steps: Step[] = [
component: CheckboxWithLabel,
props: {
id: 'terms',
fullWidth: true,
type: 'checkbox',
Label: {
label:
Expand Down Expand Up @@ -289,7 +312,7 @@ const FieldValue = ({ id, label }: { id: string; label: string }) => {

const value =
field.value instanceof Date
? field.value.toLocaleDateString()
? field.value.toLocaleDateString('cs-CZ')
: typeof field.value === 'string'
? field.value
: undefined
Expand All @@ -303,6 +326,9 @@ const FieldValue = ({ id, label }: { id: string; label: string }) => {
return null
}

const localeDateToUtc = (date: Date) =>
new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))

const Form = ({ onSubmit }: { onSubmit: Function }) => (
<LocalizationProvider dateAdapter={AdapterDateFns} locale={csLocale}>
<Wizard initialValues={initialValues} onSubmit={onSubmit}>
Expand Down Expand Up @@ -405,10 +431,15 @@ const Jak = () => {

const handleSubmit = (data: object) => {
setShowWizard(false)
const sanitizedData = Object.entries(data).reduce(
const dataWithoutUndefined = Object.entries(data).reduce(
(acc, [k, v]) => (v !== undefined ? { ...acc, [k]: v } : acc),
{}
)
) as Registration

const sanitizedData = {
...dataWithoutUndefined,
dob: localeDateToUtc(dataWithoutUndefined.dob),
}
addDoc(collection(getFirestore(), 'registrations'), sanitizedData)
}

Expand Down
2 changes: 1 addition & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ exports.onRegistration = functions.firestore

data.dob = (data.dob as admin.firestore.Timestamp)
.toDate()
.toLocaleDateString('cs-CZ')
.toLocaleDateString('cs-CZ', { timeZone: 'UTC' })

const messages = [
{
Expand Down

0 comments on commit 06a0199

Please sign in to comment.