Skip to content

Instantly share code, notes, and snippets.

@mijms
mijms / builder.js
Created July 21, 2021 13:10
Reducer builder function sample
export const buildReducers = (name)=>{
const handlers = {
[`set_main_${name}`]: mainHandlers.set_main,
};
return {
[name]: (state = {}, action)=>( get(handlers, action.type, d => d)(state, action))
}
}
// buildReducer('app_name')
@mijms
mijms / index.js
Created July 21, 2021 13:22
apply filter
//// exporting your index applying function
import * as Filters from './main';
export default function applyFilters(params={}, data, state=store.getState(), props={}){
const action = get(Filters, params.key, (p, d)=>d);
data = action(params, data, state, {applyFilters: applyFilters, ...props});
if(params.then){
data = applyFilters(params.then, data, state, props);
}
return data;
import {get} from 'lodash'
let x = {a: 1, b:2}
// a and b is conditional key like if key == a
let hope = get(x, 'kkk', 'not exist');
// magic is just happen not exist is appear welcome lodash
import {get, find} from lodash;
let object_data = {
"cat1": {
"video1": {
"active": true
}
}
}
let arrD = [{
"cat": "cat1",
export default class Authorized extends Component {
handleClick = () => {
const { onClick } = this.props
const authorized = checkAuthorization()
if (authorized) {
// console.log('authorixes')
onClick(d)
}
// show Not Authorize Popup
}
export default function LayoutMain(WrappedComponent) {
class FormComponent extends Component {
renderControls() {
return <p>hi</p>
}
render() {
return (
<WrappedComponent
{...this.props}
renderControls={this.renderControls}
import React from 'react'
import {useFormikContext} from 'formik'
import {pick} from 'lodash';
export default (data)=>{
return function FormVals(MainComponent) {
const FormComp = (props) => {
const mainProps = pick(useFormikContext(), data.pick || ['values']);
return <MainComponent {...{...props, ...mainProps}} />
};
return FormComp
import * as Validations from 'validation Module'
validationUpdate = ()=>{
// select validation function by key
this.validations = map(this.props.validates, (d, key) => (get(Validations, key, false)(d))).filter(Boolean)
}
validation = (value) => {
// try to validate one by one
const valid = this.validations.reduce((res, fun) => (res || fun(value, {parentValues})), undefined)
return valid;
}
import React, { Component } from 'react'
import { Input } from 'antd'
class text extends Component {
change = (ev) => {
const { field} = this.props
field.onChange({ target: { name: field.name, value: ev.target.value } });
// extra code after saving
}
render() {
const { field={}, placeholder, field_type='text'} = this.props
import * from './inputs'
class FormField extends Component {
shouldComponentUpdate(nextProps, nextState) {
// dependancy code for performance
}
render() {
const MainComponent = get(inputs, type, label);
return <>
<Label />
<FieldComponent {...this.props}>