A smart <LikeButton>
React component that works out of the box!
The <LikeButton>
component receives two parameters:
- topic: What is being liked
- user: Who is liking
<LikeButton topic="brownies" user="Mary" />
The example above will display a like button, and automatically hook it up with a database to display the amount of people who likes brownies. Also, if Mary has already liked brownies before, it will render filled. Mary can also change her mind and click on the like button again, this time, decreasing the number of likes in the brownie topic.
Suppose that Mary likes brownie. And 1000 more people do. Then, by default, in John's browser, the amount of likes will remain the same until he reloads the page.
The <LikeButton>
has a property called live
. Use it like this:
<LikeButton topic="brownies" user="John" live />
And have the like count updated automatically in John's screen.
It updates the count for every user who has the live
flag turned on
Both topic and user can be a username, an id, a hash, a page url...
You decide it. Just hand down the topic and user to the <LikeButton>
, and the rest is with us.
git clone https://github.com/alexmachina/rock-like.git ~/rock-like
cd ~/rock-like
docker-compose up --build -d
open localhost:3000
on your browser.
Both the front-end and the back-end are writter in TypeScript. The back-end is a Node API, built with express server, integrated with a MongoDB database. The front-end is written in React, using CRA.
Read more about the front-end here
Read more about the back-end here
Here are features that are important, but couldn't make it to the project yet.
- A good improvement would be to memcache the GET request for a couple of seconds.
- Also, find a way to queue updates to the database, in memory too, and then update the
users
array at once with multiple entries. That would reduce the number of operations in the mongo database.
- It can be done with jest and shelf, spaning a mongodb instance in memory, and then testing the endpoints with supertest. https://jestjs.io/docs/en/mongodb
- Add unit testing for the components and the hook
- Integration tests can be configured using https://github.com/mswjs/msw as a tool to take the API into account when writing the tests, without losing performance. And no need to mock module on API calls.
-
We're using the cors middleware, and allowing any origin. We can use the cors middleware, reading from a hosts allowlist, to restrict endpoint access to these known hosts only.
-
In production, we can add a cloudflare in our layer, to prevent DDoS attacks and other malicious stuff
-
Add apiKey authentication and trakcing
- At the moment, we can't have two topics or two users from different contexts (e.g different customers or differente applications). Namespacing the records is a good thing to add.
- It would be pretty welcoming if the like button's visuals could be customized.
- The
<LikeButton>
can have a version deployed that is deployed to a CDN and when required, embedded on an specific element on the page. By doint this, clients that don't use React and even non-developers can integrate the like button on their websites. A behavior similar to https://developers.facebook.com/docs/plugins/like-button/