Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
DominikStyp / App.tsx
Created December 12, 2022 16:31
React + TypeScript generic components
// App.tsx
// usage example
import { useState } from 'react';
import { TabBar } from '../../../shared/components/TabBar';
interface MySocial {
id: number;
name: string;
link: string;
}
@DominikStyp
DominikStyp / useFetch.js
Last active October 26, 2022 20:59
React: useFetch() helper, abort fetch() using AbortController
import { useEffect, useState } from "react";
expoprt function useFetch(url) {
const [loading, setLoading] = useState(true);
const [data, setData] = useState(null);
const [error, setError] = useState(null);
useEffect(() => {
controller = new AbortController();
@DominikStyp
DominikStyp / CanAssertJsonPaths.php
Created October 25, 2022 18:28
PHPUnit: Asserting JSON Paths
<?php
namespace Tests\Http;
/**
*
* Use this trait in your Laravel/Lumen TestCase class
* to get the assertJSON... functionality out of the box
*
* Trait CanAssertJsonPaths
@DominikStyp
DominikStyp / nominalTyping.ts
Created October 15, 2022 21:10
TypeScript: Nominal Typing using symbols
declare const Nominal: unique symbol;
type NominalType<TType, TNominal extends string> = TType & {
[Nominal]: TNominal;
};
type UserId = NominalType<string, 'user_id'>;
type BookId = NominalType<string, 'book_id'>;
@DominikStyp
DominikStyp / migration_file.php
Created September 30, 2022 22:00
Laravel: virtualAs(), virtual column extracted from JSON field
<?php
class SomeMigration {
public function up() {
Schema::table('json_data', function(Blueprint $table) {
$table->string('email', 100)
// here we deine the virtual column for the json_data table which will be just a column view
->virtualAs("json_unquote(json_extract(json, '$.email'))");
@DominikStyp
DominikStyp / invoke_all_backups.bat
Last active September 25, 2022 22:39
Batch (Windows): make directories backups with robocopy
REM cmd /c invokes command in CMD
REM cd /d J:\VIDEOS\ goes to the directory where the backup script lives
REM make-backup.bat invokes the backuping script in that directory
REM make backup for VIDEOS at disk J
cmd /c "cd /d J:\VIDEOS\ && dir && make-backup.bat"
REM make backup for VIDEOS at disk K
cmd /c "cd /d K:\VIDEOS\ && dir && make-backup.bat"
@DominikStyp
DominikStyp / SomeTest.php
Last active September 8, 2022 09:45
Laravel testing using built-in Validator
<?php
use Tests\TestCase;
use App\Rules\BSNRule;
class SomeTest extends TestCase
{
public function testMe()
{
$data = [
@DominikStyp
DominikStyp / example.php
Last active June 13, 2022 18:47
PHPStorm + Deep Assoc Complete
<?php
class MyRepository {
/**
*
*
* @param $filter = [
* "id" => "uuid",
* "version" => "2022-02-03 12:11:00",
* "name" => "some document name",
* "access_level" => [
@DominikStyp
DominikStyp / README.md
Last active June 10, 2022 10:47
React + TS: How to introduce TypeScript to old React project

Steps

  • add tsconfig.json and remove jsconfig.json
  • change index.js to index.tsx
  • install dependencies: yarn add typescript @types/node @types/react @types/react-dom @types/jest --dev
  • make test component like Test.tsx and check if typescript works there

Troubleshooting

Problem with relative paths

If your project uses relative-to-root instead of relative-to-current-file paths like below:

@DominikStyp
DominikStyp / complex_like_expression.sql
Last active May 27, 2022 12:32
SQL & MySQL: Using local variables in complex queries
set @s1 = '%123aaabbbcccdddeeefffggg%';
select * from `items` where
(
`title` LIKE @s1 or `apn` LIKE @s1 or `slug` LIKE @s1
or exists (select * from `sellers` where `items`.`seller_id` = `sellers`.`id`
and (`title` LIKE @s1 or `slug` LIKE @s1 or `address` LIKE @s1)
)
or exists (select * from `item_geos`
where `items`.`id` = `item_geos`.`item_id`