AngularFire2 vs Firebase JS - angularjs

I'm finding the AngularFire2 library to be poorly documented and very hard to use. I have an angular2 app and was wondering if someone could help me clear up the pros/cons of just using the vanilla JS Firebase code rather than angularfire2? Does using the vanilla JS version kill off angular2 functionality that I may be using? I'm confused as to why use one over the other, personally the vanilla JS one is waaaay better documented and feature rich, I can't even see how to Signup Users in AngularFire2, it doesn't have any UI elements and observables are doing my head in!

You can use vanilla version directly. Even with observables - observable and promises work well together. For example:
Observable.of(new firebase.auth.GoogleAuthProvider())
.switchMap(o => firebase.app().auth().signInWithPopup(o))
.subscribe()
Main advantage of AngularFire2 are head-hurting observables (; I suggest you learn rxjs and start using them over promises. I am using vanilla JS SDK, however, I've created refObservable based on what AngularFire2 does with FirebaseObservables. It's simpler, but enough for my needs:
export function refObservable(ref): Observable<firebase.database.DataSnapshot> {
return Observable.create(observer => {
let fn;
try {
fn = ref.on('value', snapshot => {
observer.next(unwrapSnapshot(snapshot));
});
} catch (error) {
observer.error(error);
}
return () => ref.off('value', fn);
});
};
where unwrapSnapshot(snapshot) is simple function that checks snapshot and returns appropriate result (array, object, string, etc...). I use it for reading data from firebase. Create/Update/Delete operations I do directly:
Observable.of(checkUserPermission())
.switchMap(() => {
return firebase.database().ref('what/ever').remove();
});
It's similar with other modules - storage, messaging, auth... I prefer Firebase SDK over AngularFire2. I also find it easier to use in service/web workers.
If you already know vanilla sdk, stick with it.
If you only need database read/write and basic authentication use AngularFire2 it's simpler.
If you need more control over firebase features use JS SDK.

Related

React.js: Best practices for API's grouping

I have been using Angular for 3 years. I have created some small and big projects. In every project, I have used proper folder structure for API calls(which many resources suggest). Below is a simple example
constants.ts
export const uploadData = `${url}/uploadData`;
service.ts
uploadData(formData: FormData): Observable<any> {
return this.http.post<any>(constants.uploadData, formData);
}
And in the component file, I import the service and call the API like this
this.service.uploadData(formData).pipe(take(1)).subscribe((res) => {
// do something
});
I specify all my related APIs and services in respective folders and call them wherever I need them. It's been 5 months since I started learning React and ReactNative. Now I am comfortable with both I started doing a major project. Now, the resources I followed to learn React didn't follow the proper structure(Create the API in whichever folder is required and call it with fetch/axios). Found some resources that follow structure but not effectively like how I did it in Angular. I can't find any resources with the requirement. It would be helpful if I can get one example of how we can have the folder structure like in Angular so that I will follow the good approach, instead of ending up writing bad code. (I am using Axios)

How to create a Google web app using Google Apps Script, React, and MobX

I found react-google-apps-script project on GitHub, and it seems to be able to make my dreams of a React-based Google Apps Script project a reality. For a while, since I learned React (and MobX), I've been wanting to use it inside Google Apps Script projects!
Per the documentation, however, it seems to only work on dialog windows... :'(
That being said, I have a freelancing client who is looking for a UI that can interact with her Google Sheets, to which the most reasonable approach would be to create a web app. I've created Google Apps Script based web apps in the past, in fact it was my very first exposure to it. It seems like they only support raw HTML templates and jQuery .
However, knowing all that I know now, I'd like to create a simple React + MobX MVVM-based web app on the Google Apps Script. How can we use this project to get that done?
UPDATE: I should have read more carefully! This is embarassing!!
From looking at src/server/ui.js, I see that is how the main use case, injecting React project into the modals in Google Apps Script projects that control Google Spreadsheets, even gets to happen.
Here is the code from that file:
export const onOpen = () => {
const menu = SpreadsheetApp.getUi()
.createMenu('My Sample React Project') // edit me!
.addItem('Sheet Editor', 'openDialog')
.addItem('Sheet Editor (Bootstrap)', 'openDialogBootstrap')
.addItem('About me', 'openAboutSidebar');
menu.addToUi();
};
export const openDialog = () => {
const html = HtmlService.createHtmlOutputFromFile('dialog-demo')
.setWidth(600)
.setHeight(600);
SpreadsheetApp.getUi().showModalDialog(html, 'Sheet Editor');
};
export const openDialogBootstrap = () => {
const html = HtmlService.createHtmlOutputFromFile('dialog-demo-bootstrap')
.setWidth(600)
.setHeight(600);
SpreadsheetApp.getUi().showModalDialog(html, 'Sheet Editor (Bootstrap)');
};
export const openAboutSidebar = () => {
const html = HtmlService.createHtmlOutputFromFile('sidebar-about-page');
SpreadsheetApp.getUi().showSidebar(html);
};
It is worth noting that this will transpile to the back-end Google Apps Script code.
This means that we could customize this and make this more general, by editing out the onOpen method. I see it is developer's responsibility to do that :P
Great project! It's easy to follow, and I can see how I can make this my own, with MobX and MVVM!

cleaning up back-end data in Salesforce between Cypress tests using JSforce

I'm using the Cypress testing framework to test a web application which has a Salesforce back-end.
I'm looking for a way to ensure that all data is cleaned up (deleted) from the Salesforce back-end after (or before) each test is run. My purpose is to make sure that all tests can be run independently and in any order, and to make sure that the back-end data gets cleaned up (deleted) even in the event of a failing test.
Unless you have any suggestions on a better way of implementing this, I'm thinking that the best approach is to use JSforce to perform the data clean up from within the Cypress test file.
You can use cy.task for this:
cypress/support/index.js:
beforeEach(() => {
cy.task('cleanUp');
});
cypress/plugins/index.js:
module.exports = ( on ) => {
on('task', {
cleanUp () {
// do your thing with your back-end
}
});
};
For more, see my previous answer.

Microfrontends React/Component based splitting

Background:
I am confronted with the task to modernize a tool and convert it to microservices with a react frontend. My idea was to have a general top-level component containing e.g. the Nav and a component for each microservice that contains the functionality.
Approaches:
bundle the components locally so that it becomes effectively a monolithic frontend and the the forntend code is seperated just in the repo.
I think that would give up on the advantage of not having to redeploy your entire app for every change
lazy-load a minified bundle of each of the components from the microservice by defining them in a config file
With this approach I could just webpack each component on their own and async import it from the Main Page but maybe there would be a huge overhead
I read about component based splitting with react-loadable and bundling react-router or webpack, but I can't find information on how to load small bundles form different URL-Endpoints.
Question:
Is it possible to bundle react components on their own and import them from different Resource-URL's and how would one approach that ?(Or is React even suitable for that)
So after quite some search and experiments I found the Project Mosaic of Zalando, which is what I wanted. It offers a great way of React Component Splitting to support Micro Frontends. Note however that it is probably not suitable for smaller projects as it takes some time to get into the material and setting up all necessary applications.
Take a look at the below link:
https://www.martinfowler.com/articles/micro-frontends.html
I've recently made a project based on that article and I think that it might be what You are looking for.
I made the wrapper app which is rendering the microfrontends dynamically on the runtime based on the URL and routings. In the article above, there is a demo app showing the idea.
Every microfrontend is the separate project with it's own code repo.
https://demo.microfrontends.com/
Each app is hosted and js chunks are getting loaded on the runtime. This code might make it a little bit more clear what's going on there.
componentDidMount() {
const { name, host } = this.props;
const scriptId =micro-frontend-script-${name}`;
if (document.getElementById(scriptId)) {
this.renderMicroFrontend();
return;
}
fetch(`${host}/asset-manifest.json`)
.then(res => res.json())
.then(manifest => {
const script = document.createElement('script');
script.id = scriptId;
script.src = `${host}${manifest['main.js']}`;
script.onload = this.renderMicroFrontend;
document.head.appendChild(script);
});
}`
I hope You'll find that helpful :)
Good luck!

easy pass data from express to react

So I'm trying to learn ReactJS but the documentation is terrible, after reading it I am even more confused than before.
I will keep it simple for the sake of learning:
This is my old approach:
Current:
app.use(function (req, res, next) {
res.locals.user = req.user || null;
next();
});
So I get it with {user} in handlebars.
Now for react:
{this.props.user}
returns NULL. Seriously, what the ...? How to pass the variable easily?
ReactJS is mostly used for single page applications, meaning that most data is fetched after the application is loaded. If you want to use ReactJS as a template engine with expressJS use express-react-views. Otherwise you need to prerender ReactJS with react-prerender when using NodeJS.
But I guess what you really want to do is to read/watch a good ReactJS tutorial.
Here are some resources for you:
Learn React & Redux
Simple React Development in 2017
Hope that brings you on track :)

Resources