Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cu 86byatnr8 create form button save #270

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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