Skip to content

Instantly share code, notes, and snippets.

@rockymeza
rockymeza / FatalErrorBoundary.js
Last active June 11, 2020 13:02
Setting up Sentry in RedwoodJS
// web/src/components/FatalErrorBoundary/FatalErrorBoundary.js
import { FatalErrorBoundary as FatalErrorBoundaryBase } from '@redwoodjs/web'
import * as Sentry from '@sentry/browser'
class FatalErrorBoundary extends FatalErrorBoundaryBase {
componentDidCatch(error, errorInfo) {
Sentry.withScope((scope) => {
scope.setExtras(errorInfo)
Sentry.captureException(error)
})
export const withTime = WrappedComponent => props => (
<GetTime>
{({ currentTime }) => (
<WrappedComponent
// by passing currentTime first and ...props second, we allow
// parent components to override the currentTime
currentTime={currentTime}
{...props}
/>
)}
@rockymeza
rockymeza / TimeProvider.js
Last active April 9, 2018 15:51
Basic TimeProvider implementation
// We need an impure function that will give us the current time.
// Notice that we return the ISO string version of the date. This is nice
// because it is not a mutable object like `Date` and it is also a
// human readable representation.
const defaultGetTime = () => new Date().toISOString();
export default class TimeProvider extends React.PureComponent {
static defaultProps = {
interval: 500,
};
@rockymeza
rockymeza / App.js
Last active April 9, 2018 15:52
Using TimeProvider with WeekendStatus
const App = () => (
<TimeProvider>
<h1>My App</h1>
<GetTime>
{({ currentTime }) => <WeekendStatus currentTime={currentTime} />}
</GetTime>
</TimeProvider>
);
@rockymeza
rockymeza / WeekendStatus.js
Created April 9, 2018 15:17
Pure implementation of WeekendStatus
import * as React from 'react';
const WeekendStatus = ({ currentTime }) => {
// Because we take currentTime as a prop, we now no longer depend
// on the external system time in our render function. We are now
// a pure function
const dayOfWeek = new Date(currentTime).getDay();
const isWeekend = [0, 6].includes(dayOfWeek);
if (isWeekend) {
return <p>It&apos;s the weekend!</p>;
@rockymeza
rockymeza / WeekendStatus.impure.js
Last active April 9, 2018 22:38
Impure implementation of WeekendStatus
import * as React from 'react';
const WeekendStatus = () => {
const dayOfWeek = new Date().getDay();
const isWeekend = [0, 6].includes(dayOfWeek);
if (isWeekend) {
return <p>It&apos;s the weekend!</p>;
}
return <p>It is not the weekend :(</p>;
};
@rockymeza
rockymeza / comparablemodel.py
Created March 15, 2016 10:55
Make Django models comparable based on the Meta.ordering
class ComparableModel(object):
"""
Make models comparable based on the Meta.ordering. Usage:
>>> import datetime
>>> class MyModel(ComparableModel, models.Model):
>>> name = models.CharField(max_length=255)
>>> birthdate = models.DateField()
>>> class Meta:
>>> app_label = 'test'
@rockymeza
rockymeza / makeDecorator.js
Created May 13, 2015 16:00
A function decorator to make function decorators
// Based on idea from @nolsto
function makeDecorator(decorator) {
return function(target, key, descriptor) {
if (typeof descriptor !== 'undefined') {
descriptor.value = decorator(descriptor.value);
return descriptor
} else {
return decorator(target);
}
@rockymeza
rockymeza / keybase.md
Created January 22, 2015 12:08
Keybase Proof

Keybase proof

I hereby claim:

  • I am rockymeza on github.
  • I am rockymeza (https://keybase.io/rockymeza) on keybase.
  • I have a public key whose fingerprint is 88B0 84DA 0420 C198 E6AA C889 37C5 522F 1DED 379A

To claim this, I am signing this object:

@rockymeza
rockymeza / uwsgi_params
Created February 27, 2014 09:07
nginx + Django configs
# /etc/nginx/uwsgi_params
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;