Skip to content

Commit 1aa2988

Browse files
authored
Merge pull request #11 from oslabs-beta/ts-appstate
Ts appstate
2 parents c7e0a65 + 03bc919 commit 1aa2988

File tree

12 files changed

+271
-184
lines changed

12 files changed

+271
-184
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"sourceType": "module"
99
},
1010
"plugins": ["import", "react", "jest", "jsx-a11y", "babel"],
11-
"parser": "babel-eslint",
11+
"parser": "@babel/eslint-parser",
1212
"env": {
1313
"browser": true,
1414
"node": true,

__tests__/componentReducer.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import reducer from '../app/src/redux/reducers/slice/appStateSlice';
22
import { State, Action } from '../app/src/interfaces/Interfaces';
33
import { initialState } from '../app/src/redux/reducers/slice/appStateSlice';
4+
import styled from '@emotion/styled';
45

56
describe('componentReducer Test', () => {
67
let state: State = initialState;
@@ -173,15 +174,15 @@ describe('componentReducer Test', () => {
173174
expect(state.canvasFocus.childId).toEqual(null);
174175
});
175176
});
176-
177+
177178
// TEST 'UPDATE CSS'
178-
describe('updateCss', () => {
179+
xdescribe('updateCss', () => {
179180
it('should add style to focused component', () => {
180181
const action: Action = {
181182
type: 'appState/updateCss',
182183
payload: {
183184
style: {
184-
backgroundColor: 'gray'
185+
backgroundColor: 'gray',
185186
},
186187
contextParam: {
187188
allContext: []
@@ -192,10 +193,13 @@ describe('componentReducer Test', () => {
192193
const styledComp = state.components.find(
193194
(comp) => comp.id === state.canvasFocus.componentId
194195
);
196+
console.log('styledcomp',styledComp.style)
195197
// expect the style property on targeted comp to equal style property in payload
198+
if (styledComp) {
196199
expect(styledComp.style.backgroundColor).toEqual(
197200
action.payload.style.backgroundColor
198201
);
202+
}
199203
});
200204
});
201205

__tests__/contextReducer.test.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import subject from '../app/src/redux/reducers/slice/contextReducer';
1+
import subject from '../app/src/redux/reducers/slice/contextReducer.ts';
22

33
describe('contextReducer test', () => {
44
let state;
55

66
beforeEach(() => {
77
state = {
8-
allContext: []
8+
allContext: [],
99
};
1010
});
1111

@@ -25,16 +25,16 @@ describe('contextReducer test', () => {
2525
const action = {
2626
type: 'context/addContext',
2727
payload: {
28-
name: 'Theme Context'
29-
}
28+
name: 'Theme Context',
29+
},
3030
};
3131

3232
it('adds a context', () => {
3333
const { allContext } = subject(state, action);
3434
expect(allContext[0]).toEqual({
3535
name: 'Theme Context',
3636
values: [],
37-
components: []
37+
components: [],
3838
});
3939
});
4040

@@ -49,7 +49,7 @@ describe('contextReducer test', () => {
4949
});
5050
});
5151

52-
//OLD ADD CONTEX TEST
52+
// OLD ADD CONTEX TEST
5353

5454
// describe('ADD_CONTEXT', () => {
5555
// const action = {
@@ -86,9 +86,9 @@ describe('contextReducer test', () => {
8686
{
8787
name: 'Theme Context',
8888
values: [],
89-
components: []
90-
}
91-
]
89+
components: [],
90+
},
91+
],
9292
};
9393
});
9494

@@ -97,8 +97,8 @@ describe('contextReducer test', () => {
9797
payload: {
9898
name: 'Theme Context',
9999
inputKey: 'Theme Color',
100-
inputValue: 'Dark'
101-
}
100+
inputValue: 'Dark',
101+
},
102102
};
103103

104104
it('adds a key-value pair to values array of the specified context', () => {
@@ -123,21 +123,21 @@ describe('contextReducer test', () => {
123123
{
124124
name: 'Theme Context',
125125
values: [],
126-
components: []
126+
components: [],
127127
},
128128
{
129129
name: 'To be deleted',
130130
values: [],
131-
components: []
132-
}
133-
]
131+
components: [],
132+
},
133+
],
134134
};
135135

136136
action = {
137137
type: 'context/deleteContext',
138138
payload: {
139-
name: 'Theme Context'
140-
}
139+
name: 'Theme Context',
140+
},
141141
};
142142
});
143143

@@ -161,22 +161,22 @@ describe('contextReducer test', () => {
161161
{
162162
name: 'Theme Context',
163163
values: [],
164-
components: []
165-
}
166-
]
164+
components: [],
165+
},
166+
],
167167
};
168168
});
169169

