Skip to content

Commit 394754f

Browse files
committed
added warnig messages for addComp deleteComp
1 parent 531bd13 commit 394754f

File tree

2 files changed

+116
-75
lines changed

2 files changed

+116
-75
lines changed

src/components/bottom/CodePreview.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ class CodePreview extends Component<CodePreviewProps, StateInt> {
3131
//if no then generate code and asign to a focus component
3232
componentDidMount() {
3333
if (this.props.focusComponent.changed) {
34-
this.setState({ readOnly: !this.state.readOnly });
3534
this.generateNewCode();
3635
}
3736
}
3837

3938
componentDidUpdate(prevProp: CodePreviewProps) {
4039
if (this.props.focusComponent.changed !== prevProp.focusComponent.changed) {
41-
this.setState({ readOnly: !this.state.readOnly });
4240
this.generateNewCode();
4341
}
4442
}
@@ -133,7 +131,8 @@ class CodePreview extends Component<CodePreviewProps, StateInt> {
133131
<ul>
134132
<li>Class or State toggles are changed</li>
135133
<li>
136-
HTML Elements, Props, or Children are added to this component
134+
HTML Elements, Props, or Children are added/removed to/from the
135+
current component
137136
</li>
138137
</ul>
139138
</div>

src/reducers/leftReducers.ts

Lines changed: 114 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import getSelectable from '../helperFunctions/getSelectable';
44
import {
55
ComponentInt,
66
ApplicationStateInt,
7-
ChildInt,
7+
ChildInt
88
} from '../interfaces/Interfaces';
99
import { initialComponentState } from './initialState';
1010
import { createHistory } from '../helperFunctions/createHistory';
1111
import { getSize } from '../utils/htmlElements.util';
12-
import { generateNewCode } from '../helperFunctions/generateNewCode'
12+
import { generateNewCode } from '../helperFunctions/generateNewCode';
1313

1414
export const addChild = (
1515
state: ApplicationStateInt,
@@ -43,7 +43,7 @@ export const addChild = (
4343
);
4444

4545
// parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
46-
let parentComponent;
46+
let parentComponent: ComponentInt;
4747

4848
// conditional if adding an HTML component
4949
if (childType === 'COMP') {
@@ -52,6 +52,23 @@ export const addChild = (
5252
);
5353
}
5454

55+
let check: boolean;
56+
if (!state.codeReadOnly) {
57+
if (childType === 'COMP') {
58+
check = window.confirm(
59+
`Are you sure you want to add ${strippedTitle} as a child to ${parentComponent.title} while the program is in "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the ${parentComponent.title} component will be overridden!`
60+
);
61+
} else {
62+
check = window.confirm(
63+
`Are you sure you want to add an HTML element to ${view.title} component while the program is in "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the ${view.title} component will be overridden!`
64+
);
65+
}
66+
if (!check) {
67+
return {
68+
...state
69+
};
70+
}
71+
}
5572
interface htmlElemPositionInt {
5673
width: number;
5774
height: number;
@@ -124,7 +141,8 @@ export const addChild = (
124141
focusComponent: component, // refresh the focus component so we have the new child
125142
history,
126143
historyIndex,
127-
future
144+
future,
145+
codeReadOnly: true
128146
};
129147
};
130148

@@ -218,41 +236,41 @@ export const addComponent = (
218236
};
219237

220238
export const changeFocusComponent = (
221-
state: ApplicationStateInt,
222-
{ title = state.focusComponent.title }: { title: string }
223-
) => {
224-
/** ****************
225-
* if the prm TITLE is a blank Object it means REFRESH focusd Components.
226-
* sometimes we update state like adding Children/Props etc and we want those changes to be reflected in focus component
227-
************************************************* */
228-
const newFocusComp: ComponentInt = state.components.find(
229-
(comp: ComponentInt) => comp.title === title
239+
state: ApplicationStateInt,
240+
{ title = state.focusComponent.title }: { title: string }
241+
) => {
242+
/** ****************
243+
* if the prm TITLE is a blank Object it means REFRESH focusd Components.
244+
* sometimes we update state like adding Children/Props etc and we want those changes to be reflected in focus component
245+
************************************************* */
246+
const newFocusComp: ComponentInt = state.components.find(
247+
(comp: ComponentInt) => comp.title === title
248+
);
249+
250+
// set the "focus child" to the focus child of this particular component .
251+
252+
let newFocusChild: ChildInt | any; // check if the components has a child saved as a Focus child
253+
if (newFocusComp.focusChildId > 0) {
254+
newFocusChild = newFocusComp.childrenArray.find(
255+
(child: ChildInt) => child.childId === newFocusComp.focusChildId
230256
);
231-
232-
// set the "focus child" to the focus child of this particular component .
233-
234-
let newFocusChild: ChildInt | any; // check if the components has a child saved as a Focus child
235-
if (newFocusComp.focusChildId > 0) {
236-
newFocusChild = newFocusComp.childrenArray.find(
237-
(child: ChildInt) => child.childId === newFocusComp.focusChildId
238-
);
239-
}
240-
241-
if (!newFocusChild) {
242-
newFocusChild = cloneDeep(state.initialApplicationFocusChild);
243-
}
244-
245-
const result = getSelectable(newFocusComp, state.components);
246-
247-
return {
248-
...state,
249-
editMode: -1,
250-
focusComponent: newFocusComp,
251-
selectableChildren: result.selectableChildren,
252-
ancestors: result.ancestors,
253-
focusChild: newFocusChild,
254-
};
257+
}
258+
259+
if (!newFocusChild) {
260+
newFocusChild = cloneDeep(state.initialApplicationFocusChild);
261+
}
262+
263+
const result = getSelectable(newFocusComp, state.components);
264+
265+
return {
266+
...state,
267+
editMode: -1,
268+
focusComponent: newFocusComp,
269+
selectableChildren: result.selectableChildren,
270+
ancestors: result.ancestors,
271+
focusChild: newFocusChild
255272
};
273+
};
256274

257275
//change image source
258276
export const changeImageSource = (
@@ -266,7 +284,7 @@ export const changeImageSource = (
266284
imageSource,
267285
history,
268286
historyIndex,
269-
future,
287+
future
270288
};
271289
};
272290

@@ -295,7 +313,6 @@ export const deleteChild = (
295313
window.alert('Cannot delete root child of a component');
296314
return state;
297315
}
298-
299316
// make a DEEP copy of the parent component (the one thats about to loose a child)
300317
const parentComponentCopy: any = cloneDeep(
301318
state.components.find((comp: ComponentInt) => comp.id === parentId)
@@ -305,6 +322,16 @@ export const deleteChild = (
305322
const indexToDelete = parentComponentCopy.childrenArray.findIndex(
306323
(elem: ChildInt) => elem.childId === childId
307324
);
325+
if (!state.codeReadOnly) {
326+
const check = window.confirm(
327+
`Are you sure you want to delete ${parentComponentCopy.childrenArray[indexToDelete].componentName} as a child to ${parentComponentCopy.title} while the program is in "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the ${parentComponentCopy.title} component will be overridden!`
328+
);
329+
if (!check) {
330+
return {
331+
...state
332+
};
333+
}
334+
}
308335
if (indexToDelete < 0) {
309336
return window.alert('No such child component found');
310337
}
@@ -338,7 +365,8 @@ export const deleteChild = (
338365
] || cloneDeep(state.initialApplicationFocusChild), // guard in case final child is deleted
339366
history,
340367
historyIndex,
341-
future
368+
future,
369+
codeReadOnly: true
342370
};
343371
};
344372

@@ -351,17 +379,31 @@ export const deleteComponent = (
351379
const compName = state.components.filter(
352380
(value: ComponentInt) => value.id === componentId
353381
);
354-
//confimation window to see if user really wants to delete component
355-
const result = window.confirm(
356-
`Are you sure you want to delete ${compName[0].title}?`
357-
);
358-
//if cancelled, return focus to current selected component
359-
if (!result) {
360-
return {
361-
...state,
362-
focusComponent: compName[0]
363-
};
382+
383+
//if the program is in the edit mode
384+
if (!state.codeReadOnly) {
385+
const check = window.confirm(
386+
`Are you sure you want to delete ${compName[0].title} while the program is in the "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the related components will be overridden!`
387+
);
388+
if (!check) {
389+
return {
390+
...state,
391+
focusComponent: compName[0]
392+
};
393+
}
394+
} else {
395+
//confirmation window to see if user really wants to delete component
396+
const result = window.confirm(
397+
`Are you sure you want to delete ${compName[0].title}?\n\nAll of the changes to the "Code Preview" for the related components will be overridden!`
398+
);
399+
if (!result) {
400+
return {
401+
...state,
402+
focusComponent: compName[0]
403+
};
404+
}
364405
}
406+
//if cancelled, return focus to current selected component
365407
//if app is selected, return state
366408
//is this really necessary if the App component is disabled from being deleted? -Tony
367409
if (componentId === 1) {
@@ -390,7 +432,8 @@ export const deleteComponent = (
390432
components: componentsCopy,
391433
history,
392434
historyIndex,
393-
future
435+
future,
436+
codeReadOnly: true
394437
};
395438
};
396439

@@ -437,24 +480,24 @@ export const editComponent = (
437480
};
438481

439482
export const exportFilesSuccess = (
440-
state: ApplicationStateInt,
441-
{ status, dir }: { status: boolean; dir: string }
442-
) => ({
443-
...state,
444-
successOpen: status,
445-
appDir: dir,
446-
loading: false,
447-
});
448-
449-
export const exportFilesError = (
450-
state: ApplicationStateInt,
451-
{ status, err }: { status: boolean; err: string }
452-
) => ({
453-
...state,
454-
errorOpen: status,
455-
appDir: err,
456-
loading: false,
457-
});
483+
state: ApplicationStateInt,
484+
{ status, dir }: { status: boolean; dir: string }
485+
) => ({
486+
...state,
487+
successOpen: status,
488+
appDir: dir,
489+
loading: false
490+
});
491+
492+
export const exportFilesError = (
493+
state: ApplicationStateInt,
494+
{ status, err }: { status: boolean; err: string }
495+
) => ({
496+
...state,
497+
errorOpen: status,
498+
appDir: err,
499+
loading: false
500+
});
458501

459502
//Reducer that toggles the component class
460503
export const toggleComponentClass = (
@@ -483,7 +526,6 @@ export const toggleComponentClass = (
483526
};
484527
};
485528

486-
487529
//Reducer that toggles the component statefulness
488530
export const toggleComponentState = (
489531
state: ApplicationStateInt,
@@ -518,11 +560,11 @@ export const toggleEditMode = (
518560
) => {
519561
if (id === 1) {
520562
return {
521-
...state,
563+
...state
522564
};
523565
}
524566
return {
525567
...state,
526-
editMode: id,
568+
editMode: id
527569
};
528570
};

0 commit comments

Comments
 (0)