|
| 1 | +import { NavigationPage } from '@/components/navigation/NavigationPage'; |
| 2 | +import { getParams, setParams as setParamsGlobal } from '@/library/powersync/ConnectionManager'; |
| 3 | +import { Box, Button, Grid, IconButton, styled, TextField } from '@mui/material'; |
| 4 | +import { FormEvent, useState } from 'react'; |
| 5 | +import DeleteIcon from '@mui/icons-material/Delete'; |
| 6 | +import AddIcon from '@mui/icons-material/Add'; |
| 7 | + |
| 8 | +const jsonToObjectArray = (json: Object) => { |
| 9 | + const entrySet = Object.entries(json); |
| 10 | + return entrySet.map(([key, value]) => ({ |
| 11 | + key, |
| 12 | + value |
| 13 | + })); |
| 14 | +}; |
| 15 | + |
| 16 | +function ClientParamsPage() { |
| 17 | + const [params, setParams] = useState(jsonToObjectArray(getParams())); |
| 18 | + |
| 19 | + const onSubmit = (e: FormEvent<HTMLFormElement>) => { |
| 20 | + e.preventDefault(); |
| 21 | + e.stopPropagation(); |
| 22 | + |
| 23 | + const newParams = params.reduce((curr, item) => ({ ...curr, [`${item.key}`]: item.value }), {}); |
| 24 | + setParamsGlobal(newParams); |
| 25 | + }; |
| 26 | + |
| 27 | + const replace = (idx: number, val: any) => setParams((a) => a.map((entity, i) => (i === idx ? val : entity))); |
| 28 | + |
| 29 | + const removeIdx = (idx: number) => |
| 30 | + setParams((a) => a.map((entity, i) => i !== idx && entity).filter((entity) => entity !== false)); |
| 31 | + |
| 32 | + const addRow = () => { |
| 33 | + setParams((a) => [...a, { key: '', value: '' }]); |
| 34 | + }; |
| 35 | + |
| 36 | + const changeValue = (idx: number, value: string, currKey: string) => { |
| 37 | + replace(idx, { key: currKey, value }); |
| 38 | + }; |
| 39 | + |
| 40 | + const changeKey = (idx: number, key: string, currValue: unknown) => { |
| 41 | + replace(idx, { key, value: currValue }); |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <NavigationPage title="Client Parameters"> |
| 46 | + <S.MainContainer> |
| 47 | + <form onSubmit={onSubmit}> |
| 48 | + {params.map(({ key, value }, idx) => ( |
| 49 | + <S.CenteredGrid container> |
| 50 | + <S.CenteredGrid item xs={12} md={10}> |
| 51 | + <TextField |
| 52 | + label="Key" |
| 53 | + value={key} |
| 54 | + sx={{ margin: '10px' }} |
| 55 | + onChange={(v) => changeKey(idx, v.target.value, value)} |
| 56 | + /> |
| 57 | + <TextField |
| 58 | + label="Value" |
| 59 | + value={value} |
| 60 | + sx={{ margin: '10px' }} |
| 61 | + onChange={(v) => changeValue(idx, v.target.value, key)} |
| 62 | + /> |
| 63 | + |
| 64 | + <IconButton sx={{ margin: '10px' }} color="error" onClick={() => removeIdx(idx)}> |
| 65 | + <DeleteIcon /> |
| 66 | + </IconButton> |
| 67 | + </S.CenteredGrid> |
| 68 | + </S.CenteredGrid> |
| 69 | + ))} |
| 70 | + <S.CenteredGrid container> |
| 71 | + <IconButton sx={{ margin: '10px' }} onClick={addRow}> |
| 72 | + <AddIcon /> |
| 73 | + </IconButton> |
| 74 | + </S.CenteredGrid> |
| 75 | + <Button type="submit" sx={{ margin: '10px' }} variant="contained"> |
| 76 | + Submit |
| 77 | + </Button> |
| 78 | + </form> |
| 79 | + </S.MainContainer> |
| 80 | + </NavigationPage> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +namespace S { |
| 85 | + export const MainContainer = styled(Box)` |
| 86 | + padding: 20px; |
| 87 | + `; |
| 88 | + |
| 89 | + export const CenteredGrid = styled(Grid)` |
| 90 | + display: flex; |
| 91 | + justify-content: center; |
| 92 | + align-items: center; |
| 93 | + `; |
| 94 | +} |
| 95 | + |
| 96 | +export default ClientParamsPage; |
0 commit comments