Skip to content

Instantly share code, notes, and snippets.

View LukeMwila's full-sized avatar
👨‍💻
Doing de tings

Lukonde Mwila LukeMwila

👨‍💻
Doing de tings
View GitHub Profile
@LukeMwila
LukeMwila / index.js
Last active June 7, 2018 14:01
Understanding State in React
import React, { Component } from 'react'
class MyComponent extends Component{
state = {
superHero: ''
}
/** Update the state */
updateFavouriteSuperHero(e){
e.preventDefault()
@LukeMwila
LukeMwila / index.ts
Last active February 22, 2020 14:31
Function that checks if parameter is an array that contains strings
const checkIfArrayOfStrings = (arrayToCheck: any): boolean => {
if (arrayToCheck && arrayToCheck instanceof Array && arrayToCheck.length) {
const arrayOfNonStringValues = arrayToCheck.filter((value: any) => {
return typeof value !== 'string';
});
return arrayOfNonStringValues && arrayOfNonStringValues.length
}
return false;
};
@LukeMwila
LukeMwila / index.ts
Created April 20, 2019 17:51
Check if a token is valid
const isTokenValid = (token: string | null): boolean => {
if (!token) {
return false;
}
try {
const decodedJwt: any = decode(token);
return decodedJwt.exp >= Date.now() / 1000;
} catch (e) {
return false;
}
@LukeMwila
LukeMwila / index.ts
Created April 20, 2019 17:59
Functions that return a week from a given date
const getDaysOfWeekFromGivenDate = (
date: Date | null
) => {
if (date) {
const startOfWeek = moment(date).startOf('isoWeek');
const weekArray = moment.weekdays();
const daysOfWeekInSelectedDate = generateWeek(
startOfWeek,
weekArray
);
@LukeMwila
LukeMwila / index.ts
Created April 20, 2019 18:07
Function that returns an array of date objects for a particular week, given a certain date
const getDaysOfWeekFromGivenDate = (
date: Date | null
) => {
if (date) {
const startOfWeek = moment(date).startOf('isoWeek');
const weekArray = moment.weekdays();
const daysOfWeekInSelectedDate = daysOfWeek.map((d, i) => {
return startOfWeek
.clone()
.add(i, 'd')
@LukeMwila
LukeMwila / handler.ts
Created May 5, 2019 20:08
Default handler file
import { APIGatewayEvent, Callback, Context, Handler } from 'aws-lambda';
export const hello: Handler = (event: APIGatewayEvent, context: Context, cb: Callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!',
input: event,
}),
};
@LukeMwila
LukeMwila / handler.ts
Created May 5, 2019 20:44
Handler for creating a customer
import { APIGatewayEvent, Callback, Context, Handler } from "aws-lambda";
import { createCustomerAndSubscribeToPlan } from "./stripe-api";
interface ICreateCustomer {
stripeToken: string;
email: string;
productPlan: string;
}
export const respond = (fulfillmentText: any): any => {
@LukeMwila
LukeMwila / createCustomerAndSubscribeToPlan.ts
Last active May 5, 2019 20:55
Function that creates customer account in Stripe and subscribes them to a product plan
import * as Stripe from "stripe";
/** Config */
const stripe = new Stripe('STRIPE_API_KEY'); // Stripe account secret key goes here
export async function createCustomerAndSubscribeToPlan(
stripeToken: string,
email: string,
productPlan: string
): Promise<any> {
@LukeMwila
LukeMwila / Styles.tsx
Created May 8, 2019 18:16
File containing styled components for product plans to be displayed
import styled from "styled-components";
/** Theme */
import { Colors } from "../Theme";
export const AppWrapper = styled.div`
display: flex;
flex: 1;
min-height: 100vh;
margin: 20px;
import * as React from "react";
const useErrorHandler = (initialState: string | null) => {
const [error, setError] = React.useState(initialState);
const showError = (errorMessage: string | null) => {
setError(errorMessage);
window.setTimeout(() => {
setError(null);
}, 3000);
};