Skip to content

Commit

Permalink
fix(skilavottord): Fix issue with numberplate count (#16120)
Browse files Browse the repository at this point in the history
* TS-916 Fix issue with numberplate count

* TS-916 Fix code after code rabbit review

* TS-916 Fix code after code rabbit review

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
birkirkristmunds and kodiakhq[bot] committed Sep 24, 2024
1 parent f92833b commit 4b8a6aa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface BoxProps {
vinNumber?: string
outInStatus: number
useStatus: string
reloadFlag: boolean // To force reload of the component to make sure the data in the parent is correct
}

export const CarDetailsBox2: FC<React.PropsWithChildren<BoxProps>> = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMutation, useQuery } from '@apollo/client'
import gql from 'graphql-tag'
import { useRouter } from 'next/router'
import React, { FC, useContext, useEffect } from 'react'
import React, { FC, useContext, useEffect, useState } from 'react'

import {
Box,
Expand Down Expand Up @@ -33,7 +33,7 @@ import {
Role,
} from '@island.is/skilavottord-web/graphql/schema'
import { useI18n } from '@island.is/skilavottord-web/i18n'
import { OutInUsage } from '@island.is/skilavottord-web/utils/consts'
import { OutInUsage, UseStatus } from '@island.is/skilavottord-web/utils/consts'
import { getYear } from '@island.is/skilavottord-web/utils/dateUtils'
import { FormProvider, useForm } from 'react-hook-form'

Expand Down Expand Up @@ -100,6 +100,17 @@ const UpdateSkilavottordVehicleInfoMutation = gql`
`

const Confirm: FC<React.PropsWithChildren<unknown>> = () => {
const [reloadFlag, setReloadFlag] = useState(false)

// Update reloadFlag to trigger the child component to reload
const triggerReload = () => {
setReloadFlag(true)
}

useEffect(() => {
triggerReload()
}, [setReloadFlag])

const methods = useForm({
mode: 'onChange',
})
Expand Down Expand Up @@ -184,19 +195,25 @@ const Confirm: FC<React.PropsWithChildren<unknown>> = () => {

const handleConfirm = () => {
let newMileage = mileageValue
let plateCount = plateCountValue

if (mileageValue !== undefined) {
newMileage = +mileageValue.trim().replace(/\./g, '')
} else {
newMileage = vehicle?.mileage
}

// If vehicle is out of use and not using ticket, set plate count to 0
if (outInStatus === OutInUsage.OUT && useStatus !== UseStatus.OUT_TICKET) {
plateCount = 0
}

// Update vehicle table with latests information
setVehicleRequest({
variables: {
permno: vehicle?.vehicleId,
mileage: newMileage,
plateCount: plateCountValue === 0 ? 0 : plateCountValue,
plateCount,
plateLost: !!plateLost?.length,
},
}).then(() => {
Expand Down Expand Up @@ -274,6 +291,7 @@ const Confirm: FC<React.PropsWithChildren<unknown>> = () => {
mileage={vehicle.mileage || 0}
outInStatus={outInStatus}
useStatus={useStatus || ''}
reloadFlag={reloadFlag}
/>
</FormProvider>
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class VehicleResolver {
@CurrentUser() user: User,
@Args('permno') permno: string,
@Args('mileage') mileage: number,
@Args('plateCount') plateCount: number,
@Args('plateCount', { nullable: true }) plateCount: number,
@Args('plateLost') plateLost: boolean,
) {
return await this.vehicleService.updateVehicleInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class VehicleService {
const findVehicle = await this.findByVehicleId(permno)
if (findVehicle) {
findVehicle.mileage = mileage ?? 0
findVehicle.plateCount = plateCount ?? 0
findVehicle.plateCount = plateCount
findVehicle.plateLost = plateLost

await findVehicle.save()
Expand All @@ -85,7 +85,6 @@ export class VehicleService {
throw new Error(errorMsg)
}
}

async create(vehicle: VehicleModel): Promise<boolean> {
try {
// check if vehicle is already in db
Expand Down

0 comments on commit 4b8a6aa

Please sign in to comment.