|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import FormControl from '@mui/material/FormControl'; |
3 | 3 | import MenuItem from '@mui/material/MenuItem'; |
4 | 4 | import Select from '@mui/material/Select'; |
5 | 5 | import { InputLabel } from '@mui/material'; |
6 | | -import {useDispatch, useSelector} from 'react-redux'; |
| 6 | +import { useDispatch, useSelector } from 'react-redux'; |
7 | 7 | import { updateAttributes } from '../../redux/reducers/slice/appStateSlice'; |
8 | 8 | import { RootState } from '../../redux/store'; |
9 | 9 |
|
10 | 10 | function AddLink({ id, onClickHandler, linkDisplayed }) { |
11 | | - |
12 | | - const { state, contextParam, isThemeLight } = useSelector((store:RootState) => ({ |
13 | | - state: store.appState, |
14 | | - contextParam: store.contextSlice, |
15 | | - isThemeLight: store.styleSlice |
16 | | - })); |
| 11 | + const { state, contextParam, isThemeLight } = useSelector( |
| 12 | + (store: RootState) => ({ |
| 13 | + state: store.appState, |
| 14 | + contextParam: store.contextSlice, |
| 15 | + isThemeLight: store.styleSlice |
| 16 | + }) |
| 17 | + ); |
17 | 18 | const dispatch = useDispatch(); |
18 | 19 | //this function allows the link to be functional when it's nested |
19 | 20 | function deepIterate(arr) { |
20 | 21 | const output = []; |
21 | | - for(let i = 0; i < arr.length; i++) { |
22 | | - if(arr[i].typeId === 1000) continue; |
| 22 | + for (let i = 0; i < arr.length; i++) { |
| 23 | + if (arr[i].typeId === 1000) continue; |
23 | 24 | output.push(arr[i]); |
24 | | - if(arr[i].children.length) { |
| 25 | + if (arr[i].children.length) { |
25 | 26 | output.push(...deepIterate(arr[i].children)); |
26 | 27 | } |
27 | 28 | } |
28 | 29 | return output; |
29 | 30 | } |
30 | 31 |
|
31 | | - const handlePageSelect = event => { |
32 | | - const currComponent = state.components.find(element => element.id === state.canvasFocus.componentId); |
33 | | - deepIterate(currComponent.children).some(element => { |
34 | | - if(element.childId === id) { |
| 32 | + const handlePageSelect = (event) => { |
| 33 | + const currComponent = state.components.find( |
| 34 | + (element) => element.id === state.canvasFocus.componentId |
| 35 | + ); |
| 36 | + deepIterate(currComponent.children).some((element) => { |
| 37 | + if (element.childId === id) { |
35 | 38 | const state = JSON.parse(JSON.stringify(element)); |
36 | 39 | state.childId = id; |
37 | 40 | state.attributes.compLink = event.target.value; |
38 | | - dispatch(updateAttributes({attributes: state, contextParam: contextParam})) |
| 41 | + dispatch( |
| 42 | + updateAttributes({ attributes: state, contextParam: contextParam }) |
| 43 | + ); |
39 | 44 | return true; |
40 | 45 | } |
41 | 46 | }); |
42 | | - } |
| 47 | + }; |
43 | 48 |
|
44 | | - const pagesItems = state.components.filter(comp => state.rootComponents.includes(comp.id)); |
45 | | - const dropDown = [<MenuItem style={{ color: '#000' }} disabled hidden selected>Pages</MenuItem>].concat(pagesItems.map(comp => <MenuItem style={{ color: '#000' }} value={comp.name}>{comp.name}</MenuItem>)); |
| 49 | + const pagesItems = state.components.filter((comp) => |
| 50 | + state.rootComponents.includes(comp.id) |
| 51 | + ); |
| 52 | + const dropDown = [ |
| 53 | + <MenuItem style={{ color: '#000' }} disabled hidden selected> |
| 54 | + Pages |
| 55 | + </MenuItem> |
| 56 | + ].concat( |
| 57 | + pagesItems.map((comp) => ( |
| 58 | + <MenuItem style={{ color: '#000' }} value={comp.name}> |
| 59 | + {comp.name} |
| 60 | + </MenuItem> |
| 61 | + )) |
| 62 | + ); |
46 | 63 |
|
47 | 64 | return ( |
48 | | - <div style={{float: 'right'}}> |
49 | | - <FormControl variant='outlined' focused={true} style={ {width: '100%'} }> |
50 | | - <InputLabel id='page-select-label' style={ {color: isThemeLight? '#000' : '#fff'} }>Pages</InputLabel> |
51 | | - <Select |
52 | | - label='Pages' |
53 | | - onMouseDown={onClickHandler} |
54 | | - onChange={handlePageSelect} |
55 | | - id="page-select" |
56 | | - value={linkDisplayed} |
57 | | - style={ isThemeLight |
58 | | - ? {backgroundColor: '#eef0f1', color: '#000', border: '1px solid black', height: '28px', width: '200px'} |
59 | | - : {backgroundColor: 'gray', color: '#fff', border: '1px solid white', height: '28px', width: '200px'}} |
60 | | - > |
61 | | - {dropDown} |
62 | | - </Select> |
| 65 | + <div style={{ float: 'right' }}> |
| 66 | + <FormControl variant="outlined" focused={true} style={{ width: '100%' }}> |
| 67 | + <InputLabel |
| 68 | + id="page-select-label" |
| 69 | + style={{ color: isThemeLight ? '#000' : '#fff' }} |
| 70 | + > |
| 71 | + Pages |
| 72 | + </InputLabel> |
| 73 | + <Select |
| 74 | + label="Pages" |
| 75 | + onMouseDown={onClickHandler} |
| 76 | + onChange={handlePageSelect} |
| 77 | + id="page-select" |
| 78 | + value={linkDisplayed} |
| 79 | + style={ |
| 80 | + isThemeLight |
| 81 | + ? { |
| 82 | + backgroundColor: '#eef0f1', |
| 83 | + color: '#000', |
| 84 | + border: '1px solid black', |
| 85 | + height: '28px', |
| 86 | + width: '200px' |
| 87 | + } |
| 88 | + : { |
| 89 | + backgroundColor: 'gray', |
| 90 | + color: '#fff', |
| 91 | + border: '1px solid white', |
| 92 | + height: '28px', |
| 93 | + width: '200px' |
| 94 | + } |
| 95 | + } |
| 96 | + > |
| 97 | + {dropDown} |
| 98 | + </Select> |
63 | 99 | </FormControl> |
64 | 100 | </div> |
65 | 101 | ); |
|
0 commit comments