Not able to Query hyperlinks from GraphQL on Gatsby JS - reactjs

I'm working on a Blog on Gatsby JS, and I'm having an issue with the hyperlinks, and I'm not able to solve it.
I have the following query
const options = {
renderNode: {
"embedded-asset-block": (node) => {
const alt = node.data.target.fields.title['es']
const url = node.data.target.fields.file['es'].url
return <img alt={alt} src={url} />
},
[INLINES.HYPERLINK]: (node) => {
if(node.data.uri.indexOf('youtube.com') !== -1){
return(
<iframe width="560" height="315" src={node.data.uri} frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
)
}else{
return(
<a href={node.data.uri}>{node.content.value}</a>
)
}
}
}
}
From this query, I'm able to add assets and youtube videos. The issue is, that when I add a hyperlink, they are shown on the page but without text.
I only see the and not the text of this link.
I know the problem is here <a href={node.data.uri}>{node.content.value}</a>, but I'm not able to query the value.
This is GraphQL:
"body": {
"json": {
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "Desde ",
"marks": [],
"data": {}
},
{
"nodeType": "hyperlink",
"content": [
{
"nodeType": "text",
"value": "FelixDigital",
"marks": [],
"data": {}
}
I need to query the value (FelixDigital)
Does anybody can help me?
Thank you so much,

Can you do a debugger inside this code?
It's hard to say.. but what I would check is the value of node. To me it looks like content is an array and inside the first item of the array you have value so to me it should look more like node.content[0].value.

I'm guessing you are using Contentful to pull your data into your blog using GraphQL. With Contentful you must use documentToReactComponents on JSON nodes (all rich text items). To obtain FelixDigital, you may want to do something instead like:
import { documentToReactComponents } from '#contentful/rich-text-react-renderer';
return(
<div>
{documentToReactComponents(videos.videoItem.videoLinks.json, options)}
</div>
)
This will then use the iframe you set up in const options and grab the src uri.
More info can be found here on rendering rich text with contentful

Related

changed attributes not waving with Wordpress block

I have a fairly simple block in a plugin, built using npx #wordpress/create-block pn-DisplayBlocks. Attributes, in block.json, are:
(code changed as per fixes below)
"attributes": {
"pid": {
"type": "text",
"default": "444"
}
}
save.js is:
import { __ } from '#wordpress/i18n';
import { useBlockProps } from '#wordpress/block-editor';
export default function Save( {attributes} ) {
console.log( "SAVING", attributes )
return (
<p { ...useBlockProps.save() }>
{ __( 'Pn Image – hello from the saved content! ' + attributes.pid, 'pn-image' ) }
</p>
);
}
and edit.js is:
import { __ } from '#wordpress/i18n';
import { useBlockProps } from '#wordpress/block-editor';
import './editor.scss';
import { __experimentalNumberControl as NumberControl } from '#wordpress/components';
export default function Edit( {attributes, setAttributes} ) {
console.log( "pid", attributes.pid )
return (
<p { ...useBlockProps() }>
{
__( 'Pn Image – hello from the editor! ' + attributes.pid, 'pn-image' )
}
<NumberControl
label="PID"
isShiftStepEnabled={ true }
onChange={ value => { setAttributes( {pid: value})} }
shiftStep={ 10 }
value={ attributes.pid }
/>
</p>
);
}
NumberControl value updates pid and the block with the correct number value when changed, that all works fine, but if I preview the page I see the default value of pid showing, not the one I've just change it to. Same if I update the page to save it; console.log shows the new pid value but when reloaded into the edit page, pid reverts to the default value. Maybe I'm staring too hard but for the life of me I can't see what's wrong. Also, I'm fairly new to this so I may be doing something idiotic.
save & edit called from registerBlockType in index.js:
import { registerBlockType } from '#wordpress/blocks';
import './style.scss';
import Edit from './edit';
import Save from './save';
registerBlockType( 'pn-displayblocks/pn-image', {
edit: Edit,
save: Save,
} );
Attributes in block.json:
{
"apiVersion": 2,
"name": "pn-displayblocks/pn-image",
"version": "0.1.0",
"title": "Pn Image",
"category": "widgets",
"icon": "smiley",
"description": "Example block written with ESNext standard and JSX support – build step required.",
"supports": {
"html": false
},
"textdomain": "pn-displayblocks",
"editorScript": "file:./build/index.js",
"editorStyle": "file:./build/index.css",
"style": "file:./build/style-index.css",
"attributes": {
"pid": {
"type": "text",
"default": "444"
}
}
}
Did you try to store your attributes in comments of block ?
In according with documentation gutenberg :
The available source values are:
– (no value) – when no source is specified then data is stored in the block’s comment delimiter.
– attribute – data is stored in an HTML element attribute.
– text – data is stored in HTML text.
– html – data is stored in HTML. This is typically used by RichText.
– query – data is stored as an array of objects.
– meta – data is stored in post meta (deprecated)
Can you try with ?
"pid": {
"type": "text",
"default": "444"
}

Access key: [ { "Key":"Value"} ] inside of Object that is inside of another Array?

I am trying to access the Options key value and get the Name and Value in my jsx but I am unable to use map on field.Options (when I hover over Options there is a question mark next to it).
I have tried field.Options[0].Name , field.Options["Name"], field.map().
I am just confused on how to access those values. An explanation would be appreciated.
I have an Array[] of Objects{} like below and I am trying to get the "Options" objects values.
{
"Type": "rating",
"Key": "professionalism",
"LabelBranding": "api#professionalism#placeholder",
"Label": "Professionalism:",
"Required": false,
"Options": [
{
"Name": "Smiley",
"Value": "0123456789"
}
]
},
I cant use a (for of ) in JSX. So I am wondering how to access this?
I think you were close there. Have you tried this?
{field.Options.map(block => (
<p key={block.Name}>{block.Value}</p>
))}
Since you want to iterate the field.Options array.
This was a case of not checking if the "Options" array was there. The 1st object that was coming back did not have the "Options" key value like the rest of them. So I added a conditional around it and it worked swimmingly.
{field.Options !== undefined
&& field.Options.map((block) => {
if (block.Value !== undefined || block.Name !== undefined) {
return (
<ul>
<li key={block.Name}>{block.Value}</li>
<li key={block.Name}>{block.Name}</li>
</ul>
);
}
return null;
})
}

Display JSON array in react component

i have Array of image urls:
"image": [
"https://i.imgur.com/QHQaUxi.jpg",
"https://i.imgur.com/GNjH4mZ.jpg",
"https://i.imgur.com/py9IkGa.jpg",
],
my YachtDetailsData looks like this:
{
"id": "1",
"name": "Rarity",
"title": "Modern facilities, sophisticated style and chic deck areas make luxury yacht RARITY one of the most sought-after charter yachts in her class. Her large beam of 10m offers extra onboard space with wide deck areas, making RARITY the ideal yacht tocharter for events such as the Grand Prix",
"description": "The 180'5 / 55m Custom motor yacht 'Rarity' was built in 2008 by Rossi Navi and last refitted in 2017. Officina Italiana Design is responsible for her beautiful exterior and interior design. Previously named Syna, her interior combines timeless styling and beautiful furnishings. She offers large volumes, an open plan Main Salon and dining room, a fully equipped gym and a sky lounge with 220-inch cinema screen. Rarity’s interior layout sleeps up to 12 guests in 6 guest cabins.",
"price": "235.000€",
"guests": "12 guests",
"length": "55 meters",
"build": "2008",
"cabins": "06",
"refit": "2017",
"maxSpeed": "15.5 kt",
"builder": "Rossinavi",
"location": "Mediterranean",
"crew": "12",
"coverImage": "https://i.imgur.com/U7yw2EF.jpg",
"image": [
"https://i.imgur.com/QHQaUxi.jpg",
"https://i.imgur.com/GNjH4mZ.jpg",
"https://i.imgur.com/py9IkGa.jpg",
"https://i.imgur.com/4n3sZxb.jpg",
"https://i.imgur.com/8CbKNUm.jpg",
"https://i.imgur.com/R5eIKmQ.jpg",
"https://i.imgur.com/zEJ8Oxa.jpg",
"https://i.imgur.com/PfuoqQQ.jpg",
"https://i.imgur.com/IgxuEA7.jpg",
"https://i.imgur.com/8GNUaSx.jpg",
"https://i.imgur.com/3kUArby.jpg",
"https://i.imgur.com/1xhp0DT.jpg",
"https://i.imgur.com/flmYnDc.jpg"
],
"video": ""
},
i have got 7 objects with image array and i want do display each image in React image slider:
import YachtDetailsData from "../data/YachtList.json"
interface Props {
match: any
}
const YachtDetails: React.FC<Props> = ({match}) => {
const yachtData = YachtDetailsData.find(yacht => yacht.id === match.params.id)
const slider = (
<AwesomeSlider>
<div><img src={yachtData?.image[]} alt=""/></div>
</AwesomeSlider>
);
i don't no what to do. Do i need to map them? I am beginner so any help is welcome.
Try this..
const YachtDetails: React.FC<Props> = ({ match }) => {
// here im just spreading/pushing the object in an array, so it will make use
// of the `find` method which is an array method.
const newYachtData = [{...YachtDetailsData}]
const yachtData = newYachtData.find(
(yacht) => parseInt(yacht.id) === match.params.id
);
const slider = (
<AwesomeSlider>
<div>
{yachtData?.image.map((image, key) => (
<div key={key}>
<img src={image} alt="" />
</div>
))}
</div>
</AwesomeSlider>
);
};

API display array map in react

im having problem displaying this 2 loops in map. im using this map for the display but cant seem to display both of the loop.
Can i display them inside one loop only? like loop inside loop? Need help here tia
Choices Data
{testQuestionData.map(({choices}) => (
<div>{choices.map((tchoices)=>
<div>
{tchoices.testChoices}
</div>
)}
</div>
))}
Question Data
{testQuestionData.map(td=>(
<div className="col-md-12">
{td.question.testQuestion}
<label>Choices: {td.choicesDisplay}</label>
</div>
))}
{
"question": {
"testQuestion": "Earth is flat?",
},
"choices": [
{
"testChoices": "true",
"
}
{
"testChoices": "false",
"
}
]
}
You have badly formatted data. You have a random " after the test choices and no , between the choices.
Your data should look like this
{
"question": {
"testQuestion": "Earth is flat?",
},
"choices": [
{
"testChoices": "true",
},
{
"testChoices": "false",
}
]
}

how to get key and value pair from json like below:-

I am trying to get key and value from the below json but do not know how can i get it dynamically. for Angular 5 project.
Component:- here is the method which i want to use in my component.
public AnalyticsRes(_AnalyticsResponse) {
let ButtonAttribute = _AnalyticsResponse.ButtonAttribute.split(',');
let Button2Attribute = _AnalyticsResponse.Button2Attribute.split(',');
let key1 = ButtonAttribute['know-more1'];
let key2 = ButtonAttribute['know-more2'];
}
I am not sure if syntax is correct kind of newbie in angular
and here is the HTML CODE:-
<div class="grid-i" *ngIf="response.Button1.trim() || response.Button2.trim()">
<a class="xyz" *ngIf="response.Button1.trim()" (click)="ManagaeNavigation(response.ButtonLink);defaultClick();"
href="{{response.ButtonLink}}" [analytics-promo]='response.BtnAnalytic ? response.BtnAnalytic : null'>
<span class="buttonClass">{{response.ButtonText}}</span>
<span class="arrowIcon"></span>
</a>
<a class="xyz" *ngIf="response.Button2.trim()" href="{{response.Button2Link}}"
[attr.data-analytics-promo]='response.Btn2Analytic ? response.Btn2Analytic : null'>
<span class="buttonClass">{{response.Button2}}</span>
<span class="arrowIcon"></span>
</a>
</div>
here is my json :-
{
"Items": [
{
"Title": "Get it:",
"field_background_color": "is-bg-color-blue",
"ButtonLink": "/myUrl",
"ButtonText": "Visit Site",
"ButtonAnalytic": "link-analytics:first, link-analytics:second",
"ButtonTwoLink": "http://dummy.com",
"ButtonTwoText": "Know More",
"Button2Analytic": "know-more:first, know-more:second",
**"ButtonAttribute": "link-analytics:first, link-analytics:second",
"Button2Attribute": "know-more:first, know-more:second",**
"ButtonLinkAbsolute": "/abc",
"ButtonTwoLinkAbsolute": "http://dummy.com",
"Students": [
{
"title": "No",
"description": "Visit a Outlet dummy ",
},
{
"title": "No",
"description": "FREE free dummy.",
},
{
"title": "No",
"description": "Bring home",
}
]
}
],
"Status": "OK",
"StatusMessage": "SUCCESS"
}
Please help me to get key and value from :-
"ButtonAttribute": "link-analytics:first, link-analytics:second",
"Button2Attribute": "know-more:first, know-more:second",
as there are comma separated values from json and i have to get these into array in my component method, and want to use them in my html ?? here another challenge is that how can i bind multiple attribute to single lement in angular 6?? code plunkar will be more helpful. Thank You in advance.

Resources