Skip to content

Commit a647165

Browse files
final commit before merge
1 parent a9a6891 commit a647165

File tree

1 file changed

+41
-100
lines changed

1 file changed

+41
-100
lines changed

app/src/reducers/componentReducer.ts

Lines changed: 41 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ const reducer = (state: State, action: Action) => {
751751
components,
752752
state.canvasFocus.componentId
753753
);
754+
//will add update StateProps to current components' array
754755
currComponent.stateProps.push(action.payload.newState);
755756
currComponent.useStateCodes = updateUseStateCodes(currComponent);
756757
currComponent.stateProps.push(action.payload.setNewState);
@@ -761,7 +762,6 @@ const reducer = (state: State, action: Action) => {
761762
state.projectType,
762763
state.HTMLTypes
763764
);
764-
console.log('action.payload', action.payload)
765765
return { ...state, components};
766766
}
767767
/*
@@ -773,10 +773,9 @@ const reducer = (state: State, action: Action) => {
773773
const components = [...state.components];
774774
const currComponent = findComponent(
775775
components,
776-
//legacyPD - tom
777-
// need to change this to match the id from the tree
778776
state.canvasFocus.componentId
779777
);
778+
//find the parent for deleting instances of where the parent is passing props to children
780779
let parent;
781780
for (let i = 0; i < components.length; i++){
782781
let currComponent = components[i]
@@ -788,30 +787,36 @@ const reducer = (state: State, action: Action) => {
788787
}
789788
}
790789

790+
//search for whether the child exists in the parent's children array
791+
//if so update the passed in props child element with the updates passed in props
791792
parent.children.forEach((child) => {
792793
if (child.name === currComponent.name) {
793794
child.passedInProps.push(action.payload.passedInProps);
794795
}
795796
})
796797

797798

798-
// do a check if prop already exists in passed in props
799+
//prevents passing in props more than one time to the current component
799800
for (let i = 0; i < currComponent.passedInProps.length; i++) {
800801
let curr = currComponent.passedInProps[i];
801802
if (curr.id === action.payload.passedInProps.id) {
802803
return { ...state, components};
803804
}
804805
}
806+
// check each components passedInProps property and updating there as well.
805807
currComponent.passedInProps.push(action.payload.passedInProps);
808+
809+
//update the import codes for the current component
806810
currComponent.useStateCodes = updateUseStateCodes(currComponent);
807-
811+
//update code preview for current component
808812
currComponent.code = generateCode(
809813
components,
810814
state.canvasFocus.componentId,
811815
[...state.rootComponents],
812816
state.projectType,
813817
state.HTMLTypes
814818
);
819+
//update code preview for parent component (since we have added it to the children array)
815820
parent.code = generateCode(
816821
components,
817822
parent.id,
@@ -830,7 +835,7 @@ const reducer = (state: State, action: Action) => {
830835
state.canvasFocus.componentId
831836
);
832837

833-
//find the parent
838+
//find the parent of the component that we are deleting from
834839
let parent;
835840
for (let i = 0; i < components.length; i++){
836841
let currComponent = components[i]
@@ -843,7 +848,7 @@ const reducer = (state: State, action: Action) => {
843848
}
844849

845850
//deletes all instances of passedInProps from the children arrays of the current Component
846-
const pPKillahChildren = (currComponent) => {
851+
const deletePassedInPropsChildren = (currComponent) => {
847852
// when there are no children, return up a level
848853
if (!currComponent.children) return;
849854
currComponent.passedInProps?.forEach((prop, i)=> {
@@ -853,13 +858,14 @@ const reducer = (state: State, action: Action) => {
853858
child.passedInProps.forEach((prop, k) => {
854859
if(prop.id === action.payload.rowId) {
855860
child.passedInProps.splice(k, 1);
856-
pPKillahChildren(child);
861+
deletePassedInPropsChildren(child);
857862
}
858863
})
859864
})
860865
}
861866
}
862867
});
868+
//for every component we update, generate new code
863869
currComponent.code = generateCode(
864870
components,
865871
currComponent.id,
@@ -868,9 +874,8 @@ const reducer = (state: State, action: Action) => {
868874
state.HTMLTypes
869875
)
870876
}
871-
872-
const pPKillah = (myComponent) => {
873-
console.log({myComponent})
877+
//delete from the components passedInProps array
878+
const deletePassedInProps = (myComponent) => {
874879
if (myComponent.children.filter((el) => el.type === 'Component').length === 0) {
875880
if (myComponent.passedInProps.length > 0) {
876881
myComponent.passedInProps.forEach((prop, index) => {
@@ -895,8 +900,7 @@ const reducer = (state: State, action: Action) => {
895900
myComponent.children.filter(el => el.type === "Component").forEach((child, i) => {
896901
let next = components.find(comp =>
897902
comp.id === child.typeId);
898-
console.log(myComponent);
899-
pPKillah(next);
903+
deletePassedInProps(next);
900904
})
901905
}
902906
})
@@ -910,66 +914,17 @@ const reducer = (state: State, action: Action) => {
910914
);
911915
}
912916

913-
pPKillahChildren(parent);
914-
pPKillah(currComponent);
917+
deletePassedInPropsChildren(parent);
918+
deletePassedInProps(currComponent);
915919

916-
/*
917-
Check whether the current component selected has passed in props and splices out that
918-
piece of state from the array.
919-
*/
920-
// let index;
921-
// //find the index of the passedInProps array of the current component
922-
// for (let i = 0; i < currComponent.passedInProps.length; i++) {
923-
// if (currComponent.passedInProps[i].id === action.payload.rowId) {
924-
// index = i;
925-
// console.log({currComponent}, 'in delete passedi n props')
926-
// break;
927-
// }
928-
// }
929-
// // this loop identfies the parent so we can change the children array and modify the passedInProps
930-
// //AND we can splice the prop out of the child component's passedInProps
931-
// let parent;
932-
// for (let i = 0; i < components.length; i++){
933-
// let currComponent = components[i]
934-
// for (let j = 0; j < currComponent.children.length; j++) {
935-
// let currChild = currComponent.children[j];
936-
// if (currChild.typeId === state.canvasFocus.componentId) {
937-
// parent = currComponent;
938-
// }
939-
// currComponent.passedInProps?.forEach((prop, i) => {
940-
// if (prop.id === action.payload.rowId) {
941-
// currComponent.passedInProps.splice(i, 1);
942-
// }
943-
// })
944920

