Skip to content

Commit c84efb5

Browse files
Merge pull request #11 from oslabs-beta/garrett/allstate
fixes generate code and drag and drop addchild issues
2 parents 36d1aaa + 7db34ab commit c84efb5

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

app/src/components/main/Canvas.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import { changeFocus, addChild,snapShotAction } from '../../redux/reducers/slice
1414

1515
function Canvas(props): JSX.Element {
1616
// const [state, dispatch] = useContext(StateContext);
17-
const state = useSelector(store => store.appState);
17+
// const {state} = useSelector(store => store.appState);
18+
// const contextParam = useSelector(store => store.contextSlice)
19+
const { state, contextParam } = useSelector((store) => ({
20+
state: store.appState,
21+
contextParam: store.contextSlice,
22+
}));
23+
1824
const dispatch = useDispatch();
1925
// const [newComp, setNewComp] = useState(false);
2026
// const [copiedChildrenArr, setCopiedChildrenArr] = useState([]);
@@ -89,40 +95,33 @@ function Canvas(props): JSX.Element {
8995
// stores a snapshot of state into the past array for UNDO. snapShotFunc is also invoked for nestable elements in DirectChildHTMLNestable.tsx
9096
const snapShotFunc = () => {
9197
// make a deep clone of state
92-
9398
const deepCopiedState = JSON.parse(JSON.stringify(state));
94-
9599
const focusIndex = state.canvasFocus.componentId - 1;
96-
console.log('about to dispatch snapshot')
97100
//pushes the last user action on the canvas into the past array of Component
98101
// state.components[focusIndex].past.push(deepCopiedState.components[focusIndex].children);
99-
100102
dispatch(snapShotAction({focusIndex: focusIndex, deepCopiedState: deepCopiedState}))
101-
console.log('left snapshot dispatch moving on ')
102103
};
103104

104105
// This hook will allow the user to drag items from the left panel on to the canvas
105106
const [{ isOver }, drop] = useDrop({
106107
accept: ItemTypes.INSTANCE,
107108
drop: (item: DragItem, monitor: DropTargetMonitor) => {
108109
const didDrop = monitor.didDrop();
109-
110110
// takes a snapshot of state to be used in UNDO and REDO cases
111-
console.log('about to snapshot')
112111
snapShotFunc();
113-
console.log('achieved snopshopt')
114112
// returns false for direct drop target
115113
console.log('diddrop', didDrop)
116114
if (didDrop) {
117115
return;
118116
}
119-
120117
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
121118
if (item.newInstance && item.instanceType !== "Component") {
122119
dispatch(addChild({
123120
type: item.instanceType,
124121
typeId: item.instanceTypeId,
125-
childId: null
122+
childId: null,
123+
contextParam: contextParam
124+
126125
}))
127126

128127
// dispatch({
@@ -167,7 +166,8 @@ function Canvas(props): JSX.Element {
167166
dispatch(addChild({
168167
type: item.instanceType,
169168
typeId: item.instanceTypeId,
170-
childId: null
169+
childId: null,
170+
contextParam: contextParam
171171
}))
172172
// dispatch({
173173
// type: 'ADD CHILD',

app/src/helperFunctions/generateCode.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ const generateCode = (
2222
rootComponents: number[],
2323
projectType: string,
2424
HTMLTypes: HTMLType[],
25-
tailwind: boolean
25+
tailwind: boolean,
26+
contextParam: any
2627
) => {
2728
const code = generateUnformattedCode(
2829
components,
2930
componentId,
3031
rootComponents,
3132
projectType,
3233
HTMLTypes,
33-
tailwind
34-
34+
tailwind,
35+
contextParam
3536
);
3637
return formatCode(code);
3738
};
@@ -43,7 +44,8 @@ const generateUnformattedCode = (
4344
rootComponents: number[],
4445
projectType: string,
4546
HTMLTypes: HTMLType[],
46-
tailwind: boolean
47+
tailwind: boolean,
48+
contextParam: any
4749
) => {
4850
const components = [...comps];
4951
// find the component that we're going to generate code for
@@ -377,7 +379,9 @@ const generateUnformattedCode = (
377379
if (projectType === 'Classic React') {
378380
//string to store all imports string for context
379381
let contextImports = '';
380-
const { allContext } = store.getState().contextSlice;
382+
// const { allContext } = store.getState().contextSlice;
383+
const { allContext } = contextParam;
384+
381385
for (const context of allContext) {
382386
contextImports += `import ${context.name}Provider from '../contexts/${context.name}.js'\n`;
383387
}

app/src/redux/reducers/slice/appStateSlice.ts

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,14 @@ const appStateSlice = createSlice({
271271
// childId: null
272272
// };
273273
const nextComponentId = state.nextComponentId + 1;
274-
newComponent.code = generateCode(
275-
state.components,
276-
newComponent.id,
277-
[...state.rootComponents],
278-
state.projectType,
279-
state.HTMLTypes,
280-
state.tailwind
281-
);
274+
// newComponent.code = generateCode(
275+
// state.components,
276+
// newComponent.id,
277+
// [...state.rootComponents],
278+
// state.projectType,
279+
// state.HTMLTypes,
280+
// state.tailwind
281+
// );
282282

283283
// return {
284284
// ...state,
@@ -289,11 +289,13 @@ const appStateSlice = createSlice({
289289
// },
290290
// state.components = components;
291291
// state.rootComponents = rootComponents;
292-
state.nextComponentId = nextComponentId
292+
// state.nextComponentId = nextComponentId
293293

294294

295295
},
296296
addChild: (state, action) => {
297+
console.log('entered the addchild1')
298+
297299
let parentComponentId: number;
298300
const {
299301
type,
@@ -305,9 +307,9 @@ const appStateSlice = createSlice({
305307
} else {
306308
parentComponentId = state.canvasFocus.componentId;
307309
}
308-
310+
309311
const components = [...state.components];
310-
312+
311313
const parentComponent = findComponent(components, parentComponentId);
312314
let componentName: string = '';
313315
let componentChildren: Object[] = [];
@@ -322,16 +324,16 @@ const appStateSlice = createSlice({
322324
if (type === 'Component') {
323325
const originalComponent = findComponent(state.components, typeId);
324326
if (childTypeExists('Component', parentComponentId, originalComponent))
325-
return state;
327+
return state;
326328
}
327-
329+
328330
let newName = state.HTMLTypes.reduce((name, el) => {
329331
if (typeId === el.id) {
330332
name = type === 'Component' ? componentName : el.tag;
331333
}
332334
return name;
333335
}, '');
334-
336+
335337
if (type === 'Route Link') {
336338
components.find((comp) => {
337339
if (comp.id === typeId) {
@@ -365,7 +367,7 @@ const appStateSlice = createSlice({
365367
// we also add a separator before any new child
366368
// if the newChild Element is an input or img type, delete the children key/value pair
367369
if (newChild.name === 'input' && newChild.name === 'img')
368-
delete newChild.children;
370+
delete newChild.children;
369371
let directParent;
370372
if (childId === null) {
371373
parentComponent.children.push(topSeparator);
@@ -392,31 +394,33 @@ const appStateSlice = createSlice({
392394
let addChildArray = components[canvasFocus.componentId - 1].children;
393395
addChildArray = manageSeparators.mergeSeparator(addChildArray, 1);
394396
if (directParent && directParent.name === 'separator')
395-
nextTopSeparatorId = manageSeparators.handleSeparators(
396-
addChildArray,
397-
'add'
397+
nextTopSeparatorId = manageSeparators.handleSeparators(
398+
addChildArray,
399+
'add'
398400
);
399-
components[canvasFocus.componentId - 1].children = addChildArray;
400-
401-
// parentComponent.code = generateCode(
402-
// components,
403-
// parentComponentId,
404-
// [...state.rootComponents],
405-
// state.projectType,
406-
// state.HTMLTypes,
407-
// state.tailwind
408-
// );
409-
410-
return {
411-
...state,
412-
components,
413-
nextChildId,
414-
canvasFocus,
415-
nextTopSeparatorId
416-
};
417-
},
418-
changeTailwind: (state, action) => {
419-
return { ...state, tailwind: action.payload }
401+
components[canvasFocus.componentId - 1].children = addChildArray;
402+
403+
404+
parentComponent.code = generateCode(
405+
components,
406+
parentComponentId,
407+
[...state.rootComponents],
408+
state.projectType,
409+
state.HTMLTypes,
410+
state.tailwind,
411+
action.payload.contextParam
412+
);
413+
console.log('entered the addchild2')
414+
// return {
415+
// ...state,
416+
// components,
417+
// nextChildId,
418+
// canvasFocus,
419+
// nextTopSeparatorId
420+
// };
421+
},
422+
changeTailwind: (state, action) => {
423+
return { ...state, tailwind: action.payload }
420424
},
421425
changeFocus: (state, action) => {
422426
const { componentId, childId } = action.payload;

0 commit comments

Comments
 (0)