- Tech Stack
- Front-End:
React,TailwindCSS - Back-End :
Sanity CMS
- Front-End:
- Setting Up Schemas. (Refer).
- We have 5 Schema :
-
Google Login Using @react-oauth/google package.
- On Clicking Google Login button provided from this package, if successful, it returns a response containing the jwt token.
- We decode this token using
jwt-decodewhich gives the logged in user details Refer. - Then, we send the details to the
/api/authroute we created Auth route where we send the details to sanity which adds this data to the db if it is not already present.
-
If Login successful, we also need to add this user to Sanity
userSchema if it was not already added. We do this with Sanity Client methodcreateIfNotExistsRefer. But we run into some permission & Cors issue if we try to add anything to Sanity for the 1st time. We need to do the following before interacting with Sanity Client from our App:- To Setup Sanity Client, we need
projectIdandtoken. For that we typesanity managein terminal from backend which opens SanityIO Project settings. Here we can find projectId.- For Token, we need to goto
API-->Tokensand then Add new Token. If we want write permissions as well, we need to give itEditorpermission. This will generate the token.
- For Token, we need to goto
- Now if we try to add user(or any other request) to Sanity backend, we will get
CORSerror. To fix this, again inAPItab in Sanity Manage, we will seeCORS originstab. Here we simply add our frontend baseURL and we are good to go.
- To Setup Sanity Client, we need
- Querying Data from Sanity uses GROQ (Graph-Relational Object Queries) language (somewhat like GQL queries). It has a particular syntax for queries. Refer. Docs.
- We query sanity for getting pins based on certain categories or searchTerm which we used data filtering, projection, dereferencing and text matching. Refer. Docs for Query. Docs for Text-matching.
- Updating Document in Sanity called Patching (in our case we did it for adding data to save array inside Pin document)
Patchmethod is used to update document in Sanity. Refer Code, Docs.
- For displaying all Pins in Page we used
MasonryLayoutinstead of simple Grid or Flex-box.We used react-masonry-css package for it which arranges which places items in fixed no of rows(or columns) which are responsive according to breakpoints but heights(or widths) of items are unequal.
- We use Sanity Client function called assets.upload() to upload file/image to sanity which then returns the file document if successful.Refer.