refactor HelpContainer#1598
Conversation
| const links = import.meta.env.VITE_HELP_LINKS?.map((link) => ( | ||
| <div key={link.url}> | ||
| <a target="_blank" href={link.url} rel="noreferrer"> | ||
| {link.name} | ||
| </a> | ||
| </div> | ||
| )); |
There was a problem hiding this comment.
Unfortunately the HELP_LINKS were not available in the demo, hence I used the url for the key prop here, it would be best to have an id I guess.
There was a problem hiding this comment.
I think this VITE_HELP_LINKS feature is broken since env vars are strings but this one is used as an array in the code. I would consider removing this feature.
For the sake of discussion, storing JSX in a variable should also be avoided (for the same reason that it doesn't reduce the complexity of the component). I would have probably inlined the JSX and maybe stored and parsed the env var in a constant at the top of the file:
cost HELP_LINKS = import.meta.env.VITE_HELP_LINKS; // + parse as array somehow
...
// In the JSX:
{HELP_LINKS.length > 0 && (
<Card>
{HELP_LINKS..map(...)}
</Card>
)}15e5587 to
6e1bf77
Compare
|
Nice :) |
axelboc
left a comment
There was a problem hiding this comment.
Nice! FYI, I think we considered removing this page at some point, which is why I hadn't touched it yet. Worth making a decision on this before addressing my comments (especially the React Hook Form one 😁 — although it could be good practice if you're interested, since it's such a simple form)
| </Card.Body> | ||
| </Card> | ||
| ); | ||
| const handleSubmit = () => { |
There was a problem hiding this comment.
I like to use the function declaration syntax for named functions: function handleSubmit() { ... }. I find it helps to distinguish functions from variables/constants when reading the code.
There was a problem hiding this comment.
Sure, I guess this was just my personal preference but for the sake of consistency I will change it (and try to not let my inner demon do that again 😆 )
| <Row> | ||
| <Col sm={12} className="d-flex"> | ||
| <Col sm={4}> | ||
| {localContactPanel()} |
There was a problem hiding this comment.
It's best to avoid "render" functions like this. It doesn't actually remove complexity from the component — on the contrary, it adds a function.
Either inline the JSX or move it into a separate component. In this case, I think inlining is fine — long JSX is okay if the logic (conditions, loops, etc.) is limited.
| const links = import.meta.env.VITE_HELP_LINKS?.map((link) => ( | ||
| <div key={link.url}> | ||
| <a target="_blank" href={link.url} rel="noreferrer"> | ||
| {link.name} | ||
| </a> | ||
| </div> | ||
| )); |
There was a problem hiding this comment.
I think this VITE_HELP_LINKS feature is broken since env vars are strings but this one is used as an array in the code. I would consider removing this feature.
For the sake of discussion, storing JSX in a variable should also be avoided (for the same reason that it doesn't reduce the complexity of the component). I would have probably inlined the JSX and maybe stored and parsed the env var in a constant at the top of the file:
cost HELP_LINKS = import.meta.env.VITE_HELP_LINKS; // + parse as array somehow
...
// In the JSX:
{HELP_LINKS.length > 0 && (
<Card>
{HELP_LINKS..map(...)}
</Card>
)}| </Col> | ||
| <Col className="col-xs-4"> | ||
| <span> | ||
| <b>Mesh </b> <br /> |
There was a problem hiding this comment.
Could you please replace the b and br tags with h3 and remove the span wrappers? Might as well improve the HTML a bit... 😂
There was a problem hiding this comment.
Well spotted! Maybe I went a little to fast over this component
| const senderRef = React.useRef(); | ||
| const contentRef = React.useRef(); |
There was a problem hiding this comment.
Perhaps a good opportunity to try React Hook Form? It's already installed — I used it for the login form and for the manual sample creation form, if I recall.
Oh, maybe I missed/forgot this consideration 😅 I guess we can keep it as a draft then, I prefer to use it as a little practice if possible;) |
6e1bf77 to
60e34ce
Compare
walesch-yan
left a comment
There was a problem hiding this comment.
I added the suggestions and integrated the react-hook-form for this component:)
| <Alert | ||
| className="mt-3" | ||
| variant={feedback.variant} | ||
| onClose={() => setFeedback({})} | ||
| dismissible | ||
| > | ||
| {feedback.message} | ||
| </Alert> |
There was a problem hiding this comment.
Added an alert as feedback for the user if the form was submitted correctly or if it failed
| setFeedback({ | ||
| variant: 'success', | ||
| message: 'Feedback sent successfully', | ||
| }); |
There was a problem hiding this comment.
The form state has an isSubmitSuccessful flag that tells you if the submit was successful (i.e. no error thrown in handleSubmit). And if sendFeedback throws an error, you can catch it and call the form's setError function as explained here: https://react-hook-form.com/docs/useform/seterror
This means that you don't need the feedback state and can just declare different alerts in the JSX based on isSubmitSuccessful and errors.
| variant: 'success', | ||
| message: 'Feedback sent successfully', | ||
| }); | ||
| reset(DEFAULT_HELPFORM_VALUES); |
There was a problem hiding this comment.
This may reset isSubmitSuccessful and errors. I think there's an option that controls this: https://react-hook-form.com/docs/useform#resetOptions
60e34ce to
2f1f635
Compare
| useEffect(() => { | ||
| if (isSubmitSuccessful) { | ||
| reset(); | ||
| setShowSuccess(true); | ||
| } | ||
| }, [isSubmitSuccessful, reset]); |
There was a problem hiding this comment.
According to the docs the recommended way of using reset is inside of useEffect using isSubmitSuccessful as trigger, I wrapped it inside the if, since reset will change the value of isSubmitSuccessful to false.
As one can see I also kept the useState here since there is no option to keep isSubmitSuccessful between resets but only isSubmitted (which becomes also true if the execution of submit fails). One could use resetField but it would go against the idea of sending multiple forms. Hence I decided to keep a local state variable.
There was a problem hiding this comment.
Makes sense, in that case, I would move setShowSuccess(false) (currently in handleSubmit) into a separate use effect as well to hide the success message when the user starts filling in the form again:
useEffect(() => {
if (isDirty) {
// Hide success message if user starts filling in the form again
setShowSuccess(false);
}
}, [isDirty]);But it's probably overly complicating things... Forms suck.
There was a problem hiding this comment.
Well in terms of UX your approach is probably better, thanks for all the suggestions!
Forms suck.
Couldn't agree more 😅
2f02f43 to
88a95a0
Compare
88a95a0 to
2802960
Compare
|
Hm, this one has been here for some time now. What would you like to do with it ? |
This PR refactors the
HelpContainerinto a functional component following similar approaches as for previous PRs.