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

Add proverb form component #24

Merged
merged 3 commits into from Oct 26, 2020
Merged

Add proverb form component #24

merged 3 commits into from Oct 26, 2020

Conversation

salih18
Copy link
Contributor

@salih18 salih18 commented Oct 23, 2020

Created Proverb Form component
Created Modal reusable component to show proverb add page inside it.
Add some styling

Copy link

@sarah-vanderlaan sarah-vanderlaan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!!! Added a couple of comments to make the code a bit more readable in some areas

<ModalBootstrap.Header
className="border-bottom-0"
closeButton
></ModalBootstrap.Header>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
></ModalBootstrap.Header>
/>

since this component has no children, you can close it within the same <>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely.

const [modal, setModal] = useState({
isOpen: false,
type: "",
id: "",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the id field here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. For now we dont need it.


//Modal Handlers

const handleShow = (type) => async (e) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const handleShow = (type) => async (e) => {
const handleShowModal = (type) => async (e) => {

Let's make this function name even more specific so it is very clear what it is for

<Section
<Modal
isOpen={modal.isOpen}
modalClose={() => setModal({ isOpen: false })}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
modalClose={() => setModal({ isOpen: false })}
modalClose={() => setModal({ isOpen: false, type: undefined })}

I think we should reset the state here as well, so that when we add other modal types (for example Edit), then we wont accidentally open the add one

Boolean(data)
);
const result = isArabic(proverb);
isFilled && result ? setDisabled(false) : setDisabled(true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets extract this into 2 lines so it is more readable:

const shouldBeDisabled = !isFilled || !result;
setDisabled(shouldBeDisabled);

Copy link
Contributor Author

@salih18 salih18 Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. Thank you

isFilled && result ? setDisabled(false) : setDisabled(true);
}, [formData]);

const onChange = (e) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const onChange = (e) => {
const handleInputChange = (e) => {

Lets make this function name more explicit as well, to make it even more readable :)

type="text"
value={proverb}
name="proverb"
onChange={(e) => onChange(e)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onChange={(e) => onChange(e)}
onChange={handleInputChange}

type="text"
value={translation}
name="translation"
onChange={(e) => onChange(e)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onChange={(e) => onChange(e)}
onChange={handleInputChange}

type="text"
value={explanation}
name="explanation"
onChange={(e) => onChange(e)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onChange={(e) => onChange(e)}
onChange={handleInputChange}

<Button
variant="info"
text="Add"
onClick={(e) => onSubmit(e)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onClick={(e) => onSubmit(e)}
onClick={onSubmit}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes my fault. We dont need to create a new function.

Copy link

@frankPairs frankPairs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great Salih! I only added a couple of small comments.

@@ -0,0 +1,5 @@
export const isArabic = (text) => {
const pattern = /[\u0600-\u06FF]/;
const result = pattern.test(text);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const result = pattern.test(text);
return pattern.test(text);

this can be simplified as we are not using the result value.

<Section
<Modal
isOpen={modal.isOpen}
modalClose={() => setModal({ isOpen: false, type: undefined })}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
modalClose={() => setModal({ isOpen: false, type: undefined })}
modalClose={handleCloseModal}

you could create a function called handleCloseModal and use it as I wrote above. this could be an implementation:

function handleCloseModal() {
   setModal({ isOpen: false, type: undefined })
}

It's good to have named functions, they are easier to read and to debug than anonymous functions. I would advise
using arrow functions when they really improve the readability of the code or when you want to avoid context issues related to using the "this" object.

You can find a lot of interesting information about arrow functions here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Copy link

@sarah-vanderlaan sarah-vanderlaan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@salih18 salih18 merged commit 3330dcc into development Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants