Skip to content

Commit

Permalink
✨ validations for supply point page in gurb
Browse files Browse the repository at this point in the history
Co-authored-by: Joana Figueira <[email protected]>
  • Loading branch information
marta197 and JoanaFigueira committed Oct 16, 2024
1 parent 168808f commit a7145e8
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 68 deletions.
5 changes: 3 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const App = (props) => {
)
const Gurb = lazy(() => import('./containers/Gurb'))


const loadContractData = () => {
const contractData =
typeof props.contract === 'string' && props.contract !== ''
Expand Down Expand Up @@ -424,7 +423,9 @@ const App = (props) => {
path="/:language/gurb/validations/"
element={
<GurbErrorContextProvider>
<Gurb {...props} />
<GurbLoadingContextProvider>
<Gurb {...props} />
</GurbLoadingContextProvider>
</GurbErrorContextProvider>
}
/>
Expand Down
8 changes: 6 additions & 2 deletions src/containers/Gurb.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
selfConsumptionValidations
} from './Gurb/requirementsValidations'
import GurbErrorContext from '../context/GurbErrorContext'
import GurbLoadingContext from '../context/GurbLoadingContext'

const MAX_STEP_NUMBER = 6
const REQUIREMENTS_STEPS = [1, 2, 3, 4]
Expand All @@ -27,6 +28,7 @@ const Gurb = (props) => {
const { i18n } = useTranslation()
const { language } = useParams()
const { error } = useContext(GurbErrorContext)
const { loading } = useContext(GurbLoadingContext)

const [activeStep, setActiveStep] = useState(0)
useEffect(() => {
Expand All @@ -35,7 +37,7 @@ const Gurb = (props) => {

const initialValues = {
is_client: undefined,
cups: undefined,
cups: '',
has_light: undefined,
address: {
street: undefined,
Expand Down Expand Up @@ -116,7 +118,9 @@ const Gurb = (props) => {
/>
<NextButton
disabled={
!formikProps.isValid || activeStep === MAX_STEP_NUMBER
loading ||
!formikProps.isValid ||
activeStep === MAX_STEP_NUMBER
}
onClick={() => nextStep(formikProps)}
title={'NEXT'}
Expand Down
8 changes: 6 additions & 2 deletions src/containers/Gurb/SupplyPoint.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ const SupplyPoint = (props) => {
// TODO: gurb id from where?
const gurbId = 1
await getGurbData(gurbId)
.then((data) => {
setGurbData(data)
.then(({ data }) => {
setGurbData({
name: data?.name,
state: data?.state,
completedPercentage: data?.assigned_betas_percentage
})
})
.catch((error) => {
// TODO: handle errors
Expand Down
30 changes: 21 additions & 9 deletions src/containers/Gurb/components/CUPS.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { useEffect } from 'react'
import { useContext, useEffect } from 'react'
import { useTranslation } from 'react-i18next'

import InputField from './InputField'

import { checkCups } from '../../../services/api'
import GurbLoadingContext from '../../../context/GurbLoadingContext'

const CUPS = (props) => {
const { values, errors, touched, setFieldValue, setFieldTouched } = props
const {
values,
errors,
touched,
setFieldValue,
setFieldError,
setFieldTouched
} = props
const { t } = useTranslation()
// const [isLoading, setIsLoading] = useState(false)
const { loading, setLoading } = useContext(GurbLoadingContext)

useEffect(() => {
const cups = values.cups
if (cups?.length > 18) {
// setIsLoading(true)
if (cups?.length >= 20) {
setLoading(true)
checkCups(cups)
.then((response) => {
const status = response?.data?.status
Expand All @@ -22,11 +30,14 @@ const CUPS = (props) => {
} else {
setFieldValue('is_client', true)
}
// setIsLoading(false)
setFieldTouched('cups', true)
setLoading(false)
})
.catch((error) => {
console.error(error.response)
// setIsLoading(false)
.catch(({ response }) => {
const { error } = response.data
setFieldError('cups', t(`GURB_ERROR_${error.code}`))
setFieldTouched('cups', true)
setLoading(false)
})
}
}, [values.cups])
Expand All @@ -52,6 +63,7 @@ const CUPS = (props) => {
touched={touched?.cups}
value={values.cups}
error={errors?.cups}
isLoading={loading}
/>
)
}
Expand Down
11 changes: 6 additions & 5 deletions src/containers/Gurb/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import Typography from '@mui/material/Typography'
import { textHeader1 } from '../gurbTheme'

const Header = ({ title }) => {
return <Typography
return (
<Typography
// variant="h2"
sx={textHeader1}
>
{ title }
sx={textHeader1}>
{title}
</Typography>
)
}

export default Header
export default Header
19 changes: 15 additions & 4 deletions src/containers/Gurb/components/InputField.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import Typography from '@mui/material/Typography'
import Box from '@mui/material/Box'
import TextField from '@mui/material/TextField'
import { textHeader4, textHelper1 } from '../gurbTheme'
import InputAdornment from '@mui/material/InputAdornment'

import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined'
import CheckOutlinedIcon from '@mui/icons-material/CheckOutlined'
import CircularProgress from '@mui/material/CircularProgress'

import { textHeader4, textHelper1 } from '../gurbTheme'

export const HelperText = ({ helperText, iconHelper }) => {
return (
Expand All @@ -24,7 +29,8 @@ const InputField = ({
handleBlur,
touched,
value,
error
error,
isLoading = false
}) => {
return (
<Box>
Expand All @@ -38,7 +44,12 @@ const InputField = ({
fullWidth
InputProps={{
sx: { borderRadius: '8px', display: 'flex' },
onBlur: handleBlur
onBlur: handleBlur,
endAdornment: (
<InputAdornment position="end">
{isLoading && <CircularProgress size={24} />}
</InputAdornment>
)
}}
label={textFieldLabel}
helperText={
Expand All @@ -50,7 +61,7 @@ const InputField = ({
}
onChange={handleChange}
value={value}
error={touched && error}
error={touched && error !== undefined}
/>
</Box>
)
Expand Down
33 changes: 16 additions & 17 deletions src/containers/Gurb/components/TextRecomendation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ import Typography from '@mui/material/Typography'

import { textHeader2, textBody1 } from '../gurbTheme'


const TextRecomendation = ({title, text}) => {
return <Box>
<Typography
// variant="h2"
sx={textHeader2}
>
{title}
</Typography>
<Typography
// variant="body1"
sx={textBody1}
>
{text}
</Typography>
const TextRecomendation = ({ title, text }) => {
return (
<Box>
<Typography
// variant="h2"
sx={textHeader2}>
{title}
</Typography>
<Typography
// variant="body1"
sx={textBody1}>
{text}
</Typography>
</Box>
}
export default TextRecomendation
)
}
export default TextRecomendation
4 changes: 2 additions & 2 deletions src/containers/Gurb/supplyPointValidations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as Yup from 'yup'
const supplyPointValidations = Yup.object().shape({
cups: Yup.string()
// .matches(/^ES[0-9]{15}\d*$/, 'INVALID_FIELD')
.min(18, 'TOO_SHORT')
.max(20, 'TOO_LONG')
.min(20, 'TOO_SHORT')
.max(22, 'TOO_LONG')
.required('REQUIRED_FIELD')
})

Expand Down
1 change: 0 additions & 1 deletion src/context/GurbErrorContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const GurbErrorContextProvider = ({ children }) => {
error: error,
errorInfo: errorInfo
}}>
<React.Fragment>{error}</React.Fragment>
{children}
</GurbErrorContext.Provider>
)
Expand Down
19 changes: 19 additions & 0 deletions src/context/GurbLoadingContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { useState, createContext } from 'react'

const GurbLoadingContext = createContext()

export const GurbLoadingContextProvider = ({ children }) => {
const [loading, setLoading] = useState(undefined)

return (
<GurbLoadingContext.Provider
value={{
loading: loading,
setLoading: setLoading,
}}>
{children}
</GurbLoadingContext.Provider>
)
}

export default GurbLoadingContext
3 changes: 2 additions & 1 deletion src/i18n/locale-ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -752,5 +752,6 @@
"GURB_CONTRACT_STEP": "Contractació",
"GURB_FINAL_STEP": "GURB",
"GURB_NEXT": "Següent",
"GURB_PREV": "Anterior"
"GURB_PREV": "Anterior",
"GURB_ERROR_INVALID_FIELD": "Camp incorrecte"
}
24 changes: 1 addition & 23 deletions src/services/apiGurb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,12 @@ const WEBFORMS_API_URL = document.getElementById('root')?.dataset?.webformsApiUr

// TODO: change this name !!!
export const getGurbData = async (gurbId) => {
console.log('gurgId', gurbId)
return axios({
method: 'GET',
url: `${WEBFORMS_API_URL}/data/gurb/${gurbId}`
}).then((response) => {
return {
name: response.data.name,
state: response.data.state,
completedPercentage: response.data.assigned_betas_percentage
}
return response?.data
})
// const data = {
// id: 1,
// name: 'GURB Mataró',
// state: 'open',
// assigned_betas_kw: 200,
// assigned_betas_percentage: 30,
// available_betas_kw: 400,
// available_betas_percentage: 70,
// generation_power: 200,
// min_power: 3,
// max_power: 20
// }
// return {
// name: data.name,
// state: data.state,
// completedPercentage: data.assigned_betas_percentage,
// }
}

// export const checkGurbPercentage = async (gurbId) => {
Expand Down

0 comments on commit a7145e8

Please sign in to comment.