Skip to content

Commit

Permalink
ISSUE #5331 - test existing rev tags using selector within validator
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Daniel committed Jan 10, 2025
1 parent fd61405 commit 9f6589f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,14 @@ export const UploadContainerRevisionForm = ({
const project = ProjectsHooksSelectors.selectCurrentProject();
const allUploadsComplete = ContainerRevisionsHooksSelectors.selectUploadIsComplete();
const revisionsArePending = ContainerRevisionsHooksSelectors.selectRevisionsPending(presetContainerId);
const revisions = ContainerRevisionsHooksSelectors.selectRevisions(presetContainerId);
const presetContainer = ContainersHooksSelectors.selectContainerById(presetContainerId);
const [alreadyExistingTags, setAlreadyExistingTags] = useState([]);
const [isUploading, setIsUploading] = useState<boolean>(false);

const formData = useForm<FormType>({
mode: 'onChange',
resolver: !isUploading ? yupResolver(UploadsSchema) : undefined,
context: {
alreadyExistingNames: [],
alreadyExistingTags,
teamspace,
project,
},
Expand Down Expand Up @@ -201,11 +198,6 @@ export const UploadContainerRevisionForm = ({
ContainerRevisionsActionsDispatchers.fetch(teamspace, project, presetContainerId);
}, []);

useEffect(() => {
if (!revisions.length) return;
setAlreadyExistingTags(revisions.map((r) => r.tag));
}, [revisions.length]);

return (
<FormProvider {...formData}>
{/* @ts-ignore */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { useFormContext } from 'react-hook-form';
import { useEffect } from 'react';
import { IContainer, UploadStatus } from '@/v5/store/containers/containers.types';
import { UploadItemFields } from '@/v5/store/containers/revisions/containerRevisions.types';
import { ContainersActionsDispatchers } from '@/v5/services/actionsDispatchers';
import { ContainerRevisionsActionsDispatchers, ContainersActionsDispatchers } from '@/v5/services/actionsDispatchers';
import { UploadListItemFileIcon } from '@components/shared/uploadFiles/uploadList/uploadListItem/uploadListItemFileIcon/uploadListItemFileIcon.component';
import { UploadListItemTitle } from '@components/shared/uploadFiles/uploadList/uploadListItem/uploadListItemTitle/uploadListItemTitle.component';
import { UploadProgress } from '@components/shared/uploadFiles/uploadList/uploadListItem/uploadProgress/uploadProgress.component';
Expand Down Expand Up @@ -95,6 +95,9 @@ export const UploadListItem = ({
});

useEffect(() => {
if (containerId) {
ContainerRevisionsActionsDispatchers.fetch(teamspace, projectId, containerId);
}
trigger(`${revisionPrefix}.revisionTag`);

for (const [key, val] of Object.entries(sanitiseContainer(selectedContainer))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import * as Yup from 'yup';
import { formatMessage } from '@/v5/services/intl';
import { alphaNumericHyphens, stripIfBlankString } from '../shared/validators';
import { selectRevisions } from '@/v5/store/containers/revisions/containerRevisions.selectors';
import { getState } from '@/v5/helpers/redux.helpers';

export const nullableNumberField = Yup.number()
.transform((value) => (Number.isNaN(value) ? null : value))
Expand Down Expand Up @@ -64,8 +66,8 @@ export const revisionTag = Yup.string()
defaultMessage: 'This tag is already used within this container',
}),
(value, testContext) => {
if (!testContext.options?.context) return true;
return !testContext.options.context.alreadyExistingTags?.map((n) => n.trim().toLocaleLowerCase()).includes(value?.toLocaleLowerCase());
const revisions = selectRevisions(getState(), testContext.parent.containerId);
return !revisions?.map(({ tag }) => tag.trim().toLocaleLowerCase()).includes(value?.toLocaleLowerCase());
},
);

Expand Down

0 comments on commit 9f6589f

Please sign in to comment.