Skip to content

Instantly share code, notes, and snippets.

@joaogarin
Last active April 8, 2021 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaogarin/0f0b94185d7f6903cce937c3bc0d4d41 to your computer and use it in GitHub Desktop.
Save joaogarin/0f0b94185d7f6903cce937c3bc0d4d41 to your computer and use it in GitHub Desktop.
Page and component structure

Summary

This document explains a generic approach to component structures in jobiqo-decoupled.

Naming pattern

Every component should use PascalCase naming in the folder (which is also required in react for components) like ResumeCompleteness but pages use a kebab-case like job-apply this is because folders map to URL's and so makes it cleaner and simpler to have proper urls like this in nextjs

Overridability

One thing that is important to consider is what parts of components we want to optimize for "overriding", and that is probably the rendering part and not so much the data fetching logic (and maybe even error and loading handling)

So for this I think a good way for us to go about it would be to inside lets say a ResumeFolderTesting component have the following folders:

  • graphql : Includes any queries, mutations and fragments used for this component
  • view : Which would include a ResumeFolderTestingView component which receives already the "processed" props (e.g. resume: Resume or also maybe errors if there were any). This would be tipically the compponent people might be overriding on client projects but this way all the data fetching and error handling is already being done
  • components : Ofc most components might have subcomponents and its fine that they are included as well, they should tipically just follow the same pattern (e.g. a graphql folder with specific queries for that subcomponent).
  • utils : If any utilities are needed (maybe some function that for example gets a logo from the job or company depending if there is a logo on the company) can be added here. Basically just helpers
  • tests : Any unit testing that is needed for a component

This folder structure works particularly well with the visual studio code extension https://marketplace.visualstudio.com/items?itemName=PKief.material-icon-theme which makes it very clear when looking at a component what is happening (see attachments)

The View part I think its mostly to separate the "overridable" part from the data fetching.

Importing

its important to import { Component } from '~/jobiqo/components' because this will ensure our overrides folder works as expected.

@joaogarin
Copy link
Author

joaogarin commented Apr 6, 2021

Here is an example of what a component would tipically look like (ofc many components wouldnt have all of this)

Screenshot from 2021-04-06 11-27-05

@joaogarin
Copy link
Author

One caveat maybe with the components subfolder is there should be only one, otherwise we get deeply nested components folders which just makes everything very confusing maybe

@Cryt1c
Copy link

Cryt1c commented Apr 6, 2021

I absolutely agree with this folder structure. Making some kind of MVC separation makes a lot of sense. Often you only want to override the view and leave the controller (data fetching etc) untouched.

I would also name the main files the same as the component name. In the example above we would name the component ResumeFolderTesting.tsx instead of index.tsx. The advantage that it brings is that you can quickly find a component by using the files search. Otherwise every component is called index.tsx and it's really hard to navigate to it.
I use "Search by file name" (CTRL + P on my setup) to jump around files and it just does not work if everything is named index.tsx.

@joaogarin
Copy link
Author

I use "Search by file name" (CTRL + P on my setup) to jump around files and it just does not work if everything is named index.tsx.

Yeah this makes sense to me. I would be fine with adapting that part.

@sepal
Copy link

sepal commented Apr 8, 2021

The structure looks good to me. Usually we shouldn't have too many component specific sub componenets, so I don't think this will be a problem.

Making some kind of MVC separation makes a lot of sense.

I don't think you can achive a real MVC with React, since you always tie logic to the view :P

Regarding index.js:

If we don't have an index.tsx then we can't import components like this:

import {MyComponent} from '~/jobiqo/components/MyComponent'

We would have to use

import {MyComponent} from '~/jobiqo/components/MyComponent/MyComponent'

right?

@joaogarin
Copy link
Author

joaogarin commented Apr 8, 2021

MVC : Yes I think the idea for me is more like making it easier to adapt certain styling on something and when doing that not having to "care" about the whole data fetching logic.

Import : yes that would be the downside.

I dont manually type imports really so my opinion there is not too strong. In vscode it will just auto import files..I normaly just go an make sure its using the "~/jobiqo" syntax and not like "./../../../" (which usually just works in vscode so I dont have to even do that)

Screenshot from 2021-04-08 09-20-48

@Cryt1c
Copy link

Cryt1c commented Apr 8, 2021

We would have to use
import {MyComponent} from '~/jobiqo/components/MyComponent/MyComponent'
right?

Yes, but the quicker navigation outweighs this downside for me.

@sepal
Copy link

sepal commented Apr 8, 2021

Have try navigating like joao? I also just go the component and not file directly.

@Cryt1c
Copy link

Cryt1c commented Apr 8, 2021

Do you mean navigating using the project tree? Yes, I do this because it's cumbersome using the files search.
How do you go to the component? By using your mouse?

@sepal
Copy link

sepal commented Apr 8, 2021

On mac you can use you [apple]+left click on a component or left click and then go to definition or apperenlty also only F12.

@sepal
Copy link

sepal commented Apr 8, 2021

On mac you can also open up the command line and enter @ComponentName:

Bildschirmfoto 2021-04-08 um 13 49 04

@Cryt1c
Copy link

Cryt1c commented Apr 8, 2021

Yep, navigating to a component by using CTRL + Click is working on Linux too.
Nice your second tip is great! I can work with that 👍 (I have bound my CTRL + P now to "Go to symbol in workspace").
If you are using #ComponentName it will search the whole workspace instead of just the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment