Skip to content

Commit

Permalink
Upload button error handling added
Browse files Browse the repository at this point in the history
  • Loading branch information
krdmnbrk committed Dec 12, 2024
1 parent d0533f1 commit a14c51e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GENERATE_SOURCEMAP=false
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "atomicgen.io",
"version": "0.1.0",
"version": "1.0.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.13.3",
Expand All @@ -19,8 +19,7 @@
"react-ace": "^13.0.0",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
"yaml": "^2.6.1"
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
Expand Down
4 changes: 4 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const validateInputs = (data, rules) => {
function App() {
const [errors, setErrors] = useState([]);
const [validationErrors, setValidationErrors] = useState([]);
const [inputButtonErrors, setInputButtonErrors] = useState([]);
const [updated, setUpdated] = useState(false);
const [inputs, setInputs] = useState(base);
const [darkMode, setDarkMode] = useState(true);
Expand Down Expand Up @@ -177,6 +178,7 @@ function App() {

// Validate inputs
useEffect(() => {
setInputButtonErrors([]);
if (updated) {

const errors = validateInputs(inputs, validationRules);
Expand Down Expand Up @@ -239,6 +241,8 @@ function App() {
setInputs={setInputs}
errors={errors}
setErrors={setErrors}
inputButtonErrors={inputButtonErrors}
setInputButtonErrors={setInputButtonErrors}
/>
</Grid>
<Grid size={isPortrait ? 12 : 6}>
Expand Down
5 changes: 4 additions & 1 deletion src/components/Inputs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const MenuProps = {
};

// Functional component for managing inputs
function Inputs({ base, darkMode, changed, setChanged, errors, setErrors, inputs, setInputs, executor_names, supported_platforms }) {
function Inputs({ inputButtonErrors, setInputButtonErrors, base, darkMode, changed, setChanged, errors, setErrors, inputs, setInputs, executor_names, supported_platforms }) {

// Handle changes to text fields (e.g., Atomic Name, Atomic Description)
const handleChangeText = (e) => {
Expand Down Expand Up @@ -103,11 +103,14 @@ function Inputs({ base, darkMode, changed, setChanged, errors, setErrors, inputs
<Box>
<Box sx={{ mb: 2 }}>
<InputButtons
inputButtonErrors={inputButtonErrors}
setInputButtonErrors={setInputButtonErrors}
base={base}
setInputs={setInputs}
setChanged={setChanged}
changed={changed}
darkMode={darkMode}

/>
</Box>
{/* Input for Atomic Name */}
Expand Down
15 changes: 8 additions & 7 deletions src/components/Inputs/InputButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import transformInputArguments from './transformInputArguments';



export default function InputButtons({ base, darkMode, setInputs, setChanged, changed }) {
export default function InputButtons({ inputButtonErrors, setInputButtonErrors, base, darkMode, setInputs, setChanged, changed }) {
const [open, setOpen] = React.useState(false);
const [errorMessage, setErrorMessage] = useState(null);
const [samples, setSamples] = useState([]);
const anchorRef = React.useRef(null);

Expand Down Expand Up @@ -54,7 +53,7 @@ export default function InputButtons({ base, darkMode, setInputs, setChanged, ch
};




const loadSample = async (level) => {
if (changed) {
Expand Down Expand Up @@ -87,15 +86,17 @@ export default function InputButtons({ base, darkMode, setInputs, setChanged, ch
base={base}
setInputs={setInputs}
darkMode={darkMode}
setErrorMessage={setErrorMessage}
errorMessage={errorMessage}
setInputButtonErrors={setInputButtonErrors}
inputButtonErrors={inputButtonErrors}
setChanged={setChanged}
changed={changed}
/>
</ButtonGroup>
{errorMessage &&
{inputButtonErrors.length > 0 &&
<Alert sx={{ mt: 1 }} variant={darkMode ? "outlined" : "filled"} severity='error'>
{errorMessage}
{inputButtonErrors.map((error, index) => (
<li key={index}>{error}</li>
))}
</Alert>
}
<Popper
Expand Down
8 changes: 4 additions & 4 deletions src/components/Inputs/UploadButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const VisuallyHiddenInput = styled('input')({
});


export default function UploadButton({ setChanged, changed, base, darkMode, setInputs, setErrorMessage }) {
export default function UploadButton({ inputButtonErrors, setInputButtonErrors, setChanged, changed, base, darkMode, setInputs }) {
const [open, setOpen] = React.useState(false);
const [atomicNames, setAtomicNames] = React.useState([]);
const [techniqueName, setTechniqueName] = React.useState(null);
const [techniqueId, setTechniqueId] = React.useState(null);
const [fileContent, setFileContent] = React.useState(null);

const handleFileUpload = async (event) => {
setInputButtonErrors([]);
if (changed) {
const confirm = window.confirm('Are you sure you want to load another test? Your current inputs will be overwritten.');
if (!confirm) return;
Expand All @@ -34,7 +35,7 @@ export default function UploadButton({ setChanged, changed, base, darkMode, setI
if (file) {
const fileExtension = file.name.split('.').pop().toLowerCase();
if (fileExtension !== "yaml" && fileExtension !== "yml") {
setErrorMessage("Only yaml files can be uploaded.");
setInputButtonErrors([...inputButtonErrors, "Only yaml files can be uploaded."]);
console.error("Only yaml files can be uploaded.");
return;
}
Expand All @@ -54,10 +55,9 @@ export default function UploadButton({ setChanged, changed, base, darkMode, setI

} catch (error) {
console.error("Error while file processing the yaml file, check format.", error);
setErrorMessage("Error while file processing the yaml file, check format.");
setInputButtonErrors([...inputButtonErrors, "Error while file processing the yaml file, check format."])
} finally {
event.target.value = null;
setChanged(false); // // // // //
}
}
};
Expand Down

0 comments on commit a14c51e

Please sign in to comment.