-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix field schema form sent_message and added customizable FormRe…
…sult component
- Loading branch information
1 parent
b4139d2
commit 6dd725b
Showing
4 changed files
with
75 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import { useIntl, defineMessages } from 'react-intl'; | ||
import { Message, Button } from 'semantic-ui-react'; | ||
|
||
const messages = defineMessages({ | ||
success: { | ||
id: 'form_submit_success', | ||
defaultMessage: 'Sent!', | ||
}, | ||
reset: { | ||
id: 'form_reset', | ||
defaultMessage: 'Clear', | ||
}, | ||
}); | ||
|
||
/* Function that replaces variables from the user customized message */ | ||
const replaceMessage = (text, sent_data) => { | ||
let i = 0; | ||
while (i < sent_data.length) { | ||
let idField = sent_data[i].label; | ||
text = text.replaceAll('${' + idField + '}', sent_data[i].value ?? ''); | ||
i++; | ||
} | ||
text = text.replaceAll('\n', '<br/>'); | ||
return text; | ||
}; | ||
|
||
const FormResult = ({ formState, data, resetFormState }) => { | ||
const intl = useIntl(); | ||
return ( | ||
<Message positive role="alert"> | ||
{/* Custom message */} | ||
{data.send_message ? ( | ||
<p | ||
dangerouslySetInnerHTML={{ | ||
__html: replaceMessage(data.send_message, formState.result.data), | ||
}} | ||
/> | ||
) : ( | ||
<> | ||
{/* Default message */} | ||
<Message.Header as="h4"> | ||
{intl.formatMessage(messages.success)} | ||
</Message.Header> | ||
<p>{formState.result}</p> | ||
</> | ||
)} | ||
{/* Back button */} | ||
<Button | ||
secondary | ||
type="button" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
resetFormState(); | ||
}} | ||
> | ||
{intl.formatMessage(messages.reset)} | ||
</Button> | ||
</Message> | ||
); | ||
}; | ||
export default FormResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters