Skip to content

Commit

Permalink
ISSUE #5173 - vertical calib is required
Browse files Browse the repository at this point in the history
  • Loading branch information
Amantini1997 committed Sep 26, 2024
1 parent d92b22d commit 5d498f2
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 132 deletions.
5 changes: 3 additions & 2 deletions frontend/src/v5/store/drawings/drawings.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface MinimumDrawing {

export interface Calibration {
verticalRange: Vector1D;
horizontal: {
horizontal?: {
model: Vector3D,
drawing: Vector2D,
}
Expand All @@ -50,7 +50,7 @@ export interface DrawingStats {
};
number: string,
calibrationStatus?: CalibrationStatus,
calibration?: Calibration,
calibration: Calibration,
type: string,
status: UploadStatus,
errorReason?: {
Expand All @@ -73,4 +73,5 @@ export type NewDrawing = {
type: string;
number: string;
desc?: string;
calibration: Omit<Calibration, 'horizontal'>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const createDrawingFromRevisionBody = (body: CreateDrawingRevisionBody):
number: body.drawingNumber,
type: body.drawingType,
desc: body.drawingDesc,
calibration: {
verticalRange: body.calibration.verticalRange,
units: body.calibration.units,
},
});

export const createFormDataFromRevisionBody = (body: CreateDrawingRevisionBody) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { CalibrationStatus } from '../drawings.types';
import { Calibration, CalibrationStatus } from '../drawings.types';

export interface StatusCode {
code: string,
Expand Down Expand Up @@ -53,6 +53,7 @@ export type CreateDrawingRevisionBody = {
drawingNumber: string;
drawingType: string;
drawingDesc?: string;
calibration: Omit<Calibration, 'horizontal'>;
};

export type CreateDrawingRevisionPayload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,23 @@
*/

import { formatMessage } from '@/v5/services/intl';
import { FormTextField, FormSelect } from '@controls/inputs/formInputs.component';
import { MenuItem } from '@mui/material';
import { SubmitHandler } from 'react-hook-form';
import { FormModal } from '@controls/formModal/formModal.component';
import { DrawingsHooksSelectors, ProjectsHooksSelectors, TeamspacesHooksSelectors } from '@/v5/services/selectorsHooks';
import { ProjectsHooksSelectors, TeamspacesHooksSelectors } from '@/v5/services/selectorsHooks';
import { DrawingsActionsDispatchers } from '@/v5/services/actionsDispatchers';
import { nameAlreadyExists, numberAlreadyExists } from '@/v5/validation/errors.helpers';
import { UnhandledErrorInterceptor } from '@controls/errorMessage/unhandledErrorInterceptor/unhandledErrorInterceptor.component';
import { IFormInput, useDrawingForm } from './drawingsDialogs.hooks';
import { DrawingForm } from './drawingForm.component';

export const CreateDrawingDialog = ({ open, onClickClose }) => {
const teamspace = TeamspacesHooksSelectors.selectCurrentTeamspace();
const project = ProjectsHooksSelectors.selectCurrentProject();
const types = DrawingsHooksSelectors.selectTypes();

const { onSubmitError, formData } = useDrawingForm();

const {
handleSubmit,
control,
formState,
formState: { errors },
} = formData;
const { handleSubmit, formState } = formData;

const onSubmit: SubmitHandler<IFormInput> = async (body) => {
try {
await new Promise<void>((accept, reject ) => DrawingsActionsDispatchers.createDrawing(teamspace, project, body as any, accept, reject));
await new Promise<void>((accept, reject) => DrawingsActionsDispatchers.createDrawing(teamspace, project, body as any, accept, reject));
onClickClose();
} catch (err) {
onSubmitError(err);
Expand All @@ -59,38 +49,7 @@ export const CreateDrawingDialog = ({ open, onClickClose }) => {
maxWidth="sm"
{...formState}
>
<FormTextField
control={control}
name="name"
label={formatMessage({ id: 'drawings.creation.form.name', defaultMessage: 'Name' })}
formError={errors.name}
required
/>

<FormTextField
control={control}
name="number"
label={formatMessage({ id: 'drawings.creation.form.number', defaultMessage: 'Number' })}
formError={errors.number}
required
/>
<FormSelect
required
control={control}
label={formatMessage({ id: 'drawings.creation.form.type', defaultMessage: 'Category' })}
name="type"
>
{types.map((type) => (
<MenuItem key={type} value={type}> {type}</MenuItem>
))}
</FormSelect>
<FormTextField
control={control}
name="desc"
label={formatMessage({ id: 'drawings.creation.form.description', defaultMessage: 'Description' })}
formError={errors.desc}
/>
<UnhandledErrorInterceptor expectedErrorValidators={[nameAlreadyExists, numberAlreadyExists]} />
<DrawingForm formData={formData} />
</FormModal>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* Copyright (C) 2024 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { formatMessage } from '@/v5/services/intl';
import { FormTextField, FormSelect, FormNumberField } from '@controls/inputs/formInputs.component';
import { MenuItem } from '@mui/material';
import { DrawingsHooksSelectors, ProjectsHooksSelectors } from '@/v5/services/selectorsHooks';
import { nameAlreadyExists, numberAlreadyExists } from '@/v5/validation/errors.helpers';
import { UnhandledErrorInterceptor } from '@controls/errorMessage/unhandledErrorInterceptor/unhandledErrorInterceptor.component';
import { IDrawing } from '@/v5/store/drawings/drawings.types';
import { ShareTextField } from '@controls/shareTextField';
import { FormattedMessage } from 'react-intl';
import { DoubleInputLineContainer, SubTitle, Title } from './drawingForm.styles';
import { Gap } from '@controls/gap';
import { MODEL_UNITS } from '../../models.helpers';

interface Props {
formData: any,
drawing?: IDrawing,
}

export const DrawingForm = ({ formData, drawing }:Props) => {
const types = DrawingsHooksSelectors.selectTypes();
const isProjectAdmin = ProjectsHooksSelectors.selectIsProjectAdmin();

const { formState: { errors }, control } = formData;

return (
<>
<Title>
<FormattedMessage
id="drawings.form.informationTitle"
defaultMessage="Drawing information"
/>
</Title>
{(drawing as any)?._id && (
<ShareTextField
label={formatMessage({ id: 'drawings.id', defaultMessage: 'ID' })}
value={(drawing as any)?._id}
/>
)}
<FormTextField
control={control}
name="name"
label={formatMessage({ id: 'drawings.form.name', defaultMessage: 'Name' })}
formError={errors.name}
disabled={!isProjectAdmin}
required
/>
<FormTextField
control={control}
name="number"
label={formatMessage({ id: 'drawings.form.number', defaultMessage: 'Number' })}
formError={errors.number}
disabled={!isProjectAdmin}
required
/>
<FormSelect
control={control}
name="type"
label={formatMessage({ id: 'drawings.form.type', defaultMessage: 'Category' })}
disabled={!isProjectAdmin}
required
>
{types.map((type) => (
<MenuItem key={type} value={type}> {type}</MenuItem>
))}
</FormSelect>
<FormTextField
control={control}
name="desc"
label={formatMessage({ id: 'drawings.form.description', defaultMessage: 'Description' })}
formError={errors.desc}
disabled={!isProjectAdmin}
/>
<Gap $height="18px" />
<Title>
<FormattedMessage
defaultMessage="Calibration Information"
id="drawings.form.calibrationInformation"
/>
</Title>
<SubTitle>
<FormattedMessage
defaultMessage="This sets the vertical axis limits of the drawing in relation to the model"
id="drawings.form.calibrationInformation.description"
/>
</SubTitle>
<DoubleInputLineContainer>
<FormNumberField
control={control}
name="calibration.verticalRange.0"
label={formatMessage({ id: 'drawings.form.bottomExtent', defaultMessage: 'Bottom Extent' })}
formError={errors.calibration?.verticalRange?.[0]}
defaultValue={0}
required
/>
<FormNumberField
control={control}
name="calibration.verticalRange.1"
label={formatMessage({ id: 'drawings.form.topExtent', defaultMessage: 'Top Extent' })}
formError={errors.calibration?.verticalRange?.[1]}
required
defaultValue={1}
/>
</DoubleInputLineContainer>
<FormSelect
required
control={control}
label={formatMessage({ id: 'drawings.form.units', defaultMessage: 'Units' })}
defaultValue="mm"
name="calibration.units"
>
{MODEL_UNITS.map(({ value, name }) => (
<MenuItem key={value} value={value}>{name}</MenuItem>
))}
</FormSelect>
<UnhandledErrorInterceptor expectedErrorValidators={[nameAlreadyExists, numberAlreadyExists]} />
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (C) 2024 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Typography } from '@controls/typography';
import styled from 'styled-components';

export const Title = styled(Typography).attrs({ variant: 'h5' })`
color: ${({ theme }) => theme.palette.secondary.main};
`;

export const SubTitle = styled(Typography).attrs({ variant: 'body1' })`
color: ${({ theme }) => theme.palette.base.main};
`;

export const DoubleInputLineContainer = styled.div`
display: grid;
grid-template-columns: 1fr 1fr;
width: 100%;
gap: 9px;
& > * {
.MuiFormHelperText-root {
width: 200%;
}
&:nth-of-type(2) .MuiFormHelperText-root {
display: none;
}
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ export interface IFormInput {
name: string;
number: string;
type: string;
desc: string
desc: string;
calibration: {
verticalRange: [number, number];
units: string;
};
}


export const useDrawingForm = (defaultValues?: IDrawing) => {
const teamspace = TeamspacesHooksSelectors.selectCurrentTeamspace();
const project = ProjectsHooksSelectors.selectCurrentProject();
Expand All @@ -58,7 +61,9 @@ export const useDrawingForm = (defaultValues?: IDrawing) => {
defaultValues,
});

const { getValues, setValue, trigger } = formData;
const { getValues, setValue, trigger, watch, formState: { dirtyFields } } = formData;
const verticalRange = watch('calibration.verticalRange');

const onSubmitError = (err) => {
if (nameAlreadyExists(err)) {
setAlreadyExistingNames([getValues('name'), ...alreadyExistingNames]);
Expand All @@ -83,5 +88,11 @@ export const useDrawingForm = (defaultValues?: IDrawing) => {
DrawingsActionsDispatchers.fetchTypes(teamspace, project);
}, [isAdmin]);

useEffect(() => {
if (dirtyFields.calibration?.verticalRange.some((v) => v)) {
trigger('calibration.verticalRange');
}
}, [verticalRange?.[0], verticalRange?.[1]]);

return { onSubmitError, formData };
};
Loading

0 comments on commit 5d498f2

Please sign in to comment.