How do we upload to Cloudflare images Direct Creator Upload URL using FilePond? - cloudflare

Uploading to Cloudflare Images with FilePond fails returning a 400. I'm testing it side by side with a simple form. I get the Direct Creator Upload URL from our api, pass it in to both components. The form always succeeds, while FilePond always fails. Can anyone suggest a fix please?
https://developers.cloudflare.com/images/cloudflare-images/upload-images/direct-creator-upload/
It's returning a 400 Bad Request. Can someone from CloudFlare or from FilePond figure out why CF Images is responding that the request from FilePond is malformed?
import { useState } from "react";
import { FilePond, registerPlugin } from "react-filepond";
import FilePondPluginImagePreview from "filepond-plugin-image-preview";
import FilePondPluginFileValidateType from "filepond-plugin-file-validate-type";
import DefaultButton from "./BaseComponents/defaultButton";
// Register the plugins
registerPlugin(
FilePondPluginImagePreview,
FilePondPluginFileValidateType,
);
interface Props {
uploadURL: string;
}
export default function UploadAvatar(props: Props) {
const { uploadURL } = props;
const [files, setFiles] = useState<any>([]);
return (
<>
{/* This simple form uploads successfully */}
<form action={uploadURL} method="post" encType="multipart/form-data">
<input type="file" id="myFile" name="file" />
<DefaultButton type="submit">Upload Image</DefaultButton>
</form>
{/* FilePond upload fails with a 400 */}
<div className="h-44 w-44 rounded-full ring-4 ring-light-control-accent-rest dark:ring-dark-control-accent-rest">
<FilePond
files={files}
onupdatefiles={setFiles}
allowMultiple={false}
maxFiles={1}
server={uploadURL}
name="file"
labelIdle='Drag & Drop your files or <span class="filepond--label-action">Browse</span>'
acceptedFileTypes={["image/*"]}
onerror={(error) => console.log(error)}
/>
</div>
</>
);
}

Okay so the solution to this is to take control of the file processing to ensure we are only sending the file.
Use the method outlined here: https://pqina.nl/filepond/docs/api/server/#process-1 replacing url-to-api with the Direct Creator Upload url we get from Cloudflare's API. If you are using TS note that as of today there is a type mismatch which causes an error. Just remove transfer options from the function params to fix.
More details here: https://github.com/pqina/filepond/issues/818

Related

Linking.openUrl not working with urls containing non english characters

I am using Linking from React-Native. Linking.OpenUrl seems to work with most urls but it does not seem to work with urls which have non-english characters. See the example below in Expo where I have reproduced the case. Note that if you click on the url link directly it will open properly. However, when the same link is being opened via the Linking.OpenUrl it does something to the url and lands in a 404 page.
Here is a repro in Expo:
https://snack.expo.dev/#rezahok/linking-not-working
I am using Expo 42. Any help with this would be really appreciated.
Try with below code
export default class App extends Component {
render() {
const uri = `https://www.prothomalo.com/bangladesh/district/%E0%A6%A6%E0%A6%BF%E0%A6%A8%E0%A6%BE%E0%A6%9C%E0%A6%AA%E0%A7%81%E0%A6%B0%E0%A7%87-%E0%A6%B0%E0%A7%87%E0%A6%B2%E0%A6%95%E0%A7%8D%E0%A6%B0%E0%A6%B8%E0%A6%BF%E0%A6%82%E0%A7%9F%E0%A7%87-%E0%A6%AC%E0%A7%8D%E0%A6%AF%E0%A6%95%E0%A7%8D%E0%A6%A4%E0%A6%BF%E0%A6%97%E0%A6%A4-%E0%A6%97%E0%A6%BE%E0%A7%9C%E0%A6%BF%E0%A6%95%E0%A7%87-%E0%A6%9F%E0%A7%8D%E0%A6%B0%E0%A7%87%E0%A6%A8%E0%A7%87%E0%A6%B0-%E0%A6%A7%E0%A6%BE%E0%A6%95%E0%A7%8D%E0%A6%95%E0%A6%BE-%E0%A6%A8%E0%A6%BF%E0%A6%B9%E0%A6%A4-%E0%A7%A9`
const decodedUri = decodeURIComponent(uri);
return (
<View style={styles.container}>
<Button title="Click me" onPress={ ()=>{ Linking.openURL(decodedUri)}} />
</View>
);
}
}

