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

fix(skilavottord): Fix issue with numberplate count #16120

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -87,7 +87,7 @@ const UpdateSkilavottordVehicleInfoMutation = gql`
mutation updateSkilavottordVehicleInfo(
$permno: String!
$mileage: Float!
$plateCount: Float!
$plateCount: Float
$plateLost: Boolean!
) {
updateSkilavottordVehicleInfo(
Expand All @@ -100,6 +100,21 @@ const UpdateSkilavottordVehicleInfoMutation = gql`
`

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

useEffect(() => {
setReloadFlag(false)
}, [reloadFlag])
birkirkristmunds marked this conversation as resolved.
Show resolved Hide resolved

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

useEffect(() => {
triggerReload()
}, [])
birkirkristmunds marked this conversation as resolved.
Show resolved Hide resolved

const methods = useForm({
mode: 'onChange',
})
Expand Down Expand Up @@ -184,19 +199,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,
birkirkristmunds marked this conversation as resolved.
Show resolved Hide resolved
plateLost: !!plateLost?.length,
},
}).then(() => {
Expand Down Expand Up @@ -274,6 +295,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 @@ -63,13 +63,13 @@ export class VehicleService {
async updateVehicleInfo(
permno: string,
mileage: number,
plateCount: number,
plateCount = 0,
birkirkristmunds marked this conversation as resolved.
Show resolved Hide resolved
plateLost: boolean,
): Promise<boolean> {
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
Loading