Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
DominikStyp / use-http-mock.js
Created December 24, 2021 11:36
React custom hook: useHttpMock() which mocks the HTTP request and returns the data from the JSON file instead of endpoint
import { useState, useCallback } from 'react';
import mockTasks from '../mocks/tasks.json';
const useHttpMock = () => {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const sendRequest = useCallback(async (requestConfig, applyData) => {
setIsLoading(true);
setError(null);
@DominikStyp
DominikStyp / Something.php
Last active January 5, 2022 10:49
Laravel 8: Database JSON fields (migration, casts, feature tests, update JSON attributes in DB)
<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Something extends Model {
// if JSON field contains object use 'object' type
@DominikStyp
DominikStyp / MyComponent.jsx
Created January 16, 2022 11:58
React: custom redux-like store
import { useStore } from './hooks-store/store';
const MyComponent = () => {
const dispatch = useStore(false)[1];
const updateCounter = () => {
dispatch('COUNTER');
};
@DominikStyp
DominikStyp / my-logo.blade.php
Created January 26, 2022 13:53
Laravel 7: Embedding images in emails (wors for Gmail too)
<img
src="{{ $message->embed( public_path('images/my-logo.png') ) }}"
alt="{{ env('APP_FRONT_URL') }}" />
@DominikStyp
DominikStyp / MyForm.test.jsx
Last active February 4, 2022 13:46
React + Jest + Enzyme: testing dynamic w values toMatchSnapshot, on example with 'react-draft-wysiwyg'
import { shallow } from 'enzyme';
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { Editor } from 'react-draft-wysiwyg';
import MyForm from '../MyForm';
import renderer from 'react-test-renderer';
const withRouter = (component: React.ReactElement) => {
return <BrowserRouter>{component}</BrowserRouter>;
};
@DominikStyp
DominikStyp / EditorState.js
Last active February 11, 2022 12:17
React + WYSIWYG: wrapper to 'draft-js' + JEST test
import { ContentState, convertFromHTML, convertFromRaw, convertToRaw, EditorState as DraftEditorState } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
class EditorState {
constructor(state: DraftEditorState = null) {
this.use(state);
}
static loadFromHtml(htmlContent: String): DraftEditorState {
const blocks = convertFromHTML(htmlContent);
@DominikStyp
DominikStyp / Readme.md
Last active February 16, 2022 17:29
PHPStorm: How to find usages of the magic method in the project with Laravel example

How it works?

  1. You make a new instance of the Repository class
  2. You use reflection to invoke the getUser method on the $repository object
  3. You now try to find in PHPStorm (or other IDE which supports it) the usages of the method getUser in your project (Alt + F7)

IDE can't find it until you put the @uses \App\Repository::getUser() in the comments for method which uses it.

@DominikStyp
DominikStyp / docker-compose.yml
Created March 11, 2022 09:38
Docker Compose: Node16 container for React Frontend
version: '3.4'
services:
nodejs:
image: node:16
environment:
- NODE_ENV=dev
user: "node"
working_dir: /home/node/app
volumes:
- ../:/home/node/app
@DominikStyp
DominikStyp / .gitignore
Last active March 25, 2022 13:22
Cypress: example tests
# Dependency directories
node_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
@DominikStyp
DominikStyp / test_routes.php
Last active March 30, 2022 13:28
Laravel: Test if request URI matches the route name (Unit Test)
<?php
namespace Tests\Feature\Controllers;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Request;
use Faker\Factory;
class SomeTestClass {
/**