Skip to content

Commit

Permalink
Fix user-editor relationship issue by removing relation field (#5)
Browse files Browse the repository at this point in the history
* Fix user relationship issue by removing

* Add slug; remove user
  • Loading branch information
cschenio authored Mar 2, 2023
1 parent 58a5267 commit 2191430
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 161 deletions.
134 changes: 0 additions & 134 deletions gatsby/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,137 +3,3 @@
*
* See: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
*/

const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);

// Define the template for blog post
// const blogPost = path.resolve(`./src/templates/blogPost.js`);


/**
* @type {import('gatsby').GatsbyNode['createPages']}
*/
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;

const result = await graphql(`
query BlogPostsQuery {
allStrapiPost {
nodes {
title
slug
}
}
}
`);

if (result.errors) {
reporter.panicOnBuild(`There was an error loading your blog posts`, result.errors);
return;
}

const posts = result.data.allStrapiPost.nodes;

/* // Use /src/pages/posts/{StrapiPost.slug}.js instead.
if (posts.length > 0) {
posts.forEach((post) => {
createPage({
path: post.slug,
component: blogPost,
context: {
post: post,
},
});
});
}
*/
}
/*
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
// Get all markdown blog posts sorted by date
const result = await graphql(`
{
allMarkdownRemark(sort: { frontmatter: { date: ASC } }, limit: 1000) {
nodes {
id
fields {
slug
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`There was an error loading your blog posts`, result.errors);
return;
}
const posts = result.data.allMarkdownRemark.nodes;
// Create blog posts pages
// But only if there's at least one markdown file found at "content/blog" (defined in gatsby-config.js)
// `context` is available in the template as a prop and as a variable in GraphQL
if (posts.length > 0) {
posts.forEach((post, index) => {
const previousPostId = index === 0 ? null : posts[index - 1].id;
const nextPostId = index === posts.length - 1 ? null : posts[index + 1].id;
createPage({
path: post.fields.slug,
component: blogPost,
context: {
id: post.id,
previousPostId,
nextPostId,
},
});
});
}
};
*/
/**
* @type {import('gatsby').GatsbyNode['onCreateNode']}
*/
/*
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
};
*/

/**
* @type {import('gatsby').GatsbyNode['createSchemaCustomization']}
*/
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;

createTypes(`
type SiteSiteMetadata {
author: Author
}
type Author {
name: String
summary: String
}
type StrapiPost implements Node {
title: String
slug: String
}
`);
};
2 changes: 1 addition & 1 deletion gatsby/src/components/avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Avatar = ({ author, swoosh }) => {

const image = getImage(author.avatar.localFile);
return (
<Link to={`/editors/${author.user.username}`}>
<Link to={`/editors/${author.slug}`}>
<GatsbyImage
className={imageClassName}
image={image}
Expand Down
2 changes: 1 addition & 1 deletion gatsby/src/components/post/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as styles from "./info.module.css";
const PostInfo = ({ date, author }) => {
const authorDateBlock = (
<div className={styles.author}>
<Link className={styles.authorName} to={`/editors/${author.user.username}`}>
<Link className={styles.authorName} to={`/editors/${author.slug}`}>
<div>{ author.fullName }</div>
</Link>
<div className={styles.date}>{ date }</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const pageQuery = graphql`
strapiEditor(id: { eq: $id }) {
fullName
slug
bio {
data {
id
Expand All @@ -43,9 +44,6 @@ export const pageQuery = graphql`
}
}
}
user {
username
}
posts {
title
slug
Expand Down
6 changes: 2 additions & 4 deletions gatsby/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const BlogIndex = ({ data, location }) => {
{ editors.map((e) => (
<li>
<Avatar author={e}/>
<Link to={"/editors/" + e.user.username}>{ e.fullName }</Link>
<Link to={"/editors/" + e.slug}>{ e.fullName }</Link>
</li>
))
}
Expand Down Expand Up @@ -86,16 +86,14 @@ export const pageQuery = graphql`
allStrapiEditor {
nodes {
fullName
slug
avatar {
localFile {
childImageSharp {
gatsbyImageData(width: 100)
}
}
}
user {
username
}
}
}
}
Expand Down
Empty file removed gatsby/src/pages/posts.js
Empty file.
4 changes: 1 addition & 3 deletions gatsby/src/pages/posts/{StrapiPost.slug}.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ export const pageQuery = graphql`
}
author {
fullName
slug
avatar {
localFile {
childImageSharp {
gatsbyImageData(width: 100)
}
}
}
user {
username
}
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions strapi/src/api/editor/content-types/editor/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"info": {
"singularName": "editor",
"pluralName": "editors",
"displayName": "Editor"
"displayName": "Editor",
"description": ""
},
"options": {
"draftAndPublish": false
Expand All @@ -24,26 +25,21 @@
"type": "string"
},
"avatar": {
"type": "media",
"multiple": false,
"required": false,
"allowedTypes": [
"images",
"files",
"videos",
"audios"
],
"type": "media",
"multiple": false,
"pluginOptions": {
"i18n": {
"localized": true
}
}
},
"user": {
"type": "relation",
"relation": "oneToOne",
"target": "plugin::users-permissions.user",
"inversedBy": "editor"
},
"bio": {
"pluginOptions": {
"i18n": {
Expand All @@ -57,6 +53,15 @@
"relation": "oneToMany",
"target": "api::post.post",
"mappedBy": "author"
},
"slug": {
"pluginOptions": {
"i18n": {
"localized": true
}
},
"type": "string",
"required": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@
"target": "plugin::users-permissions.role",
"inversedBy": "users",
"configurable": false
},
"editor": {
"type": "relation",
"relation": "oneToOne",
"target": "api::editor.editor",
"mappedBy": "user"
}
}
}

1 comment on commit 2191430

@vercel
Copy link

@vercel vercel bot commented on 2191430 Mar 2, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

blog – ./

blog-git-main-cschenio.vercel.app
blog-cschenio.vercel.app
blog-omega-ten-46.vercel.app

Please sign in to comment.