Skip to content

Instantly share code, notes, and snippets.

View martin056's full-sized avatar

Martin Angelov martin056

View GitHub Profile
@martin056
martin056 / WithDialogActions.tsx
Last active April 15, 2020 11:33
HOC that uses Redux connect and React-router withRouter
import React from 'react';
import _ from 'lodash';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { connect } from 'react-redux';
import { Diff } from 'utility-types';
import { AppState } from 'globalReducer';
import {
openDialog,
closeDialog,
@martin056
martin056 / multple_return_values_mock.py
Last active August 13, 2019 17:06
This is an example of a way to have a unittest mock that returns different values each time it's called
# in example.py
def inner_func(x=None):
if x is None:
return 1
return x + 1
def big_func():
@martin056
martin056 / poc_prepagination.py
Created June 7, 2019 13:47
Main Idea - DECOUPLE serialization & pagination
class LimitOffsetPrepagination(LimitOffsetPagination):
def __init__(self, request):
self.request = request
def paginate_queryset(self, queryset, postpagination=None):
self.results = super().paginate_queryset(queryset=queryset, request=self.request)
if postpagination:
self.results = postpagination(self.results)
@martin056
martin056 / form_soup_to_dict.py
Created June 4, 2018 07:57
Transforms form data with initial values to a dictionary that can be used for POST request. Handles Django admin forms.
def form_soup_to_dict(form_soup, add_save=False):
"""
Transforms form data with initial values to a dictionary that can be used for POST request.
* `add_save` - In Django Admin forms POST data expects `_save` to make the request.
"""
data = {}
for input_field in form_soup.find_all('input'):
# We have some custom buttons added to the Django Admin
@martin056
martin056 / drf_parsers_and_renderers.md
Last active November 28, 2019 09:27
How to deal with cases mismatch between Django and React

Introduction

As you may know from my other blog posts I am mainly working with Django and React at the moment. In this article we are going to take a look on how to solve one bothering problem - the cases mismatch.

The picture

  • We have working frontend that uses only React
  • We are migrating to full Single Page Application (SPA) now
  • We maintain the APIs of the app's mobile application
@martin056
martin056 / deep_transform.py
Created May 28, 2018 18:12
Deep snake_case and camelCase transformations
import re
from typing import Dict, Callable, Optional
def to_camel_case(snake_case_str: str) -> str:
"""
Transforms snake_case to camelCase
"""
components = snake_case_str.split('_')
titled_components = ''.join(x.title() for x in components[1:])
@martin056
martin056 / loginFlow.md
Last active February 1, 2018 16:00
Implementation of login flow using redux-saga.
function* authenticate(credentials) {
  try {
    const {data} = yield call(requestLogin, credentials);

    storeAuthToken(data.token);

    yield* [
      put(loginSuccess()),
      put(storeMe(data.me)),
@martin056
martin056 / create_container.sh
Last active August 28, 2017 14:25
Bash script that generates containers for Redux apps. The directories it uses are not configurable at the moment - if you have user `create-react-app` it will work for you.
#!/bin/bash
# USAGE (cd utility/ first!): ./create_container.sh -n <CONTAINER_NAME> -a <ACTION_1> (-a <ACTION_2>...)
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
SRC_PATH=../src
CONTAINERS_PATH=${SRC_PATH}/containers
@martin056
martin056 / split_webpack.md
Last active December 11, 2017 08:39
How to split your `webpack.config.js`

Introduction

In my previous article I promised you to write about how we split our webpack configurations for production and for development. This post is a continuation to the previous one and I will use the webpack.config.js file from there. You had better check it out if you haven't yet!

Why do we need to separate the webpack configuration?

Well, like the most things in programming you may want to use a different configuration for your production files and for development. If you think your webpack.config.js is good enough for both cases then this article is not for you.

If you are from the ones that want to have different configurations - like minifying your bundle file only for production and stuff like that - then let's dive in!

@martin056
martin056 / first_webpack.md
Last active December 12, 2017 08:04
Recently we've started a new SPA project. In this gist I will explain the main struggles and knowledge I take during creating the webpack configuration.

Introduction

Recently we've started a new SPA (single-page application) project with Redux, React and React Router (v4).

Until now I've been working mainly on the backend part of apps so this was a new challenge. In this blog post I will share my knowledge with you.

TL;DR You can check this demo project that uses the Webpack configuration that we are going to achieve.

What is this article about?