Skip to content

Commit

Permalink
Merge pull request #270 from jembi/CU-86byatnr8_Create-Form-Button-Save
Browse files Browse the repository at this point in the history
Cu 86byatnr8 create form button save
  • Loading branch information
MatthewErispe authored Jul 9, 2024
2 parents 7abf54e + 6d5e3e8 commit 76eef67
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
35 changes: 34 additions & 1 deletion JeMPI_Apps/JeMPI_UI/src/pages/settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { Grid, Tab, Tabs, Typography } from '@mui/material';
import { Grid, Tab, Tabs, Typography, Button } from '@mui/material';
import { Box } from '@mui/system';
import { SyntheticEvent } from 'react';
import CommonSettings from './common/Common';
Expand All @@ -12,18 +12,34 @@ import InteractiveNode from './interactiveNode/InteractiveNode';
import { CustomTabPanel, a11yProps } from './deterministic/BasicTabs';
import { Configuration } from 'types/Configuration';
import { generateId } from 'utils/helpers';
import { useConfig } from 'hooks/useConfig';

const Settings = () => {
const [value, setValue] = useState(0);
const [configurationData, setConfigurationData] = useState(() => {
const storedData = localStorage.getItem('configuration');
return storedData ? generateId(JSON.parse(storedData)) : ({} as Configuration);
});
const [isSaving, setIsSaving] = useState<boolean>(false);

const { apiClient } = useConfig();

const handleChange = (event: SyntheticEvent, newValue: number) => {
setValue(newValue);
};

const handleSave = async () => {
setIsSaving(true);
const response = await apiClient.saveConfiguration();
setIsSaving(false);
if (response && response.response === 'ok') {
console.log('handleSave result', response.data);
}
if (response && response.response === 'error') {
console.log('handleSave error', response.data);
}
};

useEffect(() => {
const handleStorageChange = (event: StorageEvent) => {
if (event.key === 'configuration') {
Expand Down Expand Up @@ -128,6 +144,23 @@ const Settings = () => {
Probabilistic
</Typography>
</CustomTabPanel>
<Box sx={{ display: 'flex', justifyContent: 'space-between', my: 2 }}>
<Box sx={{ display: 'flex', gap: 1 }}>
{/* <Button variant="outlined" color="secondary">Edit</Button>
<Button variant="outlined" color="secondary">Clear</Button>
<Button variant="outlined" color="secondary">Set to Reference</Button> */}
</Box>
<Box>
<Button
variant="contained"
color="primary"
onClick={handleSave}
disabled={isSaving}
>
{isSaving ? 'Saving...' : 'Save'}
</Button>
</Box>
</Box>
</Box>
</Grid>
</Grid>
Expand Down
24 changes: 24 additions & 0 deletions JeMPI_Apps/JeMPI_UI/src/services/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ export class ApiClient {
return data
}

async saveConfiguration() {
const configuration = localStorage.getItem('configuration');
let payload = configuration;
if (!payload) return; // Avoid overwriting the configuration with nothing
if (typeof payload == 'string') payload = JSON.parse(payload); // Avoid overwriting the configuration with garbage

try {
const { data } = await this.client.post<Configuration>(
ROUTES.POST_CONFIGURATION,
payload
)
return {
response: "ok",
data
}
} catch (error) {
console.error('Error saving configuration:', error)
return {
response: "error",
error
}
}
}

async fetchMatches(
limit: number,
offset: number,
Expand Down

0 comments on commit 76eef67

Please sign in to comment.