Skip to content

Instantly share code, notes, and snippets.

View james2doyle's full-sized avatar

James Doyle james2doyle

View GitHub Profile
@james2doyle
james2doyle / axios.md.blade.php
Last active November 23, 2021 18:27
A Scribe partial for creating example requests using Axios. Place in resources/views/vendor/scribe/partials/example-requests/axios.md.blade.php
{{--
File: resources/views/vendor/scribe/partials/example-requests/axios.md.blade.php
--}}
@php
use Knuckles\Scribe\Tools\WritingUtils as u;
/** @var Knuckles\Camel\Output\OutputEndpointData $endpoint */
@endphp
```javascript
const instance = axios.create({
baseURL: '{{ rtrim($baseUrl, '/') }}',
@james2doyle
james2doyle / HasSlug.php
Last active December 16, 2021 16:56
A trait for Laravel (v8) that uses model events to set a slug field based on another field
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
/**
* Handles the logic for settings slugs
*/
@james2doyle
james2doyle / role-based-access-control.md
Created July 13, 2021 23:46
Authentication is the act of validating that users are who they claim to be. Authorization is the process of giving the user permission to access a specific resource or function. Taken from Redwoodjs Role Based Access Control

Authentication vs Authorization

Taken from Redwoodjs Role Based Access Control

How is Authorization different from Authentication?

Authentication is the act of validating that users are who they claim to be. Authorization is the process of giving the user permission to access a specific resource or function.

In even more simpler terms authentication is the process of verifying oneself, while authorization is the process of verifying what you have access to.

@james2doyle
james2doyle / redux-devtools-helper.ts
Created June 4, 2021 17:56
A simple interface to work with the redux devtools methods. Useful for observable objects of just tracing object state
const noop = () => ({});
const fakeRedux = {
init: noop,
subscribe: noop,
send: noop
};
const reduxPlaceholder = { connect: () => fakeRedux };
@james2doyle
james2doyle / detect-prefers-color-scheme.js
Created April 8, 2021 14:17
An example of how to detect dark theme preferences (prefers-color-scheme) using window.matchMedia
(function() {
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
const setting = localStorage.getItem('color-schema') || 'auto';
if (setting === 'dark' || (prefersDark && setting !== 'light')) {
document.documentElement.classList.toggle('dark', true);
}
})();
@james2doyle
james2doyle / fix-window-call-dynamic-component.vue
Created April 3, 2021 19:04
Fix issue with external library that calls window in Nuxt/Vue when being server rendered by dynamically swapping it in the client
<template>
<component :is="component" />
</template>
<script>
export default {
name: 'DynamicComponentForBrowser',
data() {
return {
component: 'div',
@james2doyle
james2doyle / Dockerfile
Created March 30, 2021 20:58
A dockerfile for python 3 projects that use pip and python
FROM python:3.8
# Setup environment
RUN cp /usr/local/bin/pip3.8 /usr/local/bin/pip3 # reenable pip3
RUN pip3 install --upgrade pip
WORKDIR /usr/src/app
# Install requirements
COPY requirements.txt ./
RUN pip3 install --no-cache-dir -r requirements.txt
@james2doyle
james2doyle / nuxt.sublime-project
Last active May 29, 2024 20:46
A starting configuration file for Sublime Text Nuxt Vue projects
{
"folders": [
{
"file_exclude_patterns": [
".eslintcache",
".gitkeep",
"*.min.*",
"*.snap",
"*.pem",
"*lock*"
@james2doyle
james2doyle / debounce.ts
Created March 29, 2021 16:35
Debounce creates and returns a new version of the passed function that will run only after the timeout has elapsed
/**
* Debounce creates and returns a new version of the passed function
* that will run only after the timeout has elapsed
*/
function debounce(fn: Function, wait: number = 0) {
let timeout: ReturnType<typeof setTimeout> | null = null;
return (...args: any) => {
if (timeout) {
clearTimeout(timeout);
@james2doyle
james2doyle / throttle.ts
Created March 29, 2021 16:29
Throttle will make sure a function is only called once within the given amount of time
/**
* Throttle will make sure a function is only called once within the given amount of time
* must use function to keep `this` context
*/
function throttle(fn: Function, threshold: number = 250) {
// check function
if (typeof fn !== 'function') {
throw new TypeError('You must provide a function as the first argument for throttle.');
}