Skip to content

Commit

Permalink
Merge pull request #1090 from assemblee-virtuelle/ControlLexiconImpor…
Browse files Browse the repository at this point in the history
…tForm

Control lexiconImportForm to avoid creating empty items
  • Loading branch information
srosset81 authored Dec 22, 2022
2 parents 848c08e + cea8a40 commit ab52648
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/frontend/packages/interop-components/dist/index.cjs.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/frontend/packages/interop-components/dist/index.es.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useCallback } from 'react';
import { required } from 'react-admin';
import { Form } from 'react-final-form';
import { Box, Toolbar, makeStyles, Button } from '@material-ui/core';
import SaveIcon from '@material-ui/icons/Save';
Expand All @@ -17,12 +18,17 @@ const useStyles = makeStyles(theme => ({

const LexiconImportForm = ({ resource, fetchLexicon, selectData, redirect, save, saving, ...rest }) => {
const classes = useStyles();

const onSubmit = useCallback(
async ({ lexicon }) => {
// If we have no URI, it means we are creating a new definition
// Delete the summary as it is "Ajouter XXX au dictionaire"
if (!lexicon.uri) delete lexicon.summary;

// If the user doesn't select any option, use the text as the label
if (typeof lexicon === 'string') {
lexicon = { label : lexicon };
}

await save(selectData(lexicon), redirect);
},
[selectData, save, redirect]
Expand All @@ -34,7 +40,7 @@ const LexiconImportForm = ({ resource, fetchLexicon, selectData, redirect, save,
render={({ handleSubmit, dirtyFields }) => (
<form onSubmit={handleSubmit}>
<Box m="1em">
<LexiconAutocompleteInput label="Titre" source="lexicon" fetchLexicon={fetchLexicon} />
<LexiconAutocompleteInput label="Titre" source="lexicon" fetchLexicon={fetchLexicon} validate={required()} />
</Box>
<Toolbar className={classes.toolbar}>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const selectOptionText = (option, optionText) => {
}
};

const capitalizeFirstLetter = string => string.charAt(0).toUpperCase() + string.slice(1);
const capitalizeFirstLetter = string => string && (string.charAt(0).toUpperCase() + string.slice(1));

const LexiconAutocompleteInput = ({
fetchLexicon,
Expand Down Expand Up @@ -88,7 +88,7 @@ const LexiconAutocompleteInput = ({
// For some reasons, this prop has to be passed
filterOptions={(options, params) => {
// Suggest the creation of a new value
if (keyword !== '') {
if (keyword) {
options.push({
label: capitalizeFirstLetter(keyword),
summary: `Ajouter "${capitalizeFirstLetter(keyword)}" au dictionnaire`,
Expand Down

0 comments on commit ab52648

Please sign in to comment.