Skip to content

Commit

Permalink
chore: Set initital values for mitigation and threat status
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieweiyi committed Aug 29, 2024
1 parent 2d6b214 commit 9fc987d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { FC, useState, useCallback } from 'react';
import { useMitigationsContext } from '../../../contexts/MitigationsContext/context';
import { useThreatsContext } from '../../../contexts/ThreatsContext/context';
import { Assumption, AssumptionSchema } from '../../../customTypes';
import getNewAssumption from '../../../utils/getNewAssumption';
import getNewMitigation from '../../../utils/getNewMitigation';
import GenericEntityCreationCard, { DEFAULT_ENTITY } from '../../generic/GenericEntityCreationCard';
import GenericEntityCreationCard from '../../generic/GenericEntityCreationCard';
import MitigationLinkView from '../../mitigations/MitigationLinkView';
import ThreatLinkView from '../../threats/ThreatLinkView';

Expand All @@ -28,7 +29,7 @@ export interface AssumptionCreationCardProps {
}

const AssumptionCreationCard: FC<AssumptionCreationCardProps> = ({ onSave }) => {
const [editingEntity, setEditingEntity] = useState<Assumption>(DEFAULT_ENTITY);
const [editingEntity, setEditingEntity] = useState<Assumption>(getNewAssumption());
const [linkedMitigationIds, setLinkedMitigationIds] = useState<string[]>([]);
const [linkedThreatIds, setLinkedThreatIds] = useState<string[]>([]);

Expand All @@ -37,13 +38,13 @@ const AssumptionCreationCard: FC<AssumptionCreationCardProps> = ({ onSave }) =>

const handleSave = useCallback(() => {
onSave?.(editingEntity, linkedMitigationIds, linkedThreatIds);
setEditingEntity(DEFAULT_ENTITY);
setEditingEntity(getNewAssumption());
setLinkedMitigationIds([]);
setLinkedThreatIds([]);
}, [editingEntity, linkedMitigationIds, linkedThreatIds]);

const handleReset = useCallback(() => {
setEditingEntity(DEFAULT_ENTITY);
setEditingEntity(getNewAssumption());
setLinkedMitigationIds([]);
setLinkedThreatIds([]);
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ export interface GenericEntityCreationCardProps {
validateData?: TextAreaProps['validateData'];
}

export const DEFAULT_ENTITY = {
id: DEFAULT_NEW_ENTITY_ID,
numericId: -1,
content: '',
};

const GenericEntityCreationCard: FC<GenericEntityCreationCardProps> = ({
editingEntity,
setEditingEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import { DEFAULT_NEW_ENTITY_ID } from '../../../configs';
import { useAssumptionsContext } from '../../../contexts/AssumptionsContext/context';
import { useThreatsContext } from '../../../contexts/ThreatsContext/context';
import { Mitigation, MitigationSchema } from '../../../customTypes';
import getNewMitigation from '../../../utils/getNewMitigation';
import AssumptionLinkView from '../../assumptions/AssumptionLinkView';
import GenericEntityCreationCard, { DEFAULT_ENTITY } from '../../generic/GenericEntityCreationCard';
import GenericEntityCreationCard from '../../generic/GenericEntityCreationCard';
import ThreatLinkView from '../../threats/ThreatLinkView';

export interface MitigationCreationCardProps {
onSave?: (entity: Mitigation, linkedAssumptionIds: string[], linkedThreatIds: string[]) => void;
}

const MitigationCreationCard: FC<MitigationCreationCardProps> = ({ onSave }) => {
const [editingEntity, setEditingEntity] = useState<Mitigation>(DEFAULT_ENTITY);
const [editingEntity, setEditingEntity] = useState<Mitigation>(getNewMitigation());
const [linkedAssumptionIds, setLinkedAssumptionIds] = useState<string[]>([]);
const [linkedThreatIds, setLinkedThreatIds] = useState<string[]>([]);

Expand All @@ -37,13 +38,13 @@ const MitigationCreationCard: FC<MitigationCreationCardProps> = ({ onSave }) =>

const handleSave = useCallback(() => {
onSave?.(editingEntity, linkedAssumptionIds, linkedThreatIds);
setEditingEntity(DEFAULT_ENTITY);
setEditingEntity(getNewMitigation());
setLinkedAssumptionIds([]);
setLinkedThreatIds([]);
}, [editingEntity, linkedAssumptionIds, linkedThreatIds]);

const handleReset = useCallback(() => {
setEditingEntity(DEFAULT_ENTITY);
setEditingEntity(getNewMitigation());
setLinkedAssumptionIds([]);
setLinkedThreatIds([]);
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ const MitigationList: FC<MitigationListProps> = ({
}, []);
}, [mitigationList]);

const handleAddTagToEntity = useCallback((assumption: Mitigation, tag: string) => {
const handleAddTagToEntity = useCallback((mitigation: Mitigation, tag: string) => {
const updated: Mitigation = {
...assumption,
tags: assumption.tags ?
(!assumption.tags.includes(tag) ?
[...assumption.tags, tag] : assumption.tags) :
...mitigation,
tags: mitigation.tags ?
(!mitigation.tags.includes(tag) ?
[...mitigation.tags, tag] : mitigation.tags) :
[tag],
};
saveMitigation(updated);
}, [saveMitigation]);

const handleRemoveTagFromEntity = useCallback((assumption: Mitigation, tag: string) => {
const handleRemoveTagFromEntity = useCallback((mitigation: Mitigation, tag: string) => {
const updated: Mitigation = {
...assumption,
tags: assumption.tags?.filter(t => t !== tag),
...mitigation,
tags: mitigation.tags?.filter(t => t !== tag),
};
saveMitigation(updated);
}, [saveMitigation]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import TextContent from '@cloudscape-design/components/text-content';
import * as awsui from '@cloudscape-design/design-tokens';
import { css } from '@emotion/react';
import React, { FC, useCallback, useMemo, useState, useRef, useEffect, ReactNode, PropsWithChildren } from 'react';
import { v4 as uuidV4 } from 'uuid';
import { EditorProps } from './types';
import { DEFAULT_NEW_ENTITY_ID, DEFAULT_WORKSPACE_LABEL } from '../../../configs/constants';
import { useAssumptionLinksContext } from '../../../contexts/AssumptionLinksContext/context';
Expand Down Expand Up @@ -256,11 +255,9 @@ export const ThreatStatementEditorInner: FC<ThreatStatementEditorProps & { editi
const handleExampleClicked = useCallback((statement: TemplateThreatStatement) => {
setEditingStatement({
...statement,
...getNewThreatStatement(),
tags: [],
metadata: [],
displayOrder: -1,
numericId: -1,
id: uuidV4(),
});
const recommendedEditor = getRecommendedEditor(statement);
recommendedEditor && setEditor(recommendedEditor);
Expand All @@ -273,11 +270,9 @@ export const ThreatStatementEditorInner: FC<ThreatStatementEditorProps & { editi
const example = threatStatementExamples[randomNumber] as TemplateThreatStatement;
const statement = {
...example,
...getNewThreatStatement(),
tags: [],
metadata: [],
displayOrder: -1,
numericId: -1,
id: uuidV4(),
};

setEditingStatement(statement);
Expand Down
27 changes: 27 additions & 0 deletions packages/threat-composer/src/utils/getNewAssumption/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************************************************** */
import { DEFAULT_NEW_ENTITY_ID } from '../../configs';
import { Assumption } from '../../customTypes';

const getNewAssumption = (content: string = ''): Assumption => {
return {
id: DEFAULT_NEW_ENTITY_ID,
numericId: -1,
content: content,
};
};

export default getNewAssumption;
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const getNewThreatStatement = (): TemplateThreatStatement => {
return {
id: uuidV4(),
numericId: -1,
displayOrder: -1,
status: DEFAULT_THREAT_STATUS,
};
};
Expand Down

0 comments on commit 9fc987d

Please sign in to comment.