Skip to content

Instantly share code, notes, and snippets.

View narennaik's full-sized avatar

Naren Naik narennaik

View GitHub Profile
@narennaik
narennaik / reducer.js
Created August 1, 2021 09:20
reducer
const reducer = (state, action) => {
if (action.type === 'ADD_TODO') {
return { ...state, todo: [...state.todo, action.payload] };
} else {
return state;
}
};
@narennaik
narennaik / dispatch.js
Last active August 1, 2021 09:08
update the state with the change
const state = {};
const subscribe = (listener) => {
listeners.push(listener);
};
const dispatch = (action/change) => {
const newState = getNewState(state, action);
state = newState;
};
@narennaik
narennaik / action.js
Last active August 1, 2021 08:50
What if we want to update/change the state? We would dispatch a change/action
const action = { type: 'ADD_TODO', payload: 'Have a snack' };
@narennaik
narennaik / subscribe.js
Last active August 1, 2021 09:12
How do we know when the state changes? We subscribe!
const state = {};
const listeners = [];
const subscribe = (listener) => {
listeners.push(listener);
};
const dispatch = (action/change) => {
const newState = getNewState(state, action);
state = newState;
@narennaik
narennaik / state.js
Created July 31, 2021 07:57
First we would need state
const state = {};
@narennaik
narennaik / function.js
Last active June 29, 2021 09:02
adding new feature
const REDIRECT_URL = CONFIG.serverRedirectURL;
const isDomainValid = (item) => item.detatils.valid;
const getProtocol = (item) => (isDomainValid(item) ? 'https' : 'http');
const getDomainName = (item) => item.details.name;
const getUrl = (item) => `${getProtocol(item)}://${getDomainName(item)}?}`;
// TODO: (temporary) Adding callback=true because server side redirection needs this parameter
const addCallbackParameter = (url) => `${url}&callback=true`;
@narennaik
narennaik / functions.js
Last active June 28, 2021 19:52
using pipe
const getDomainName = (item) => item.details.name;
const getProtocol = (item) => (item.details.valid ? 'https' : 'http');
const getUrl = (item) => `${getProtocol(item)}://${getDomainName(item)}`;
// TODO: (temporary) Adding callback=true because server side redirection needs this parameter
const addCallbackParameter = (url) => `${url}?callback=true`;
const generateUrl = (item) => {
const url = getUrl(item);
const urlWithCallbackParameter = addCallbackParameter(url);
return urlWithCallbackParameter;
@narennaik
narennaik / functions.js
Last active June 29, 2021 05:40
using .map multiple times
const getDomainName = (item) => item.details.name;
const getProtocol = (item) => (item.details.valid ? 'https' : 'http');
const getUrl = (item) => `${getProtocol(item)}://${getDomainName(item)}`;
// TODO: (temporary) Adding callback=true because server side redirection needs this parameter
const addCallbackParameter = (url) => `${url}?callback=true`;
const urlList = domainList.map(getUrl).map(addCallbackParameter);
@narennaik
narennaik / functions.js
Last active June 28, 2021 19:52
Split into multiple functions
const getDomainName = (item) => item.details.name;
const getProtocol = (item) => (item.details.valid ? 'https' : 'http');
const getUrl = (item) => `${getProtocol(item)}://${getDomainName(item)}`;
// TODO: (temporary) Adding callback=true because server side redirection needs this parameter
const addCallbackParameter = (url) => `${url}?callback=true`;
@narennaik
narennaik / urlListWithIncompleteComments.js
Last active June 28, 2021 19:00
Incomplete comments
// Construct URL list
// Adding https protocol since the backend service doesn't accept http
// TODO: (temporary) Adding callback=true because server side redirection needs this parameter
const urlList = domainList.map((item) => {
const domain = item.details.name;
let url;
if (item.details.valid) {
url = `https://${domain}?callback=true`;
} else {
url = `http://${domain}?callback=true`;