Skip to content

Instantly share code, notes, and snippets.

View sht5's full-sized avatar

Shahar Taite sht5

  • 3DSignals
View GitHub Profile
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using softwareBlogREST.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
@sht5
sht5 / gist:71dba08455f228a1d204
Created August 3, 2015 15:47
MongoDB static class for DB
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace softwareBlogREST.Controllers
{
public static class MongoDatabase
{
@sht5
sht5 / new_promise.js
Created April 30, 2017 20:49
new promise wrapping example
const methodReturningPromise = () => {
return new Promise((resolve, reject) => {
asyncMethodToFetchData(params, (err,result) => {
if(err){
reject(err); // return rejected promise to method caller
};
else{
resolve(result); // return fulfilled promise to method caller
}
});
/**
* Created by shahartaite on 20/11/2016.
*/
import React, {Component} from 'react';
import styles from './time_selection_button_bar.css';
import {Translate} from 'react-redux-i18n';
import CustomTimeButton from 'js/components/custom_time_button/custom_time_button';
import consts from 'js/consts';
import classNames from 'classnames';
@sht5
sht5 / utils.js
Last active June 28, 2017 18:35
logging utility functions
const config = require('config'); // library for code configuration setups
const logHttpRequestError = (request, errorObj) => {
const logObj = {
server_type : config.get('server_type'), // we take the server type from configuration
//in order to know which server created the log
//such as production\staging etc
message : errorObj.message,
stack : errorObj.stack,
url : request.url,
@sht5
sht5 / users_routes.js
Created June 28, 2017 18:47
example of ExpressJS endpoint logging an http error
app.post('/user/defaultLanguage', (req, res) => {
const defaultLanguage = req.body.defaultLanguage;
const userEmail = req.user.email;
userLogic.updateUserDefaultLang(userEmail, defaultLanguage)
.then(() => {
res.end();
})
.catch((err) => {
utils.logHttpRequestError(res.req, err);
res.status(500).end();
export function getCoins(name,amountOfCoins){
return {
type : types.GET_COINS,
async : true,
httpMethodToInvoke : httpService.getCoins,
params : [name,amountOfCoins]
}
}
@sht5
sht5 / gist:e7bb8539326f872b22e2dee4f537efc2
Created January 19, 2018 21:12
asyncActionsMiddleware
const asyncActionsMiddleware = store => next => action => {
const isActionAsync = action.hasOwnProperty('async');
if (!isActionAsync) {
return next(action);
}
else {
const {httpMethodToInvoke, params, type} = action;
const inProgressType = generateInProgressActionTypeName(type);
//the resolved promise here is to make sure the action fired here comes after firing original action for example:
//getData => getDataInProgress and not the other way round. hack suggested in redux forums.
@sht5
sht5 / gist:ba622250317718cfc6cec2f8cca9d12f
Created January 19, 2018 21:23
using generateSuccessActionTypeName in reducer
switch (action.type) {
case generateSuccessActionTypeName(types.GET_COINS): {
const {coins} = action;
const currentAmountOfCoins = state[consts.STATE_INNER_OBJECT_NAMES.COINS];
return {
...state,
[consts.STATE_INNER_OBJECT_NAMES.COINS]: currentAmountOfCoins + coins
}
}
@sht5
sht5 / gist:229aea45a4f146e86244171e4b54c924
Last active March 5, 2018 09:47
repetitive async actions
const requestToGetCoins = async() => {
return (dispatch) => {
dispatch(requestToGetCoinsInProgress());
try{
const users = await httpService.getCoins();
dispatch(requestToGetCoinsSuccess(users));
}
catch(e){
dispatch(requestToGetCoinsError(e));
}