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

1430: Add query params to card self service form #1749

Merged
merged 10 commits into from
Nov 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Checkbox, FormGroup, InputGroup, Intent } from '@blueprintjs/core'
import InfoOutlined from '@mui/icons-material/InfoOutlined'
import { styled } from '@mui/material'
import React, { ReactElement, useContext, useState } from 'react'
import { useSearchParams } from 'react-router-dom'

import { Card, getFullNameValidationErrorMessage, isFullNameValid, isValid } from '../../cards/Card'
import ClearInputButton from '../../cards/extensions/components/ClearInputButton'
Expand Down Expand Up @@ -48,6 +49,7 @@ const CardSelfServiceForm = ({
const [touchedFullName, setTouchedFullName] = useState(false)
const [openDataPrivacy, setOpenDataPrivacy] = useState<boolean>(false)
const [openReferenceInformation, setOpenReferenceInformation] = useState<boolean>(false)
const [_, setSearchParams] = useSearchParams()
const cardValid = isValid(card, { expirationDateNullable: true })
const appToaster = useAppToaster()
const showErrorMessage = touchedFullName || formSendAttempt
Expand All @@ -68,6 +70,7 @@ const CardSelfServiceForm = ({
return
}
await generateCards()
setSearchParams(undefined, { replace: true })
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ApolloError } from '@apollo/client'
import React, { useCallback, useContext, useState } from 'react'
import { useSearchParams } from 'react-router-dom'

import { Card, generateCardInfo, initializeCard } from '../../../cards/Card'
import { Card, generateCardInfo, initializeCardFromCSV } from '../../../cards/Card'
import { generatePdf } from '../../../cards/PdfFactory'
import { CreateCardsError, CreateCardsResult } from '../../../cards/createCards'
import getMessageFromApolloError from '../../../errors/getMessageFromApolloError'
Expand All @@ -12,6 +13,7 @@ import { base64ToUint8Array, uint8ArrayToBase64 } from '../../../util/base64'
import downloadDataUri from '../../../util/downloadDataUri'
import getCustomDeepLinkFromQrCode from '../../../util/getCustomDeepLinkFromQrCode'
import { useAppToaster } from '../../AppToaster'
import { getHeaders } from '../../cards/ImportCardsController'
import FormErrorMessage from '../components/FormErrorMessage'

export enum CardSelfServiceStep {
Expand All @@ -35,9 +37,12 @@ type UseCardGeneratorSelfServiceReturn = {
const useCardGeneratorSelfService = (): UseCardGeneratorSelfServiceReturn => {
const projectConfig = useContext(ProjectConfigContext)
const appToaster = useAppToaster()
const [selfServiceCard, setSelfServiceCard] = useState(
initializeCard(projectConfig.card, undefined, { expirationDate: null })
)
const [searchParams] = useSearchParams()
const [selfServiceCard, setSelfServiceCard] = useState(() => {
const headers = getHeaders(projectConfig)
const values = headers.map(header => searchParams.get(header))
return initializeCardFromCSV(projectConfig.card, values, headers, undefined, true)
})
const [isLoading, setIsLoading] = useState<boolean>(false)
const [selfServiceState, setSelfServiceState] = useState<CardSelfServiceStep>(CardSelfServiceStep.form)
const [deepLink, setDeepLink] = useState<string>('')
Expand Down
4 changes: 2 additions & 2 deletions administration/src/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ export const initializeCardFromCSV = (
cardConfig: CardConfig,
line: (string | null)[],
headers: string[],
region: Region,
region: Region | undefined,
withDefaults = false
): Card => {
const defaultCard = withDefaults
? initializeCard(cardConfig, region)
: { fullName: '', expirationDate: null, extensions: { [REGION_EXTENSION_NAME]: region.id } }
: { fullName: '', expirationDate: null, extensions: region ? { [REGION_EXTENSION_NAME]: region.id } : {} }
const extensions = headers.reduce((acc, header, index) => {
const value = line[index]
const extension = Extensions.find(extension => extension.name === getExtensionNameByCSVHeader(cardConfig, header))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const BirthdayExtension: Extension<BirthdayExtensionState> = {
},
}),
isValid: ({ birthday }: BirthdayExtensionState) => {
if (birthday === null) {
if (!birthday) {
return false
}
const today = PlainDate.fromLocalDate(new Date())
Expand Down