|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + FlexBox, |
| 4 | + Box, |
| 5 | + EditField, |
| 6 | + Paragraph, |
| 7 | + Container, |
| 8 | + FullWidthSpinner, |
| 9 | +} from '../../components'; |
| 10 | +import styles from './index.module.scss'; |
| 11 | +import { useService } from './useService'; |
| 12 | + |
| 13 | +export const NonEditableConfig: React.FC<{ details: any }> = ({ details }) => { |
| 14 | + const { flavor } = useService({ |
| 15 | + details, |
| 16 | + }); |
| 17 | + console.log( |
| 18 | + details?.configuration, |
| 19 | + 'flavoflavorr', |
| 20 | + flavor?.config_schema?.properties, |
| 21 | + ); |
| 22 | + |
| 23 | + const titleCase = (s: any) => |
| 24 | + s.replace(/^_*(.)|_+(.)/g, (s: any, c: string, d: string) => |
| 25 | + c ? c.toUpperCase() : ' ' + d.toUpperCase(), |
| 26 | + ); |
| 27 | + |
| 28 | + const getFormElement: any = (elementName: any, elementSchema: any) => { |
| 29 | + if (typeof elementSchema === 'string') { |
| 30 | + return ( |
| 31 | + <Box marginVertical={'md'} style={{ width: '100%' }}> |
| 32 | + <EditField |
| 33 | + disabled |
| 34 | + onChangeText={() => console.log('')} |
| 35 | + label={titleCase(elementName)} |
| 36 | + optional={false} |
| 37 | + value={elementSchema} |
| 38 | + placeholder="" |
| 39 | + hasError={false} |
| 40 | + className={styles.field} |
| 41 | + /> |
| 42 | + </Box> |
| 43 | + ); |
| 44 | + } |
| 45 | + if (typeof elementSchema === 'object') { |
| 46 | + return ( |
| 47 | + <Box marginVertical={'xl'} style={{ width: '100%' }}> |
| 48 | + <Paragraph size="body" style={{ color: 'black' }}> |
| 49 | + <label htmlFor={elementName}>{titleCase(elementName)}</label> |
| 50 | + </Paragraph> |
| 51 | + {Object.keys(elementSchema).length < 1 && ( |
| 52 | + <FlexBox.Row> |
| 53 | + <EditField |
| 54 | + disabled |
| 55 | + onChangeText={() => console.log('')} |
| 56 | + label="Key" |
| 57 | + optional={false} |
| 58 | + value={''} |
| 59 | + placeholder="" |
| 60 | + hasError={false} |
| 61 | + className={styles.field} |
| 62 | + /> |
| 63 | + <div style={{ width: '10%' }}></div> |
| 64 | + <EditField |
| 65 | + disabled |
| 66 | + // marginRight={'md'} |
| 67 | + onChangeText={() => console.log('')} |
| 68 | + label="Value" |
| 69 | + // optional={true} |
| 70 | + value={''} |
| 71 | + placeholder="" |
| 72 | + hasError={false} |
| 73 | + className={styles.field} |
| 74 | + /> |
| 75 | + </FlexBox.Row> |
| 76 | + )} |
| 77 | + {Object.entries(elementSchema).map(([key, value]) => ( |
| 78 | + <FlexBox.Row> |
| 79 | + <EditField |
| 80 | + disabled |
| 81 | + onChangeText={() => console.log('')} |
| 82 | + label="Key" |
| 83 | + optional={false} |
| 84 | + value={key} |
| 85 | + placeholder="" |
| 86 | + hasError={false} |
| 87 | + className={styles.field} |
| 88 | + /> |
| 89 | + <div style={{ width: '10%' }}></div> |
| 90 | + <EditField |
| 91 | + disabled |
| 92 | + // marginRight={'md'} |
| 93 | + onChangeText={() => console.log('')} |
| 94 | + label="Value" |
| 95 | + // optional={true} |
| 96 | + value={value} |
| 97 | + placeholder="" |
| 98 | + hasError={false} |
| 99 | + className={styles.field} |
| 100 | + /> |
| 101 | + </FlexBox.Row> |
| 102 | + ))} |
| 103 | + </Box> |
| 104 | + ); |
| 105 | + } |
| 106 | + if (typeof elementSchema === 'boolean') { |
| 107 | + return ( |
| 108 | + <Box marginVertical={'md'} style={{ width: '100%' }}> |
| 109 | + <Box> |
| 110 | + <FlexBox.Row justifyContent="space-between"> |
| 111 | + <Paragraph>{titleCase(elementName)}</Paragraph> |
| 112 | + <label className={styles.switch}> |
| 113 | + <input type="checkbox" checked={elementSchema} /> |
| 114 | + <span className={`${styles.slider} ${styles.round}`}></span> |
| 115 | + </label> |
| 116 | + </FlexBox.Row> |
| 117 | + </Box> |
| 118 | + </Box> |
| 119 | + ); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + if (flavor === undefined) { |
| 124 | + return <FullWidthSpinner color="black" size="md" />; |
| 125 | + } |
| 126 | + // const values = [...flavor?.config_schema?.properties]; |
| 127 | + |
| 128 | + let result = Object.keys(flavor?.config_schema?.properties).reduce(function ( |
| 129 | + r: any, |
| 130 | + name: any, |
| 131 | + ) { |
| 132 | + return ( |
| 133 | + (r[name] = |
| 134 | + flavor?.config_schema?.properties[name].type === 'string' && |
| 135 | + flavor?.config_schema?.properties[name].default === undefined |
| 136 | + ? '' |
| 137 | + : flavor?.config_schema?.properties[name].default), |
| 138 | + r |
| 139 | + ); |
| 140 | + }, |
| 141 | + {}); |
| 142 | + |
| 143 | + const mappedObject = { |
| 144 | + ...result, |
| 145 | + ...details?.configuration, |
| 146 | + }; |
| 147 | + |
| 148 | + return ( |
| 149 | + <FlexBox.Column marginTop="xl" fullWidth> |
| 150 | + <FlexBox.Row> |
| 151 | + <Container> |
| 152 | + <Box style={{ width: '80%' }}> |
| 153 | + <EditField |
| 154 | + disabled |
| 155 | + onChangeText={() => console.log('')} |
| 156 | + label={'Flavor Name'} |
| 157 | + optional={false} |
| 158 | + value={details.flavor} |
| 159 | + placeholder="" |
| 160 | + hasError={false} |
| 161 | + className={styles.field} |
| 162 | + /> |
| 163 | + </Box> |
| 164 | + <FlexBox.Row justifyContent="space-between" style={{ width: '80%' }}> |
| 165 | + <Paragraph>Share Component with public</Paragraph> |
| 166 | + <label className={styles.switch}> |
| 167 | + <input type="checkbox" checked={details.is_shared} /> |
| 168 | + <span className={`${styles.slider} ${styles.round}`}></span> |
| 169 | + </label> |
| 170 | + </FlexBox.Row> |
| 171 | + </Container> |
| 172 | + {/* <Container> |
| 173 | + |
| 174 | + </Container> */} |
| 175 | + </FlexBox.Row> |
| 176 | + <FlexBox.Row style={{ width: '80%' }}> |
| 177 | + <Container> |
| 178 | + {/* <Row> |
| 179 | + <Col xs={5}> |
| 180 | + |
| 181 | + </Col> |
| 182 | + <Col xs={5} style={{ marginLeft: '100px' }}> |
| 183 | + <FlexBox.Row justifyContent="space-between"> |
| 184 | + <Paragraph>Share Component with public</Paragraph> |
| 185 | + <label className={styles.switch}> |
| 186 | + <input type="checkbox" checked={details.isShared} /> |
| 187 | + <span className={`${styles.slider} ${styles.round}`}></span> |
| 188 | + </label> |
| 189 | + </FlexBox.Row> |
| 190 | + </Col> |
| 191 | + </Row> */} |
| 192 | + |
| 193 | + {Object.keys(mappedObject).map((key, ind) => ( |
| 194 | + // <Col xs={6} key={ind}> |
| 195 | + <>{getFormElement(key, mappedObject[key])}</> |
| 196 | + // </Col> |
| 197 | + ))} |
| 198 | + </Container> |
| 199 | + </FlexBox.Row> |
| 200 | + </FlexBox.Column> |
| 201 | + ); |
| 202 | +}; |
0 commit comments