945-
// }
946-
// }
947-
// //this loops splices the passedInProps from the parent components children array (this is needed
948-
// // for the code preview)
949-
// parent.children.forEach((child) => {
950-
// if (child.name === currComponent.name) {
951-
// child.passedInProps.splice(index, 1);
952-
// }
953-
// })
954-
955-
956-
// currComponent.passedInProps.splice(index, 1);
957-
// currComponent.useStateCodes = updateUseStateCodes(currComponent);
958-
// currComponent.code = generateCode(
959-
// components,
960-
// state.canvasFocus.componentId,
961-
// [...state.rootComponents],
962-
// state.projectType,
963-
// state.HTMLTypes
964-
// );
965921
parent.code = generateCode(
966922
components,
967923
parent.id,
968924
[...state.rootComponents],
969925
state.projectType,
970926
state.HTMLTypes
971927
);
972-
console.log(components)
973928
return { ...state, components};
974929
}
975930

@@ -987,16 +942,13 @@ const reducer = (state: State, action: Action) => {
987942
// in the passedInProps array within the children array of the component
988943
// using the action.payload.rowId (variable name) and action.payload.otherId (setVariable name))
989944
components.forEach((component) => {
990-
console.log('component in for loop', component);
991945
//find all instances of state within child elements and delete state
992946

993947
component.children.forEach((child) => {
994-
console.log('child.passedinprops before delete',child.passedInProps)
995948
if (child.type === 'Component') {
996949
child.passedInProps.forEach((prop, i) => {
997950
if (prop.id === action.payload.rowId || prop.id === action.payload.otherId) {
998951
child.passedInProps.splice(i, 1);
999-
console.log('child.passedinprops after delete',child.passedInProps)
1000952
}
1001953
});
1002954
}
@@ -1008,9 +960,6 @@ const reducer = (state: State, action: Action) => {
1008960
// using the action.payload.rowId (variable name) and action.payload.otherId (setVariable name))
1009961
for (let i = 0; i < component.passedInProps?.length; i++) {
1010962
if (component.passedInProps[i]['id'] === action.payload.rowId || component.passedInProps[i]['id'] === action.payload.otherId) {
1011-
console.log('prop.id', component.passedInProps[i]['id']);
1012-
console.log('rowid',action.payload.rowId)
1013-
console.log('otherid',action.payload.otherId)
1014963
component.passedInProps.splice(i,1);
1015964
i--;
1016965
}
@@ -1021,26 +970,28 @@ const reducer = (state: State, action: Action) => {
1021970
// check if a useContext if created and if the useContext contains the providerId
1022971
// we then delete from the set, statesFromProvider, the row id, and regenerate the code
1023972
// Ex: useContext {1: {statesFromProvider: Set, compLink, compText}, 2 : ..., 3 : ...}
1024-
// if(component.useContext && component.useContext[state.canvasFocus.componentId ]) {
1025-
// component.useContext[state.canvasFocus.componentId].statesFromProvider.delete(action.payload.rowId);
1026-
// // iterate over children to see where it is being used, then reset that compText/compLink/useState
1027-
// for (let child of component.children) {
1028-
// if (child.stateUsed) {
1029-
// if (child.stateUsed.compTextProviderId === currComponent.id && child.stateUsed.compTextPropsId === action.payload.rowId) {
1030-
// child.attributes.compText = '';
1031-
// delete child.stateUsed.compText;
1032-
// delete child.stateUsed.compTextProviderId;
1033-
// delete child.stateUsed.compTextPropsId;
1034-
// }
1035-
// if (child.stateUsed.compLinkProviderId === currComponent.id && child.stateUsed.compLinkPropsId === action.payload.rowId) {
1036-
// child.attributes.compLink = '';
1037-
// delete child.stateUsed.compLink;
1038-
// delete child.stateUsed.compLinkProviderId;
1039-
// delete child.stateUsed.compLinkPropsId;
1040-
// }
1041-
// }
1042-
// }
1043-
// }
973+
if(component.useContext && component.useContext[state.canvasFocus.componentId ]) {
974+
component.useContext[state.canvasFocus.componentId].statesFromProvider.delete(action.payload.rowId);
975+
// iterate over children to see where it is being used, then reset that compText/compLink/useState
976+
for (let child of component.children) {
977+
if (child.stateUsed) {
978+
if (child.stateUsed.compTextProviderId === currComponent.id && child.stateUsed.compTextPropsId === action.payload.rowId) {
979+
child.attributes.compText = '';
980+
delete child.stateUsed.compText;
981+
delete child.stateUsed.compTextProviderId;
982+
delete child.stateUsed.compTextPropsId;
983+
}
984+
if (child.stateUsed.compLinkProviderId === currComponent.id && child.stateUsed.compLinkPropsId === action.payload.rowId) {
985+
child.attributes.compLink = '';
986+
delete child.stateUsed.compLink;
987+
delete child.stateUsed.compLinkProviderId;
988+
delete child.stateUsed.compLinkPropsId;
989+
}
990+
}
991+
}
992+
}
993+
994+
// find parent
1044995
let parent;
1045996
for (let i = 0; i < components.length; i++){
1046997
let currComponent = components[i]
@@ -1051,7 +1002,6 @@ const reducer = (state: State, action: Action) => {
10511002
}
10521003
}
10531004
}
1054-
console.log('ben parent', {parent})
10551005
if (parent) {
10561006
parent.code = generateCode(
10571007
components,
@@ -1070,16 +1020,7 @@ const reducer = (state: State, action: Action) => {
10701020
state.projectType,
10711021
state.HTMLTypes
10721022
);
1073-
console.log({component});
10741023
});
1075-
// currComponent.useStateCodes = updateUseStateCodes(currComponent);
1076-
// currComponent.code = generateCode(
1077-
// components,
1078-
// state.canvasFocus.componentId,
1079-
// [...state.rootComponents],
1080-
// state.projectType,
1081-
// state.HTMLTypes
1082-
// );
10831024
return { ...state, components};
10841025
}
10851026
default:

0 commit comments

Comments
 (0)