Skip to content

Commit 3a2c4e4

Browse files
committed
Merge branch 'stretch' of https://github.com/oslabs-beta/ReacType into testing
2 parents d7b855a + cc205f5 commit 3a2c4e4

File tree

9 files changed

+151
-29
lines changed

9 files changed

+151
-29
lines changed

src/components/left/HTMLComponentPanel.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Grid from '@material-ui/core/Grid';
1212
interface HTMLCompPropsInt {
1313
classes: any;
1414
addChild: any;
15+
native: boolean;
1516
}
1617

1718
interface StateInt {
@@ -35,7 +36,6 @@ class HTMLComponentPanel extends Component<HTMLCompPropsInt, StateInt> {
3536

3637
render(): JSX.Element {
3738
const { classes } = this.props;
38-
3939
// refactor of HTML elements panel
4040
const elementButtons = [
4141
{
@@ -72,7 +72,9 @@ class HTMLComponentPanel extends Component<HTMLCompPropsInt, StateInt> {
7272
className={classes.label}
7373
aria-label={element.name}
7474
size="medium"
75-
onClick={() => this.handleCreateHTMLChild(element.name)}
75+
onClick={() => {
76+
this.handleCreateHTMLChild(element.name);
77+
}}
7678
>
7779
<span className={classes.elemName}>{element.name}</span>
7880
{element.button}

src/components/left/LeftColExpansionPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const LeftColExpansionPanel = (props: LeftColExpPanPropsInt) => {
100100
(component: ComponentInt) => component.title === title
101101
);
102102

103-
const parentKeys: any[] = [];
103+
const parentKeys: string[] = [];
104104
if (focusComponent.props.length > 0) {
105105
focusComponent.props.forEach(key => parentKeys.push(key.key));
106106
}

src/components/left/NativeComponentPanel.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const NativeComponentPanel = (props: any) => {
7777

7878
// onClick function to invoke dispatch/reducers for adding Native Element to component
7979
const handleCreateNativeChild = (type: string) => {
80+
// if (native) type = 'RNTouchOpacity';
8081
addChild({ title: type, childType: type, HTMLInfo: {} });
8182
};
8283

@@ -88,8 +89,10 @@ const NativeComponentPanel = (props: any) => {
8889
key={`button-${component.name}`}
8990
className={classes.label}
9091
aria-label={component.name}
91-
size='medium'
92-
onClick={() => handleCreateNativeChild(`${component.name}`)}
92+
size="medium"
93+
onClick={() => {
94+
handleCreateNativeChild(`${component.name}`);
95+
}}
9396
>
9497
<span className={classes.compName}>{component.name.slice(2)}</span>
9598
{component.button}

src/containers/LeftContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class LeftContainer extends Component<LeftContPropsInt, StateInt> {
386386
<div className={classes.expansionPanel}>{componentsExpansionPanel}</div>
387387
{native ? (
388388
// React Native Components will display when in 'Native' mode
389-
<NativeComponentPanel addChild={addChild} />
389+
<NativeComponentPanel native={native} addChild={addChild} />
390390
) : (
391391
<HTMLComponentPanel
392392
className={classes.htmlCompWrapper}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ChildInt } from '../interfaces/Interfaces';
2+
export default function importNativeNameGenerator(child: ChildInt) {
3+
switch (child.componentName) {
4+
case 'RNView':
5+
return 'View';
6+
case 'RNSafeAreaView':
7+
return 'SafeAreaView';
8+
case 'RNButton':
9+
return 'Button';
10+
case 'RNFlatList':
11+
return 'FlatList';
12+
case 'RNImage':
13+
return 'Image';
14+
case 'RNModal':
15+
return 'Modal';
16+
case 'RNSwitch':
17+
return 'Switch';
18+
case 'RNText':
19+
return 'Text';
20+
case 'RNTextInput':
21+
return 'TextInput';
22+
case 'RNTouchOpacity':
23+
return 'TouchableOpacity';
24+
default:
25+
return 'div';
26+
}
27+
}

src/reducers/__tests__/leftReducers.test.ts

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as reducers from '../leftReducers';
2-
2+
import cloneDeep from '../../helperFunctions/cloneDeep';
3+
import * as interfaces from '../../interfaces/Interfaces';
4+
import * as types from '../../actionTypes/index';
35
describe('Left reducers', () => {
46
let state: any;
5-
6-
// redefine the default state before each reducer test
77
beforeEach(() => {
88
state = {
99
editMode: -1,
@@ -30,35 +30,95 @@ describe('Left reducers', () => {
3030
props: [],
3131
stateful: false,
3232
title: 'App'
33+
},
34+
{
35+
changed: false,
36+
childrenArray: [{}],
37+
classBased: false,
38+
code: '....',
39+
color: '#FFF',
40+
focusChild: {},
41+
focusChildId: 0,
42+
id: 99,
43+
nextChildId: 9,
44+
nextPropId: 9,
45+
position: {
46+
height: 850,
47+
width: 500,
48+
x: 70,
49+
y: 100
50+
},
51+
props: [],
52+
stateful: false,
53+
title: 'TEST'
3354
}
34-
]
55+
],
56+
focusComponent: 'App',
57+
history: []
3558
};
3659
});
37-
38-
describe('toggleComponentState', () => {
39-
it('inverts the statefulness of component passed in', () => {});
40-
});
41-
60+
// describe('toggleComponentState', () => {
61+
// it('inverts the statefulness of component passed in', () => {});
62+
// });
4263
// toggleEditMode reducer allows changing of component names in left container
4364
describe('toggleEditMode reducer', () => {
4465
it('should return the same state if id === 1', () => {
4566
const action = {
4667
type: 'EDIT_MODE',
4768
payload: { id: 1 }
4869
};
49-
5070
const newState = reducers.toggleEditMode(state, action.payload);
5171
expect(newState).toStrictEqual(state);
5272
});
53-
5473
it('should return new state with updated editMode', () => {
5574
const action = {
5675
type: 'EDIT_MODE',
5776
payload: { id: 2 }
5877
};
59-
6078
const newState = reducers.toggleEditMode(state, action.payload);
6179
expect(newState.editMode).toEqual(action.payload.id);
6280
});
6381
});
82+
// TEST CHANGE FOCUS COMPONENT: test component will add "look" for "test" after it's added
83+
describe('changeFocusComponent reducer', () => {
84+
it('should change the focus component title', () => {
85+
const action = {
86+
type: types.CHANGE_FOCUS_COMPONENT,
87+
payload: { title: 'TEST' }
88+
};
89+
const newState = reducers.changeFocusComponent(state, action.payload);
90+
// expecting new payload of "title" to the payload we just created
91+
expect(newState.focusComponent.title).toEqual(action.payload.title);
92+
});
93+
});
94+
// TEST IMAGE SOURCE CHANGE: image URL should be changed after local state is changed
95+
describe('changeImageSource reducer', () => {
96+
it('should change the change the image source', () => {
97+
const action = {
98+
type: types.CHANGE_IMAGE_SOURCE,
99+
payload: { imageSource: 'www.test.com/test.img' }
100+
};
101+
const newState = reducers.changeImageSource(state, action.payload);
102+
// expecting new payload of "title" to the payload we just created
103+
expect(newState.imageSource).toEqual(action.payload.imageSource);
104+
});
105+
});
106+
// TEST DELETE CHILD: test child should be deleted from local state components array
107+
// describe('deleteChild reducer', () => {
108+
// it('should delete test component', () => {
109+
// // const action = {
110+
// // type: types.CHANGE_IMAGE_SOURCE,
111+
// // payload: { imageSource: 'www.test.com/test.img' }
112+
// // };
113+
// const model = {
114+
// parentId = state.focusComponent.id,
115+
// childId = state.focusChild.childId,
116+
// calledFromDeleteComponent = false
117+
// };
118+
// const newState = reducers.deleteChild();
119+
// // expecting new payload of "title" to the payload we just created
120+
// expect(newState.imageSource).toEqual(action.payload.imageSource);
121+
// });
122+
// });
123+
// NEXT TEST
64124
});

src/reducers/initialState.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,16 @@ export const initialComponentState: ComponentInt = {
8888
code: '',
8989
changed: false
9090
};
91+
92+
export const nativeComponentTypes = [
93+
'RNView',
94+
'RNSafeAreaView',
95+
'RNButton',
96+
'RNFlatList',
97+
'RNImage',
98+
'RNModal',
99+
'RNSwitch',
100+
'RNText',
101+
'RNTextInput',
102+
'RNTouchOpacity'
103+
];

src/reducers/leftReducers.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ApplicationStateInt,
77
ChildInt
88
} from '../interfaces/Interfaces';
9-
import { initialComponentState } from './initialState';
9+
import { initialComponentState, nativeComponentTypes } from './initialState';
1010
import { createHistory } from '../helperFunctions/createHistory';
1111
import { getSize } from '../utils/htmlElements.util';
1212
import { generateNewCode } from '../helperFunctions/generateNewCode';
@@ -16,11 +16,13 @@ export const addChild = (
1616
{
1717
title,
1818
childType = '',
19-
HTMLInfo = {}
19+
HTMLInfo = {},
20+
native
2021
}: {
2122
title: string;
2223
childType: string;
2324
HTMLInfo: { [index: string]: string };
25+
native: boolean;
2426
}
2527
) => {
2628
const strippedTitle = title;
@@ -30,11 +32,12 @@ export const addChild = (
3032
window.alert('addChild Error! no type specified');
3133
}
3234

33-
//weird use of a ternary operator, could've wrapped it in one if statement
3435
const htmlElement = childType !== 'COMP' ? childType : null;
35-
if (childType !== 'COMP') {
36-
childType = 'HTML';
37-
}
36+
37+
// if childType is NOT included in the array of NATIVE React types && also not coming from left panel then the childType will revert to HTML
38+
!nativeComponentTypes.includes(childType) && childType !== 'COMP'
39+
? (childType = 'HTML')
40+
: (childType = 'NATIVE');
3841

3942
// view represents the curretn FOCUSED COMPONENT - this is the component where the child is being added to
4043
// we only add childrent (or do any action) to the focused omconent

src/utils/componentRender.util.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
PropInt
77
} from '../interfaces/Interfaces';
88
import cloneDeep from '../helperFunctions/cloneDeep';
9+
import { nativeComponentTypes } from '../reducers/initialState';
10+
import importNativeNameGenerator from '../helperFunctions/importNativeGenerator';
911

1012
const componentRender = (
1113
component: ComponentInt,
@@ -77,7 +79,7 @@ const componentRender = (
7779
.props.map((prop: PropInt) => `${prop.key}={${prop.value}}`)
7880
.join(' ');
7981
}
80-
if (child.childType === 'HTML') {
82+
if (child.childType === 'HTML' || child.childType === 'NATIVE') {
8183
const keys: string[] = Object.keys(child.HTMLInfo);
8284
return keys
8385
.map(key => {
@@ -118,6 +120,11 @@ const componentRender = (
118120
return 'p';
119121
// REACT NATIVE COMPONENTS
120122
// TO DO: UPDATE REDUCER LOGIC TO HAVE THESE COMPONENTS IN A SEPARATE FUNCTION
123+
default:
124+
return 'div';
125+
}
126+
} else if (child.childType === 'NATIVE') {
127+
switch (child.componentName) {
121128
case 'RNView':
122129
return 'View';
123130
case 'RNSafeAreaView':
@@ -145,6 +152,7 @@ const componentRender = (
145152
return child.componentName;
146153
}
147154
}
155+
148156
// logic below consists of conditional that will render depending
149157
// on the toggling of "state" and/or "class"
150158
// class components can optioally have a state which will
@@ -157,10 +165,14 @@ const componentRender = (
157165
158166
${childrenArray
159167
.filter(child => child.childType !== 'HTML')
160-
.map(
161-
child =>
162-
`import ${child.componentName} from './${child.componentName}.tsx'`
163-
)
168+
.map(child => {
169+
if (child.childType === 'NATIVE') {
170+
return `import {${importNativeNameGenerator(
171+
child
172+
)}} from 'react-native'`;
173+
} else
174+
`import ${child.componentName} from './${child.componentName}.tsx'`;
175+
})
164176
.reduce((acc: Array<string>, child) => {
165177
if (!acc.includes(child)) {
166178
acc.push(child);
@@ -169,6 +181,8 @@ const componentRender = (
169181
return acc;
170182
}, [])
171183
.join('\n')}
184+
185+
172186
173187
interface Props {
174188
${props.map(prop => `${prop.key}: ${typeSwitcher(prop.type)}\n`)}

0 commit comments

Comments
 (0)