From 4b8a6aaf21cb9949eb7f7cdc1e9caa9a1760abbd Mon Sep 17 00:00:00 2001 From: birkirkristmunds <142495885+birkirkristmunds@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:44:48 +0000 Subject: [PATCH] fix(skilavottord): Fix issue with numberplate count (#16120) * 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> --- .../CarDetailsBox2/CarDetailsBox2.tsx | 1 + .../DeregisterVehicle/Confirm/Confirm.tsx | 24 ++++++++++++++++--- .../app/modules/vehicle/vehicle.resolver.ts | 2 +- .../app/modules/vehicle/vehicle.service.ts | 3 +-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/apps/skilavottord/web/components/CarDetailsBox2/CarDetailsBox2.tsx b/apps/skilavottord/web/components/CarDetailsBox2/CarDetailsBox2.tsx index b76419da7cf3..546459bafcb3 100644 --- a/apps/skilavottord/web/components/CarDetailsBox2/CarDetailsBox2.tsx +++ b/apps/skilavottord/web/components/CarDetailsBox2/CarDetailsBox2.tsx @@ -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> = ({ diff --git a/apps/skilavottord/web/screens/DeregisterVehicle/Confirm/Confirm.tsx b/apps/skilavottord/web/screens/DeregisterVehicle/Confirm/Confirm.tsx index 293ac53554ed..53333b718288 100644 --- a/apps/skilavottord/web/screens/DeregisterVehicle/Confirm/Confirm.tsx +++ b/apps/skilavottord/web/screens/DeregisterVehicle/Confirm/Confirm.tsx @@ -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, @@ -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' @@ -100,6 +100,17 @@ const UpdateSkilavottordVehicleInfoMutation = gql` ` const Confirm: FC> = () => { + 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', }) @@ -184,6 +195,7 @@ const Confirm: FC> = () => { const handleConfirm = () => { let newMileage = mileageValue + let plateCount = plateCountValue if (mileageValue !== undefined) { newMileage = +mileageValue.trim().replace(/\./g, '') @@ -191,12 +203,17 @@ const Confirm: FC> = () => { 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(() => { @@ -274,6 +291,7 @@ const Confirm: FC> = () => { mileage={vehicle.mileage || 0} outInStatus={outInStatus} useStatus={useStatus || ''} + reloadFlag={reloadFlag} /> diff --git a/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.resolver.ts b/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.resolver.ts index a5f2af14ec32..d8a1516ffda1 100644 --- a/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.resolver.ts +++ b/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.resolver.ts @@ -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( diff --git a/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.service.ts b/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.service.ts index 3be43b3a65b8..67729e02c3a9 100644 --- a/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.service.ts +++ b/apps/skilavottord/ws/src/app/modules/vehicle/vehicle.service.ts @@ -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() @@ -85,7 +85,6 @@ export class VehicleService { throw new Error(errorMsg) } } - async create(vehicle: VehicleModel): Promise { try { // check if vehicle is already in db