How to ignore some routes with getStaticPath? - reactjs

I am using multilanguage with getStaticPath generation (English and Spanish) but i am getting some errors cause of default language.
I am trying to generate these routes:
[
{ params: { slug: 'ruta-en-espaniol', locale: 'es' } },
{ params: { slug: 'path-in-english', locale: 'en' } },
]
But Next.js is generating not 2 routes...even 3.
/en/ruta-en-espaniol
/es/ruta-en-espaniol
/en/path-in-english
is there some property to disable this? Maybe something like this?:
{ params: { slug: 'ruta-en-espaniol', locale: 'en', ignoreRoute: true } }

Related

Next.JS matching routes without leading unique keyword

I'm using next.js and I'm facing the follow issue:
I have
domain.com/{tag_name} that has pages/tag/[...slug],
domain.com/{location_name} that has pages/locations/[...slug]
domain.com/{landing_page_name} that has **pages/landing_pages/[...slug]
How I can capture all of them when I don't have a unique key to match them?
Let's say I have the follow real life links :
domain.com/awesome , something-else-that-user-can-input- tag
domain.com/visit-sofia, something-else-that-user-can-input - landing page
domain.com/sofia, something-else-that-user-can-input - location page
My current routes are
async rewrites() {
return [
{
source: "/:slug/things-to-do/:action",
destination: "/activity/:slug/things-to-do/:action",
},
{
source: "/things-to-do/:action",
destination: "/activity/things-to-do/:action",
},
{
source: "/:slug/stay/:action",
destination: "/accommodation/:slug/stay/:action",
},
{
source: "/packages/:slug",
destination: "/package/:slug",
},
{
source: "/:slug",
destination: "/location/:slug",
},
{
source: "/",
destination: "/home",
},
];
},

Create a custom element in EditorJs

I added EditorJs plugin in my react js application:
import ReactDOM from "react-dom";
import React, { Component } from "react";
import EditorJs from "react-editor-js";
import { EDITOR_JS_TOOLS } from "./constants";
class ReactEditor extends Component {
render() {
return (
<EditorJs
tools={EDITOR_JS_TOOLS}
data={{
blocks: [
{
type: "header",
data: {
text: "Editor.js",
level: 2
}
},
{
type: "paragraph",
data: {
}
},
{
type: "header",
data: {
text: "Key features",
level: 3
}
},
{
type: "list",
data: {
style: "unordered",
items: [
"It is a block-styled editor",
"It returns clean data output in JSON",
"Designed to be extendable and pluggable with a simple API"
]
}
},
{
type: "header",
data: {
text: "What does it mean «block-styled editor»",
level: 3
}
},
{
type: "paragraph",
data: {
text:
'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class="cdx-marker">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.'
}
},
{
type: "paragraph",
data: {
text:
'There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.'
}
},
{
type: "header",
data: {
text: "What does it mean clean data output",
level: 3
}
},
{
type: "paragraph",
data: {
text:
"Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below"
}
},
{
type: "paragraph",
data: {
text:
'Given data can be used as you want: render with HTML for <code class="inline-code">Web clients</code>, render natively for <code class="inline-code">mobile apps</code>, create markup for <code class="inline-code">Facebook Instant Articles</code> or <code class="inline-code">Google AMP</code>, generate an <code class="inline-code">audio version</code> and so on.'
}
},
{
type: "paragraph",
data: {
text:
"Clean data is useful to sanitize, validate and process on the backend."
}
},
{
type: "delimiter",
data: {}
},
{
type: "paragraph",
data: {
text:
"We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏"
}
},
{
type: "image",
data: {
file: {
url:
"https://codex.so/upload/redactor_images/o_e48549d1855c7fc1807308dd14990126.jpg"
},
caption: "",
withBorder: true,
stretched: false,
withBackground: false
}
}
],
version: "2.12.4"
}}
/>
);
}
}
ReactDOM.render(<ReactEditor />, document.getElementById("root"));
According to the documentation i can create a custom element:
render() {
return (
<EditorJs holder="custom">
<div id="custom" />
</EditorJs>
);
}
Question: I want to add as a custom element an input: <input type="text"/>, but i don't manage even if i do:
<EditorJs holder="custom">
<input id="custom" type="text"/>
</EditorJs>
Who knows how to add this custom element in the plugin above?
demo: https://codesandbox.io/embed/react-editor-js-23opz
I found in the documentation that i can create a plugin for editor.js:
https://editorjs.io/the-first-plugin. One of example looks like this:
class SimpleImage {
static get toolbox() {
return {
title: 'Image',
icon: '<svg width="17" height="15" viewBox="0 0 336 276" xmlns="http://www.w3.org/2000/svg"><path d="M291 150V79c0-19-15-34-34-34H79c-19 0-34 15-34 34v42l67-44 81 72 56-29 42 30zm0 52l-43-30-56 30-81-67-66 39v23c0 19 15 34 34 34h178c17 0 31-13 34-29zM79 0h178c44 0 79 35 79 79v118c0 44-35 79-79 79H79c-44 0-79-35-79-79V79C0 35 35 0 79 0z"/></svg>'
};
}
render() {
return document.createElement('input');
}
save(blockContent) {
return {
url: blockContent.value
}
}
}

