Skip to content

Instantly share code, notes, and snippets.

View ryan2clw's full-sized avatar

Ryan Dines ryan2clw

  • Tampa, FL
View GitHub Profile
@ryan2clw
ryan2clw / cards.tsx
Last active August 24, 2019 22:30
Card action and reducer
import { actionTypes } from './actionTypes';
import { action as act } from 'typesafe-actions'
import { getCards } from '../webservices/bingoService';
import { Reducer, AnyAction } from 'redux'
import { Dispatch } from 'react';
import { danger, clear } from './messages';
/* Card Interface */
export interface ICardArray {
@ryan2clw
ryan2clw / models.py
Created August 17, 2019 12:33
Model for card data
""" Generates SQL tables for Cards and Rows """
from django.db import models
class Card(models.Model):
""" A bingo card with 5 rows. """
created_on = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "Card: " + str(self.id) + ", " + str(self.created_on)
@ryan2clw
ryan2clw / serializers.py
Last active August 25, 2019 01:14
Serialize card and row data
"""
Form the JSON that we need
"""
from random import sample
from rest_framework import serializers
from play.models import Row, Card
def card_data(card):
""" populates card with 5 rows of random data """
b_column = sample(range(1, 16), k=5)
@ryan2clw
ryan2clw / views.py
Created August 17, 2019 12:56
Endpoints for creating and requesting cards
""" These are endpoints that get or create cards and rows """
from rest_framework import generics
from play.models import Card
from play.serializers import CardSerializer
class CardList(generics.ListCreateAPIView):
""" Get or create cards """
queryset = Card.objects.all()
serializer_class = CardSerializer
@ryan2clw
ryan2clw / configureStore.tsx
Last active August 23, 2019 18:02
Python Bingo sample code
import { createStore, compose, applyMiddleware, combineReducers, Dispatch, Action, AnyAction } from 'redux'
import { createLogger } from 'redux-logger';
import thunk from "redux-thunk";
import { messageReducer, IMessageState } from './messages';
export interface IConnectedReduxProps<A extends Action = AnyAction> {
dispatch: Dispatch<A>
}
export interface IApplicationState {
@ryan2clw
ryan2clw / messages.tsx
Last active August 23, 2019 18:15
Action types for python bingo
import { action as act } from 'typesafe-actions'
import actionTypes from './actionTypes';
import { Reducer } from 'redux'
/* Message Actions */
export const success = (message: string) => act(actionTypes.ALERT_SUCCESS, message);
export const danger = (message: string) => act(actionTypes.ALERT_DANGER, message)
export const clear = () => act(actionTypes.ALERT_CLEAR);
@ryan2clw
ryan2clw / index.html
Created August 17, 2019 13:32
The entry point of the python bingo app
<head>
<!-- keep the old stuff -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<head>
<!-- keep the old stuff -->
<footer class="text-center pt-3">
<p class="d-flex flex-row justify-content-center">
<a href="https://medium.com/@ryan.dines" target="_top">Brought to you with
<span class="fa fa-heart crimson"></span>
by Ryan Dines
@ryan2clw
ryan2clw / BingoPage(Part3).tsx
Last active August 24, 2019 22:26
Main page for python bingo
import * as React from 'react'
import styled from 'styled-components';
import { connect } from 'react-redux';
import { Flex } from 'rebass';
import { Container, Row, Spinner } from 'reactstrap';
import { IApplication, IConnectedReduxProps } from '../store/configureStore';
import BingoBoard from './BingoBoard';
import { requestNumbers } from '../store/cards';
import './styles/App.css';
import './styles/bootstrap.css';
@ryan2clw
ryan2clw / actionTypes.tsx
Last active August 23, 2019 23:56
Events for bingo game aka actionTypes
enum actionTypes {
/* Messaging system */
ALERT_SUCCESS = 'ALERT_SUCCESS',
ALERT_DANGER = 'ALERT_DANGER',
ALERT_CLEAR = 'ALERT_CLEAR',
/* Bingo cards */
CARD_REQUEST = 'CARD_REQUEST',
CARD_SUCCESS = 'CARD_SUCCESS',
CARD_FAILURE = 'CARD_FAILURE',
}
@ryan2clw
ryan2clw / bingoService.tsx
Last active August 24, 2019 00:10
Call the bingo backend with AJAX request
import { handleResponse } from '../store/events';
export const getCards = async () => {
const requestOptions = {
method: 'GET'
};
const uri = "http://localhost:8000/cards";
return fetch(uri, requestOptions)
.then(handleResponse)
.then(cards => cards);