170170
const action = {
171171
type: 'context/addComponentToContext',
172172
payload: {
173173
context: {
174-
name: 'Theme Context'
174+
name: 'Theme Context',
175175
},
176176
component: {
177-
name: 'Main Component'
178-
}
179-
}
177+
name: 'Main Component',
178+
},
179+
},
180180
};
181181

182182
it('adds a new component to the specified context', () => {
Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChildElement } from '../interfaces/Interfaces';
1+
import { ChildElement, ManageSeparators } from '../interfaces/Interfaces';
22

33
const separator = {
44
id: 1000,
@@ -11,79 +11,87 @@ const separator = {
1111
framework: '',
1212
nestable: true
1313
};
14-
const manageSeparators = {};
15-
manageSeparators.nextTopSeparatorId = 1000;
16-
// this function checks for two separators in a row or missing separators and adds/removes as needed
17-
manageSeparators.handleSeparators = (arr: object[], str: string) => {
18-
if (
19-
(str === 'delete' || str === 'change position') &&
20-
arr.length === 1 &&
21-
arr[0].name === 'separator'
22-
) {
23-
arr.splice(0, 1);
24-
}
25-
for (let index = 0; index < arr.length; index++) {
26-
if (
27-
arr[index].name === 'separator' &&
28-
arr[index + 1].name === 'separator'
29-
) {
30-
arr.splice(index, 1); // removes extra separator from array
31-
}
32-
// check for duplicated separator at the end of array and remove it if separator is at the last index
33-
if (arr[arr.length - 1].name === 'separator') arr.splice(arr.length - 1, 1);
34-
// check for missing separators
14+
const manageSeparators: ManageSeparators = {
15+
nextTopSeparatorId: 1000,
16+
// this function checks for two separators in a row or missing separators and adds/removes as needed
17+
handleSeparators: (arr: ChildElement[], str?) => {
3518
if (
36-
arr[index].name !== 'separator' &&
37-
(index === 0 || arr[index - 1].name !== 'separator')
19+
(str === 'delete' || str === 'change position') &&
20+
arr.length === 1 &&
21+
arr[0].name === 'separator'
3822
) {
39-
// initialize topSeparator inside the if condition so that every time this condition evaluated to true,
40-
// a new topSeparator with incremented id will be created
41-
const topSeparator: ChildElement = {
42-
type: 'HTML Element',
43-
typeId: separator.id,
44-
name: 'separator',
45-
childId: manageSeparators.nextTopSeparatorId,
46-
style: separator.style,
47-
children: []
48-
};
49-
// add a topSeparator before the element that does not have one
50-
arr.splice(index, 0, topSeparator);
51-
// update this value in state
52-
manageSeparators.nextTopSeparatorId += 1;
23+
arr.splice(0, 1);
5324
}
54-
// check is length is > 0 or it is a nested element
55-
if (
56-
arr[index].name !== 'input' &&
57-
arr[index].name !== 'img' &&
58-
arr[index].children.length
59-
) {
60-
// recursive call if children array
61-
str === 'delete' || str === 'change position'
62-
? manageSeparators.handleSeparators(arr[index].children, str)
63-
: manageSeparators.handleSeparators(arr[index].children);
25+
for (let index = 0; index < arr.length; index++) {
26+
if (
27+
arr[index].name === 'separator' &&
28+
arr[index + 1].name === 'separator'
29+
) {
30+
arr.splice(index, 1); // removes extra separator from array
31+
}
32+
// check for duplicated separator at the end of array and remove it if separator is at the last index
33+
if (arr[arr.length - 1].name === 'separator') arr.splice(arr.length - 1, 1);
34+
// check for missing separators // cooment
35+
if (
36+
arr[index].name !== 'separator' &&
37+
(index === 0 || arr[index - 1].name !== 'separator')
38+
) {
39+
// initialize topSeparator inside the if condition so that every time this condition evaluated to true,
40+
// a new topSeparator with incremented id will be created
41+
const topSeparator: ChildElement = {
42+
type: 'HTML Element',
43+
typeId: separator.id,
44+
name: 'separator',
45+
childId: manageSeparators.nextTopSeparatorId,
46+
style: separator.style,
47+
attributes: {}, // Added
48+
events: {}, // Added
49+
stateProps: [], // Added
50+
passedInProps: [], // Added
51+
children: [],
52+
53+
};
54+
// add a topSeparator before the element that does not have one
55+
arr.splice(index, 0, topSeparator);
56+
// update this value in state
57+
manageSeparators.nextTopSeparatorId += 1;
58+
}
59+
// check is length is > 0 or it is a nested element
60+
if (
61+
arr[index].name !== 'input' &&
62+
arr[index].name !== 'img' &&
63+
arr[index].children?.length
64+
) {
65+
// recursive call if children array
66+
str === 'delete' || str === 'change position'
67+
? manageSeparators.handleSeparators(arr[index].children, str)
68+
: manageSeparators.handleSeparators(arr[index].children);
69+
}
6470
}
71+
return manageSeparators.nextTopSeparatorId;
72+
73+
},
74+
75+
// this function replaces separators onto which an element is dropped with the element itself
76+
mergeSeparator: (arr: ChildElement[], index: number) => {
77+
return arr.map((child) => {
78+
// Added additional nested types for lists
79+
if (
80+
(child.name === 'div' ||
81+
child.name === 'form' ||
82+
child.name === 'ol' ||
83+
child.name === 'ul') &&
84+
child?.children?.length
85+
) {
86+
const divContents = manageSeparators.mergeSeparator(
87+
child.children,
88+
index
89+
)
90+
return { ...child, children: divContents };
91+
} else if (child.name === 'separator' && child?.children?.length) {
92+
return child.children[index];
93+
} else return child;
94+
});
6595
}
66-
return manageSeparators.nextTopSeparatorId;
67-
};
68-
// this function replaces separators onto which an element is dropped with the element itself
69-
manageSeparators.mergeSeparator = (arr: object[], index: number) => {
70-
return arr.map((child) => {
71-
// Added additional nested types for lists
72-
if (
73-
(child.name === 'div' ||
74-
child.name === 'form' ||
75-
child.name === 'ol' ||
76-
child.name === 'ul') &&
77-
child.children.length
78-
) {
79-
const divContents = manageSeparators.mergeSeparator(
80-
child.children,
81-
index
82-
);
83-
return { ...child, children: divContents };
84-
} else if (child.name === 'separator' && child.children.length) {
85-
return child.children[index];
86-
} else return child;
87-
});
88-
};
96+
}
8997
export default manageSeparators;

app/src/interfaces/Interfaces.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export interface State {
77
rootComponents: number[];
88
projectType: string;
99
config?: {};
10-
separator: ChildElement;
10+
separator?: ChildElement;
1111
canvasFocus: { componentId: number; childId: number | null };
1212
nextComponentId: number;
1313
nextTopSeparatorId: number;
14-
nextBottomSeparatorId: number;
14+
nextBottomSeparatorId?: number;
1515
nextChildId: number;
1616
HTMLTypes: HTMLType[];
1717
tailwind: boolean;
@@ -46,6 +46,7 @@ export interface Component {
4646
useStateCodes: string[];
4747
useContext?: object;
4848
passedInProps: StateProp[];
49+
stateUsed?: object;
4950
}
5051
export interface StateProp {
5152
id: string;
@@ -75,7 +76,8 @@ export interface HTMLType {
7576
style: object;
7677
placeHolderShort: string | JSX.Element;
7778
placeHolderLong: string;
78-
icon: any;
79+
// ? == optional type part of icon, cant comment out icon and it works
80+
icon?: any;
7981
framework: string;
8082
nestable: boolean;
8183
}
@@ -103,3 +105,9 @@ export interface AddRoutes {
103105
id: number;
104106
name: string;
105107
}
108+
109+
export interface ManageSeparators {
110+
mergeSeparator: (arg1:any, arg2:any) => any; // update specificity
111+
handleSeparators: (arg1: any, arg2?: any) => any; // update specificity
112+
nextTopSeparatorId: number
113+
}

app/src/reducers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const initialState = {code: ``}
2-
const rootReducer = (state = initialState, action) => {
1+
const initialState = { code: '' };
2+
const rootReducer = (action, state = initialState) => {
33
switch (action.type) {
44
case 'CODE':
55
return { ...state, code: action.payload };
66
case 'ADD_CHILD':
77
return { ...state, child: action.payload };
8-
case 'RESET_STATE':
9-
return {...state, code: {} };
8+
case 'RESET_STATE':
9+
return { ...state, code: {} };
1010
default:
1111
return state;
1212
}

0 commit comments

Comments
 (0)