Working on this demo → https://github.com/frontity-juanmaguitar/bootstrap-theme-demo
that took mars-theme
as a reference
I realized there are a lot of operations where you need to know exactly the structure of the data received and the location of that data
Trying to apply good patterns in software development I tried to extract that logic into an external file
Something like…
export const getMedia = state => id => {
const media = state.source.attachment[id];
const srcset = Object.values(media.media_details.sizes)
.map(item => [item.source_url, item.width])
.reduce(
(final, current, index, array) =>
final.concat(
`${current.join(" ")}w${index !== array.length - 1 ? ", " : ""}`
),
""
) || null;
return { media, srcset }
}
export const getTaxonomy = state => taxonomy => idTaxonomy => state.source[taxonomy][idTaxonomy];
export const getTaxonomyCapitalized = taxonomy => taxonomy.charAt(0).toUpperCase() + taxonomy.slice(1);
export const getAuthor = state => idAuthor => state.source.author[idAuthor]
export const getPost = state => type => idPost => state.source[type][idPost]
I think it would be great for developers creating things w/ Frontity to have a set of helpers directly available from state that they can use to access some common data:
-
getAuthor
→ get author details given an id -
getMedia
→ get media details given an id
…and so on
We could document this in the official documentation and I think this approach has some advantages:
- We allow people to decouple their code from the current WP Rest API structure (I don’t know how often is changing but it’s usually a good practice)
- We simplify the development (I think it’s easier to remember
state.getAuthor(2)
thanstate.source.author[idAuthor]
)
Of course the whole state.source
should continue to be available but we could make developments w/ Frontity easier by providing a set of tools to access the data in an easier way
The mindset behind is something like → It's not the responsibility of a component to know how to get the details of an author, I'm just going to use this method to get the details given its id
I’m not sure what is the best way to handle this, if there’s some kind of client we can actually use, or if we would complicate things even more by adding more methods to learn
What are your thoughts about this?