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)
})
@rockymeza
rockymeza / django-strorages.json
Created August 5, 2013 16:00
AWS IAM Policy for S3 for django-storages
{
   "Statement":[
      {
         "Effect":"Allow",
       
         "Action":[
            "s3:ListAllMyBuckets"
         ],
         "Resource":"arn:aws:s3:::*"
      },
@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 / 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 / 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,
};
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 / 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 / dedent.py
Created July 24, 2013 17:42
Django template tag that dedents all lines.
import re
from django import template
register = template.Library()
leading_whitespace_re = re.compile(r'^\s+', flags=re.MULTILINE)
@register.tag(name='dedent')
def do_dedent(parser, token):
@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 / settings_live.py
Created August 26, 2013 05:08
A self-contained Django settings include for using S3 and Django Compressor. It depends on django-herokuify and expects three environment variables to be present: `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_BUCKET_NAME`.
# Example Usage
from .settings_s3 import *