Skip to content

Commit

Permalink
Merge branch 'add-and-view-a-spares-definition-#1161' into integrate-…
Browse files Browse the repository at this point in the history
…spares-definition-with-catalogue-items-and-items-#1162
  • Loading branch information
joshuadkitenge committed Jan 8, 2025
2 parents 9fcc536 + 2ad8f9f commit ebe75b8
Show file tree
Hide file tree
Showing 13 changed files with 1,880 additions and 1,867 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get install libgconf-2-4
yarn --immutable
- name: Run linting
Expand Down Expand Up @@ -55,7 +54,6 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get install libgconf-2-4
yarn --immutable
- name: Run e2e tests
Expand Down Expand Up @@ -130,7 +128,6 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get install libgconf-2-4
yarn --immutable
- name: Run e2e tests
Expand Down
3 changes: 3 additions & 0 deletions cypress/e2e/with_mock_data/catalogueCategories.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,11 +952,14 @@ describe('Catalogue Category', () => {

cy.findByText('Add Property').should('have.length', 1);

// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(100);
cy.findBrowserMockedRequests({
method: 'POST',
url: '/v1/catalogue-categories/:catalogue_category_id/properties',
}).should(async (patchRequests) => {
expect(patchRequests.length).equal(1);

const request = patchRequests[0];
expect(JSON.stringify(await request.json())).equal(
JSON.stringify({
Expand Down
110 changes: 2 additions & 108 deletions src/catalogue/category/catalogueCategoryDialog.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
Divider,
FormControl,
FormControlLabel,
FormHelperText,
FormLabel,
Grid,
IconButton,
Expand All @@ -31,7 +30,6 @@ import {
FormProvider,
useFieldArray,
useForm,
useFormContext,
} from 'react-hook-form';
import {
AllowedValues,
Expand All @@ -55,6 +53,7 @@ import {
} from '../../app.types';
import { CatalogueCategorySchema, RequestType } from '../../form.schemas';
import handleIMS_APIError from '../../handleIMS_APIError';
import AllowedValuesListTextFields from './property/allowedValuesListTextFields.component';
import CatalogueItemsPropertiesTable from './property/catalogueItemPropertiesTable.component';

// Function to convert a list of strings to a list of numbers
Expand Down Expand Up @@ -154,111 +153,6 @@ function transformPostPropertyToAddProperty(
return modifiedCatalogueItemProperty;
}

const AllowedValuesListTextFields = (props: { nestIndex: number }) => {
const {
control,
formState: { errors },
clearErrors,
} = useFormContext<AddCatalogueCategoryWithPlacementIds>();

const { nestIndex } = props;

const { fields, append, remove } = useFieldArray({
control,
name: `properties.${nestIndex}.allowed_values.values.values`, // Adjust the field name according to your data structure
});

const clearDuplicateValueErrors = React.useCallback(() => {
const allowedValuesErrors = errors?.properties?.[nestIndex]?.allowed_values;
const errorIndexes =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
((allowedValuesErrors?.values?.values as any[]) || [])
.map((error, index) => {
if (error?.value?.message === 'Duplicate value.') {
return index;
}
return -1;
})
.filter((index) => index !== -1);

errorIndexes.forEach((errorIndex) => {
clearErrors(
`properties.${nestIndex}.allowed_values.values.values.${errorIndex}`
);
});
}, [clearErrors, errors?.properties, nestIndex]);

return (
<>
{fields.map((field, index) => {
return (
<Stack
key={field.av_placement_id}
direction="row"
sx={{ alignItems: 'center', justifyContent: 'center', mb: 1 }}
spacing={1}
>
<Controller
control={control}
name={`properties.${nestIndex}.allowed_values.values.values.${index}`}
render={({ field: controllerField }) => (
<TextField
id={`list-item-input-${controllerField.value.av_placement_id}`}
label={`List item`}
variant="outlined"
{...controllerField}
value={controllerField.value.value}
onChange={(event) => {
controllerField.onChange({
av_placement_id: controllerField.value.av_placement_id,
value: event.target.value,
});
clearDuplicateValueErrors();
}}
error={
!!errors?.properties?.[nestIndex]?.allowed_values?.values
?.values?.[index]?.value
}
helperText={
errors?.properties?.[nestIndex]?.allowed_values?.values
?.values?.[index]?.value?.message as string
}
/>
)}
/>

<IconButton
aria-label={`Delete list item`}
onClick={() => {
remove(index);
clearDuplicateValueErrors();
}}
>
<DeleteIcon />
</IconButton>
</Stack>
);
})}
<IconButton
aria-label={`Add list item`}
onClick={() =>
append({ av_placement_id: crypto.randomUUID(), value: '' })
}
>
<AddIcon />
</IconButton>
{!!errors?.properties?.[nestIndex]?.allowed_values?.values?.values && (
<FormHelperText error>
{
errors?.properties?.[nestIndex]?.allowed_values?.values?.values
?.message
}
</FormHelperText>
)}
</>
);
};

export interface CatalogueCategoryDialogProps {
open: boolean;
onClose: () => void;
Expand Down Expand Up @@ -720,7 +614,7 @@ const CatalogueCategoryDialog = (props: CatalogueCategoryDialogProps) => {
>
<FormProvider {...formMethods}>
<AllowedValuesListTextFields
nestIndex={index}
propertyIndex={index}
/>
</FormProvider>
</Stack>
Expand Down
Loading

0 comments on commit ebe75b8

Please # to comment.