Skip to content

Commit

Permalink
fix: Add conditional check to cellRenderer
Browse files Browse the repository at this point in the history
* Move from onRowClick to LinkRenderer and update linkRendeder to only be used if its a draft report
  • Loading branch information
dhaselhan committed Dec 20, 2024
1 parent dfb5501 commit fcf5d32
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 227 deletions.
2 changes: 1 addition & 1 deletion backend/lcfs/tests/fuel_code/test_fuel_code_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def test_get_fuel_categories(fuel_code_repo, mock_db):


@pytest.mark.anyio
async def test_get_fuel_category_by_name(fuel_code_repo, mock_db):
async def test_get_fuel_category_by(fuel_code_repo, mock_db):
mock_fc = FuelCategory(
fuel_category_id=2, category="Fossil", default_carbon_intensity=0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def test_get_table_options(notional_transfer_service):
async def test_create_notional_transfer(notional_transfer_service):
service, mock_repo, mock_fuel_repo = notional_transfer_service
notional_transfer_data = create_mock_schema({})
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(
mock_fuel_repo.get_fuel_category_by = AsyncMock(
return_value=MagicMock(fuel_category_id=1)
)

Expand Down Expand Up @@ -93,7 +93,7 @@ async def test_update_notional_transfer(notional_transfer_service):
mock_repo.get_latest_notional_transfer_by_group_uuid = AsyncMock(
return_value=mock_existing_transfer
)
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(
mock_fuel_repo.get_fuel_category_by = AsyncMock(
return_value=MagicMock(category="Gasoline")
)

Expand Down
17 changes: 11 additions & 6 deletions backend/lcfs/tests/other_uses/test_other_uses_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ async def test_create_other_use(other_uses_service):
mock_fuel_code.fuel_code = "FuelCode123"

# Mock fuel repository methods
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(return_value=mock_fuel_category)
mock_fuel_repo.get_fuel_category_by = AsyncMock(return_value=mock_fuel_category)
mock_fuel_repo.get_fuel_type_by_name = AsyncMock(return_value=mock_fuel_type)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(return_value=mock_expected_use)
mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock(return_value=mock_provision_of_the_act)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(
return_value=mock_expected_use
)
mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock(
return_value=mock_provision_of_the_act
)
mock_fuel_repo.get_fuel_code_by_name = AsyncMock(return_value=mock_fuel_code)

# Create a mock for the created other use
Expand Down Expand Up @@ -135,8 +139,10 @@ async def test_update_other_use(other_uses_service):

# Mock fuel repository methods
mock_fuel_repo.get_fuel_type_by_name = AsyncMock(return_value=mock_fuel_type)
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(return_value=mock_fuel_category)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(return_value=mock_expected_use)
mock_fuel_repo.get_fuel_category_by = AsyncMock(return_value=mock_fuel_category)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(
return_value=mock_expected_use
)
mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock(
return_value=mock_provision_of_the_act
)
Expand Down Expand Up @@ -181,7 +187,6 @@ async def test_update_other_use(other_uses_service):
mock_repo.update_other_use.assert_awaited_once()



@pytest.mark.anyio
async def test_update_other_use_not_found(other_uses_service):
service, mock_repo, _ = other_uses_service
Expand Down
9 changes: 5 additions & 4 deletions backend/lcfs/web/api/notional_transfer/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi import Depends

from lcfs.db.base import UserTypeEnum, ActionTypeEnum
from lcfs.tests.fuel_supply.test_fuel_supplies_services import fuel_category
from lcfs.web.api.notional_transfer.repo import NotionalTransferRepository
from lcfs.web.core.decorators import service_handler
from lcfs.db.models.compliance.NotionalTransfer import NotionalTransfer
Expand Down Expand Up @@ -50,8 +51,8 @@ async def convert_to_model(
"""
Converts data from NotionalTransferCreateSchema to NotionalTransfer data model to store into the database.
"""
fuel_category = await self.fuel_repo.get_fuel_category_by_name(
notional_transfer_data.fuel_category
fuel_category = await self.fuel_repo.get_fuel_category_by(
category=notional_transfer_data.fuel_category
)
return NotionalTransfer(
**notional_transfer_data.model_dump(
Expand Down Expand Up @@ -164,8 +165,8 @@ async def update_notional_transfer(
!= notional_transfer_data.fuel_category
):
existing_transfer.fuel_category = (
await self.fuel_repo.get_fuel_category_by_name(
notional_transfer_data.fuel_category
await self.fuel_repo.get_fuel_category_by(
category=notional_transfer_data.fuel_category
)
)

Expand Down
11 changes: 5 additions & 6 deletions backend/lcfs/web/api/other_uses/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi import Depends

from lcfs.db.base import UserTypeEnum, ActionTypeEnum
from lcfs.tests.fuel_supply.test_fuel_supplies_services import fuel_category
from lcfs.web.api.other_uses.repo import OtherUsesRepository
from lcfs.web.core.decorators import service_handler
from lcfs.db.models.compliance.OtherUses import OtherUses
Expand Down Expand Up @@ -52,8 +53,8 @@ async def schema_to_model(self, other_use: OtherUsesCreateSchema) -> OtherUses:
"""
Converts data from OtherUsesCreateSchema to OtherUses data model to store into the database.
"""
fuel_category = await self.fuel_repo.get_fuel_category_by_name(
other_use.fuel_category
fuel_category = await self.fuel_repo.get_fuel_category_by(
category=other_use.fuel_category
)
fuel_type = await self.fuel_repo.get_fuel_type_by_name(other_use.fuel_type)
expected_use = await self.fuel_repo.get_expected_use_type_by_name(
Expand Down Expand Up @@ -200,10 +201,8 @@ async def update_other_use(
)

if other_use.fuel_category.category != other_use_data.fuel_category:
other_use.fuel_category = (
await self.fuel_repo.get_fuel_category_by_name(
other_use_data.fuel_category
)
other_use.fuel_category = await self.fuel_repo.get_fuel_category_by(
category=other_use_data.fuel_category
)

if other_use.expected_use.name != other_use_data.expected_use:
Expand Down
16 changes: 3 additions & 13 deletions frontend/src/views/Admin/AdminMenu/components/Users.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
/* eslint-disable react-hooks/exhaustive-deps */
// @mui component
import BCTypography from '@/components/BCTypography'
import BCButton from '@/components/BCButton'
import BCBox from '@/components/BCBox'
import BCAlert from '@/components/BCAlert'
import BCDataGridServer from '@/components/BCDataGrid/BCDataGridServer'
// icons
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faCirclePlus } from '@fortawesome/free-solid-svg-icons'
// hooks
import { useLocation, useNavigate } from 'react-router-dom'
import { useCallback, useRef, useState, useEffect } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'

import { ROUTES, apiRoutes } from '@/constants/routes'
import { usersColumnDefs, idirUserDefaultFilter } from './_schema'
import { calculateRowHeight } from '@/utils/formatters'
import { apiRoutes, ROUTES } from '@/constants/routes'
import { idirUserDefaultFilter, usersColumnDefs } from './_schema'

export const Users = () => {
const { t } = useTranslation(['common', 'admin'])
Expand Down Expand Up @@ -46,10 +41,6 @@ export const Users = () => {
return params.data.userProfileId
}, [])

const handleRowClicked = useCallback((params) => {
navigate(`${ROUTES.ADMIN_USERS}/${params.data.userProfileId}`)
})

const gridRef = useRef()
useEffect(() => {
if (location.state?.message) {
Expand Down Expand Up @@ -99,7 +90,6 @@ export const Users = () => {
defaultSortModel={defaultSortModel}
defaultFilterModel={idirUserDefaultFilter}
handleGridKey={handleGridKey}
handleRowClicked={handleRowClicked}
enableResetButton={false}
enableCopyButton={false}
/>
Expand Down
9 changes: 0 additions & 9 deletions frontend/src/views/Admin/AdminMenu/components/_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,6 @@ export const usersColumnDefs = (t) => [
}
]

export const usersDefaultColDef = {
resizable: true,
sortable: true,
filter: true,
minWidth: 300,
floatingFilter: true, // enables the filter boxes under the header label
suppressHeaderMenuButton: true // suppresses the menu button appearing next to the Header Label
}

export const idirUserDefaultFilter = [
{ filterType: 'text', type: 'blank', field: 'organizationId', filter: '' }
]
Expand Down
27 changes: 1 addition & 26 deletions frontend/src/views/Admin/__tests__/Users.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ vi.mock('@/utils/formatters', () => ({

// Mock BCDataGridServer component
vi.mock('@/components/BCDataGrid/BCDataGridServer', () => ({
default: ({ handleRowClicked }) => (
<div
data-test="mocked-data-grid"
onClick={() => handleRowClicked({ data: { userProfileId: '123' } })}
>
Mocked DataGrid
</div>
)
default: ({}) => <div data-test="mocked-data-grid">Mocked DataGrid</div>
}))

// Helper component to access current location
Expand Down Expand Up @@ -137,22 +130,4 @@ describe('Users Component', () => {
)
expect(screen.getByText('Test alert message')).toBeInTheDocument()
})

it('handles row click correctly', async () => {
render(
<WrapperComponent>
<Users />
</WrapperComponent>
)

// Simulate a row click
const mockedDataGrid = screen.getByTestId('mocked-data-grid')
fireEvent.click(mockedDataGrid)

// Check if the navigation occurred
await waitFor(() => {
const locationDisplay = screen.getByTestId('location-display')
expect(locationDisplay.textContent).toBe('/admin/users/123')
})
})
})
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import BCAlert from '@/components/BCAlert'
import BCBox from '@/components/BCBox'
import BCDataGridServer from '@/components/BCDataGrid/BCDataGridServer'
import { apiRoutes, ROUTES } from '@/constants/routes'
import { apiRoutes } from '@/constants/routes'
import Grid2 from '@mui/material/Unstable_Grid2/Grid2'
import { formatNumberWithCommas as valueFormatter } from '@/utils/formatters'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useLocation, useParams, useNavigate } from 'react-router-dom'
import { useLocation, useParams } from 'react-router-dom'
import { v4 as uuid } from 'uuid'
import { COMPLIANCE_REPORT_STATUSES } from '@/constants/statuses.js'
import { LinkRenderer } from '@/utils/grid/cellRenderers.jsx'

export const AllocationAgreementSummary = ({ data, status }) => {
const [alertMessage, setAlertMessage] = useState('')
const [alertSeverity, setAlertSeverity] = useState('info')
const [gridKey, setGridKey] = useState(`allocation-agreements-grid`)
const { complianceReportId, compliancePeriod } = useParams()
const [gridKey, setGridKey] = useState('allocation-agreements-grid')
const { complianceReportId } = useParams()

const gridRef = useRef()
const { t } = useTranslation(['common', 'allocationAgreement'])
const location = useLocation()
const navigate = useNavigate()

useEffect(() => {
if (location.state?.message) {
Expand Down Expand Up @@ -47,9 +47,16 @@ export const AllocationAgreementSummary = ({ data, status }) => {
const defaultColDef = useMemo(
() => ({
floatingFilter: false,
filter: false
filter: false,
cellRenderer:
status === COMPLIANCE_REPORT_STATUSES.DRAFT ? LinkRenderer : undefined,
cellRendererParams: {
url: () => {
return 'allocation-agreements'
}
}
}),
[]
[status]
)

const columns = useMemo(
Expand Down Expand Up @@ -137,17 +144,6 @@ export const AllocationAgreementSummary = ({ data, status }) => {
setGridKey(`allocation-agreements-grid-${uuid()}`)
}

const handleRowClicked = (params) => {
if (status === COMPLIANCE_REPORT_STATUSES.DRAFT) {
navigate(
ROUTES.REPORTS_ADD_ALLOCATION_AGREEMENTS.replace(
':compliancePeriod',
compliancePeriod
).replace(':complianceReportId', complianceReportId)
)
}
}

return (
<Grid2 className="allocation-agreement-container" mx={-1}>
<div>
Expand All @@ -159,10 +155,10 @@ export const AllocationAgreementSummary = ({ data, status }) => {
</div>
<BCBox component="div" sx={{ height: '100%', width: '100%' }}>
<BCDataGridServer
className={'ag-theme-material'}
className="ag-theme-material"
gridRef={gridRef}
apiEndpoint={apiRoutes.getAllAllocationAgreements}
apiData={'allocationAgreements'}
apiData="allocationAgreements"
apiParams={{ complianceReportId }}
columnDefs={columns}
gridKey={gridKey}
Expand All @@ -172,7 +168,6 @@ export const AllocationAgreementSummary = ({ data, status }) => {
enableCopyButton={false}
defaultColDef={defaultColDef}
suppressPagination={data.allocationAgreements.length <= 10}
handleRowClicked={handleRowClicked}
/>
</BCBox>
</Grid2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const EditViewComplianceReport = ({ reportData, isError, error }) => {
const { t } = useTranslation(['common', 'report'])
const location = useLocation()
const [modalData, setModalData] = useState(null)
const [internalComment, setInternalComment] = useState('')
const [hasMet, setHasMet] = useState(false)
const [isSigningAuthorityDeclared, setIsSigningAuthorityDeclared] =
useState(false)
Expand All @@ -60,9 +59,7 @@ export const EditViewComplianceReport = ({ reportData, isError, error }) => {
})
}
}
const handleCommentChange = useCallback((newComment) => {
setInternalComment(newComment)
}, [])

const handleScroll = useCallback(() => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop
setIsScrollingUp(scrollTop < lastScrollTop || scrollTop === 0)
Expand All @@ -74,7 +71,6 @@ export const EditViewComplianceReport = ({ reportData, isError, error }) => {
return () => window.removeEventListener('scroll', handleScroll)
}, [handleScroll])

// hooks
const {
data: currentUser,
isLoading: isCurrentUserLoading,
Expand Down Expand Up @@ -239,9 +235,8 @@ export const EditViewComplianceReport = ({ reportData, isError, error }) => {
<BCBox>
<Role roles={govRoles}>
<InternalComments
entityType={'complianceReport'}
entityType="complianceReport"
entityId={parseInt(complianceReportId)}
onCommentChange={handleCommentChange}
/>
</Role>
</BCBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ const ReportDetails = ({ currentStatus = 'Draft', isAnalystRole }) => {
return `panel${index}`
}).filter(Boolean)) // Initialize with panels that should be open by default

const [allExpanded, setAllExpanded] = useState(true)

const handleChange = (panel) => (event, isExpanded) => {
setExpanded((prev) =>
isExpanded ? [...prev, panel] : prev.filter((p) => p !== panel)
Expand All @@ -206,7 +204,6 @@ const ReportDetails = ({ currentStatus = 'Draft', isAnalystRole }) => {

const handleExpandAll = () => {
setExpanded(activityList.map((_, index) => `panel${index}`))
setAllExpanded(true)
}

const handleCollapseAll = () => {
Expand Down
Loading

0 comments on commit fcf5d32

Please sign in to comment.