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

Wokers list #7

Merged
merged 8 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions client/src/App.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import React from "react";
import React, { useState } from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import "./App.css";
import { CreateWorker } from "./pages";
import { CreateWorker, ShowWorkers } from "./pages";

export function App() {
const [kick, setKick] = useState(false);
const workersHandle = () => setKick(!kick);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we name this a bit more descriptive, maybe triggerFetch or triggerRefetch?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to "trigger".

return (
<Router>
<main role="main">
<Link to="/">Home</Link>
<br />
<Link to="add-worker">Create cleaner</Link>
<br />
<Link to="workers" onClick={workersHandle}>Show cleaners</Link>
</main>
<Switch>
<Route path="/add-worker">
<CreateWorker />
</Route>
<Route path="/workers">
<ShowWorkers kick={kick} />
</Route>
</Switch>
</Router>
);
Expand Down
49 changes: 49 additions & 0 deletions client/src/components/ShowWorkersForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { useState ,useEffect } from "react";
import { Table } from "reactstrap";


import { getWorkers } from "../service";
const ShowWorkersForm = ({ kick }) => {
const [list, setList] = useState([]);
useEffect(() => getWorkers()
.then((data) => setList(data.workers))
.catch((err) => console.error(err)),[kick]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am thinking to have custom hook like form here https://www.digitalocean.com/community/tutorials/creating-a-custom-usefetch-react-hook. Then we would reuse it through our app and we would have loading, error and success states. On loading we could show bootstrap's Spinner. But we can add that later.



if (list) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Empty array is a truthy value so this if statement will always execute.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed.

return (
<Table striped>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Phone</th>
<th>WhatsApp</th>
</tr>
</thead>
<tbody>
{list.map((worker) => {
return(
<tr key={worker.id}>
<th scope="row">{worker.id}</th>
<td>{worker.name}</td>
<td>{worker.address}</td>
<td>{worker.email}</td>
<td>{worker.phone_number}</td>
<td>{worker.whatsapp}</td>
</tr>
);
} )}


</tbody>
</Table>
);
} else {
return <> </>;
}
};

export default ShowWorkersForm;
2 changes: 2 additions & 0 deletions client/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { default as CreateWorkerForm } from "./CreateWorkerForm";

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please could you explain why you've added this commit? Thanks to version control there isn't any need to add white space - if you absolutely need to add a TODO you can add commented out lines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just fixed the code according to all the comments from the reviews.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Harriethw I originally added that file to make it more clear and tidy when we will import them inside App.js or /pages. More info here https://www.digitalocean.com/community/tutorials/react-index-js-public-interfaces#using-indexjs

Copy link
Collaborator

Choose a reason for hiding this comment

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

@g1st ah I just meant that we don't need empty white space, which is all this commit seems to add unless I'm missing something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yesterday we made a customer list with Hadiyah. It doesn't work on her computer. I made the same code for workers and it works. I added empty spaces for her code. :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh sorry @Harriethw yes I agree. I already proposed @GTabala to use Prettier and it can take care of those empty spaces for us!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Prettier doesn't delete empty lines.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I don't know who does it then but when I save a file with loads of empty lines they get deleted for me.

export { default as ShowWorkersForm } from "./ShowWorkersForm";
16 changes: 16 additions & 0 deletions client/src/pages/ShowWorkers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";
import { Container } from "reactstrap";
import { ShowWorkersForm } from "../components";

const ShowWorkers = ({kick}) => {
return (
<Container>
<h2 className="text-center mt-4 mt-md-5 mb-5 mb-md-5">
Cleaners
</h2>
<ShowWorkersForm kick={kick} />
</Container>
);
};

export default ShowWorkers;
2 changes: 2 additions & 0 deletions client/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { default as CreateWorker } from "./CreateWorker.js";

export { default as ShowWorkers } from "./ShowWorkers.js";
14 changes: 13 additions & 1 deletion client/src/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { post } from "./api";
import { post, get } from "./api";

export const postWorkers = async (data) => {
const response = await post("/workers", data);
return response.data;
};








export const getWorkers = async (data) => {
const response = await get("/workers", data);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need to pass data to this function and to get method as this service is GET method and we are not sending anything to backend. They can be removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed.

return response.data;
};