Gatsby API endpoint

I'm new to gatsby/graphql and I'm using the gatsby-source-custom-api plugin and based on the gatsby-config example:
// Lets assume this is the data from your API:
const exampleDataFromApi = [
{
url: "post-1",
images: [
{
url: "image-1.jpg",
modified: 1556752476267
},
{
url: "image-2.jpg",
modified: 1556752702168
}
],
author: {
firstname: "John",
lastname: "Doe"
}
}
];
// This is the content of your gatsby-config.js
// and what you need to provide as schema:
module.exports = {
plugins: [
{
resolve: "gatsby-source-custom-api",
options: {
url: {
development: "http://my-local-api.dev", // on "gatsby develop"
production: "https://my-remote-api.com" // on "gatsby build"
},
imageKeys: ["images"],
rootKey: "posts",
schemas: {
posts: `
url: String
images: [images]
author: author
`,
images: `
url: String
modified: Int
`,
author: `
firstname: String
lastname: String
`
}
}
}
]
};
I was wondering if there's a way to change the endpoint of the API based on the current page? For example current URL is https://mywebsite.com/data1 and API endpoint https://my-remote-api.com/data1.
I'm trying to reproduce this:
componentDidMount() {
fetch('https://my-remote-api.com/' + this.props.endpoint)
.then(response => response.json())
.then(data => this.setState({ data }));
}
but with graphql.

i18next deep key value react native

{
"content": {
"en": {
"transfer_screen_amount_title": "Amount",
"demo_screen_welcome_title": "Welcome"
},
"bm": {
"transfer_screen_amount_title": "Jumlah",
"demo_screen_welcome_title": "Selamat Datang"
}
}
}
I reckon that in order to retrieve the value, we can set it like this t('content.en.transfer_screen_amount_title').
what if i want to use it like this instead t('transfer_screen_amount_title') ??
It depends on how your translations are loaded: https://www.i18next.com/how-to/add-or-load-translations
Here an example loading the resources on init:
https://github.com/i18next/react-i18next/blob/master/example/react-native/App.js#L20
So your code would look like this:
i18next
.use(languageDetector)
.use(initReactI18next)
.init({
fallbackLng: 'en',
debug: true,
defaultNS: 'content',
resources: {
en: {
content: {
transfer_screen_amount_title: 'Amount',
demo_screen_welcome_title: 'Welcome',
},
},
bm: {
content: {
transfer_screen_amount_title: 'Jumlah',
demo_screen_welcome_title: 'Selamat Datang',
},
},
},
});
// t('transfer_screen_amount_title')
Maybe read also: https://www.i18next.com/principles/namespaces

Apollo Client - How to test a component that uses multiple queries, using HOC components that use compose

I am reading over the docs for testing React/Apollo components Link. If the component has one query, it seems pretty simple to test it.
const mocks = [
{
request: {
query: GET_DOG_QUERY,
variables: {
name: 'Buck',
},
},
result: {
data: {
dog: { id: '1', name: 'Buck', breed: 'bulldog' },
},
},
},
];
it('renders without error', () => {
renderer.create(
<MockedProvider mocks={mocks} addTypename={false}>
<Dog name="Buck" />
</MockedProvider>,
);
});
My component is a little different than the one provided in the documentation.
It doesn't use the useQuery hook, instead I am opting for the HOC approach as outlined here.
I have two queries that my function uses, and so I use two graphql functions and combine them together using compose, as recommended in the docs.
My component is exported like this:
export default compose(withQueryA, withQueryB)(MyComponent);
const withQueryA = graphql(QUERY_A, {
name: "QueryA",
options: (props) => ({
variables: {
foo: props.foo,
},
}),
});
const withQueryB = graphql(QUERY_B, {
name: "QueryB ",
options: (props) => ({
variables: {
bar: props.bar,
},
}),
});
What I'm trying to do is provide the mocks object with multiple objects, each containing a request/result for the corresponding query. I just wanted to know if anyone has been testing their components in a similar way or if there is a better suggestion.
const mocks = [
{
request: {
query: QUERY_A,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
{
request: {
query: QUERY_B,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
];
I'm also confused about what to put in the result object. When I console.log what is actually returned to the component when making a query in production, it has the data plus error, fetchMore, loading, networkStatus. Do I have to put all those things in the mocks as well?
My feeling was correct. The result object should look something like this:
const mocks = [
{
request: {
query: QUERY_A,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
{
request: {
query: QUERY_B,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
];

Resources