React Native app with react-hook-form not working (View config getter callback form component 'input' must be a function (received 'undefined')

I'm creating a mobile app with React Native and I need to use a form. I want to use react-hook-form, but I can't get it to work, even in a newly created project. I did the following:
Created new React Native projects: it runs as it should
npm install react-hook-form
copy-paste following code to App.js:
import React from "react";
import { useForm } from "react-hook-form";
export default function App() {
const { register, handleSubmit, watch, formState: { errors } } = useForm();
const onSubmit = data => console.log(data);
console.log(watch("example")); // watch input value by passing the name of it
return (
/* "handleSubmit" will validate your inputs before invoking "onSubmit" */
<form onSubmit={handleSubmit(onSubmit)}>
{/* register your input into the hook by invoking the "register" function */}
<input defaultValue="test" {...register("example")} />
{/* include validation with required or other standard HTML validation rules */}
<input {...register("exampleRequired", { required: true })} />
{/* errors will return when field validation fails */}
{errors.exampleRequired && <span>This field is required</span>}
<input type="submit" />
</form>
);
}
It gives following error: screenshot of emulator
I have no idea what's the problem. Is there something wrong with the installation of react-hook-form as it doesn't recognize the input field?
Thanks!

Why don't WebView script injections work?

this document suggests that I should be able to inject values into a web page displayed by the WebView component such that the value can be used by loaded the page:
https://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md#communicating-between-js-and-native
specifically, the code below shows how to set a value within the window object but does not show how it is used:
import React, { Component } from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';
export default class App extends Component {
render() {
const runFirst = `
window.isNativeApp = true;
true; // note: this is required, or you'll sometimes get silent failures
`;
return (
<View style={{ flex: 1 }}>
<WebView
source={{uri: 'my-url-here'}}
injectedJavaScriptBeforeContentLoaded={runFirst}
/>
</View>
);
}
}
the page I'm loading looks like this:
<html>
<body>
<script>
alert(window.isNativeApp)
</script>
</body>
</html>
which displays undefined. I've also tried:
<html>
<body>
<script>
var fn = function() {
alert(window.isNativeApp)
}
document.addEventListener('DOMContentLoaded', fn, false)
</script>
</body>
</html>
with identical results. given that I'm supposed to be able to send the webpage values, how am I supposed to use them?
from my package.json:
"react-dom": "~16.9.0",
"react-native": "^0.62.1",
"react-native-webview": "^8.2.1",
Appendix I
in fact, the above doesn't seem to run at all. if I try the following:
const runFirst = `
console.log('INJECTION')
alert('INJECTION')
true; // note: this is required, or you'll sometimes get silent failures
`;
I get neither an alert, nor a trace in the log. of course, I'm not sure whether alert() can work before the document is loaded, or whether the log would be visible to me in the regular app's console output
by contrast injectedJavaScript does seem to run, albeit after the document loads, which means that at the time that the <script> in my doc runs, the value hasn't yet been set
for the next poor sod that struggles with this, the library is broken and will (someday) be fixed, but in the meantime, this works:
<WebView
source={{uri: 'my-url-here'}}
injectedJavaScriptBeforeContentLoaded={runFirst}
onMessage={event => { alert(event.nativeEvent.data )}}
/>
the onMessage is intended for communications in the other direction but its mere presence makes the code work

Users get immediatelly redirected to Auth0 Login page instead of landing on the IndexPage

I try to create a personal portfolio site with GatsbyJS and want to protect certain areas of my site with a login, to be precise all pages with path /account should be proteced. Auth0 provides the protection. However, instead of showing the landing page (where I provide some basic information), the users get redirected directly to Auth0's login page.
I followed the tutorial of jlengstorf (https://www.youtube.com/watch?v=j-vuF2PYHmU&index=2&list=PLz8Iz-Fnk_eTpvd49Sa77NiF8Uqq5Iykx and https://github.com/jlengstorf/gatsby-auth0-app) and I'm quite sure, that I implemented it similar to his solution.
import React from "react"
import { Link } from "gatsby"
import ContainerUnauthenticated from "../components/containerUnauthenticated"
const IndexPage = () => {
return (
<div>
<ContainerUnauthenticated>
<p>
Here is some introducing text, just divs and ps, nothing special
</p>
<div style={{ textAlign: 'center' }}>
<Link to="/account/cv" className="waves-effect waves-default" style={{ color: '#039be5' }} >
<i class="fas fa-sign-in-alt" />
Please login here
</Link>
</div>
</ContainerUnauthenticated>
</div >
)
}
export default IndexPage
I also tried /account in Link's to attribute, but this did not work too.
I'd like to show the IndexPage first, hence the user can read the basic information of the landing page and can click on the "Login" button.
this is a problem likely in your react routes or somewhere else as to redirect to the Auth0 Login page you have to purposely redirect to the authorization endpoint. Also this could be the result of your index being protected by triggering a unauthenticated event. I hope this helps you in your quest.
https://auth0.com/docs/quickstart/spa/react

Render HTML in React Native

In my React Native app, I am pulling in JSON data that has raw HTML elements like this: <p>This is some text. Let’s figure out...</p>
I've added the data to a view in my app like this:
<Text>{this.props.content}</Text>
The problem is that the HTML comes out raw, it does not render like it would in a browser. Is there a way to get my JSON data to look like it would in a browser, inside my app view?
Edit Jan 2021: The React Native docs currently recommend React Native WebView:
<WebView
originWhitelist={['*']}
source={{ html: '<p>Here I am</p>' }}
/>
https://github.com/react-native-webview/react-native-webview
If you don't want to embed a WebView, there are also third party libraries to render HTML into native views:
react-native-render-html
react-native-htmlview
Edit March 2017: the html prop has been deprecated. Use source instead:
<WebView source={{html: '<p>Here I am</p>'}} />
https://facebook.github.io/react-native/docs/webview.html#html
Thanks to Justin for pointing this out.
Edit Feb 2017: the PR was accepted a while back, so to render HTML in React Native, simply:
<WebView html={'<p>Here I am</p>'} />
Original Answer:
I don't think this is currently possible. The behavior you're seeing is expected, since the Text component only outputs... well, text. You need another component that outputs HTML - and that's the WebView.
Unfortunately right now there's no way of just directly setting the HTML on this component:
https://github.com/facebook/react-native/issues/506
However I've just created this PR which implements a basic version of this feature so hopefully it'll land in some form soonish.
I found this component. https://github.com/jsdf/react-native-htmlview
This component takes HTML content and renders it as native views, with customisable style and handling of links, etc.
A pure JavaScript react-native component that renders your HTML into 100% native views. It's made to be extremely customizable and easy to use and aims at being able to render anything you throw at it.
react-native-render-html
Using this component will improve your application memory footprint and performance when compared to embedded WebViews.
Install
npm install react-native-render-html --save or yarn add react-native-render-html
Basic usage
import React from "react";
import { ScrollView, useWindowDimensions } from "react-native";
import RenderHTML from "react-native-render-html";
const html = `
<h1>This HTML snippet is now rendered with native components !</h1>
<h2>Enjoy a webview-free and blazing fast application</h2>
<img src="https://i.imgur.com/dHLmxfO.jpg?2" />
<em style="textAlign: center;">Look at how happy this native cat is</em>
`;
export default function App() {
// Allow images to scale to available width
// with contentWidth prop.
const { width } = useWindowDimensions();
return (
<ScrollView style={{ flex: 1 }}>
<RenderHTML contentWidth={width} source={{ html }} />
</ScrollView>
);
}
RenderHTML Props reference
You may customize the style of elements via class names, tags, and you can even register custom renders for tags. More info on the official website.
i uses Js function replace simply.
<Text>{item.excerpt.rendered.replace(/<\/?[^>]+(>|$)/g, "")}</Text>
React Native has updated the WebView component to allow for direct html rendering. Here's an example that works for me
var htmlCode = "<b>I am rendered in a <i>WebView</i></b>";
<WebView
ref={'webview'}
automaticallyAdjustContentInsets={false}
style={styles.webView}
html={htmlCode} />
<WebView ref={'webview'} automaticallyAdjustContentInsets={false} source={require('../Assets/aboutus.html')} />
This worked for me :) I have html text aboutus file.
import HTML from "react-native-render-html";
var htmlCode = "<b>I am <i>Italic</i></b>";
<HTML source={{html: htmlCode}}/>
The WebView component was not rendering for me HTML snippets, like
<b>hello</b>, world!
But if I would enclose the HTML snippet in a document, like the example below, then it did actually render the document:
<View style={styles.accContent}>
<WebView source={{html: `<!DOCTYPE html><html><body style="font-size: 3rem">${data.content}</body></html>`}} />
</View>