Skip to content

Instantly share code, notes, and snippets.

@MrMjauh
MrMjauh / Pagination.tsx
Last active June 3, 2019 17:54
Pagination and infinite scroll
import * as React from "react";
import { RefObject } from "react";
import { IContainerComponentProps } from "../../model/ui/IContainerComponentProps";
import { Backoff } from "../../services/util/Backoff";
import { DynamicIntervalHandler } from "../../services/util/VariableIntervaller";
const classnames = require("classnames");
const styles = require("./style.scss");
@MrMjauh
MrMjauh / Backoff.tsx
Last active June 3, 2019 17:53
Backoff strategy
/**
* Linear backoff strategy
*/
export class Backoff {
private timeMillisToSleep: number;
private start: number;
private max: number;
private step: number;
export interface IContainerComponentProps {
className?: string;
}
@MrMjauh
MrMjauh / Explore.tsx
Last active June 3, 2019 17:53
Connected component
export class Explore extends React.Component<Props, State> {
public render() {
const req: PageableSearchResult<string> | undefined = this.getRequest();
return (
<Pagination<number>
ref={this.paginationRef}
lastSyncedPage={this.lastSyncedPage()}
requestPage={(page: number) => this.requestPage(page)}
nextPage={() => req != null && req.next != null ? req.next : undefined}
>
class VariableIntervaller {
private id: number = 0;
private dic: any = {};
public start(work: () => void, getTimeout: () => number): number {
const newID: number = this.id++;
this.dic[newID] = {
run: true,
intervaller: undefined
@MrMjauh
MrMjauh / Person.ts
Last active June 11, 2019 22:04
Person.tsx
// Setting readonly to force immutability
interface Person {
readonly id: string;
readonly name: string;
// Using https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
// to store image blob as temp urls
// Just remeber to call revokeObjectURL() when you are done with this
readonly avatarURL?: string;
readonly bannerURL?: string;
@MrMjauh
MrMjauh / PartRender.tsx
Last active June 24, 2019 22:55
PartRender.tsx
renderUsers(): any {
return (
<div className="result">
{
this.props.result.map((user: User) => {
return (
<Card
key={user.id}
title={user.name}
avatar={user.avatarURL}
@MrMjauh
MrMjauh / CacheKey.ts
Last active June 12, 2019 11:30
Cache Key
export type CacheGroup = "USER_BANNER" | "USER_AVATAR";
export interface CacheKey {
readonly group: CacheGroup;
readonly id: string;
}
export function idFromCacheKey(key: CacheKey) {
return key.group + "@_@" + key.group;
}
@MrMjauh
MrMjauh / CacheAction.ts
Created June 12, 2019 11:47
Cache Actions and Creators
export enum CacheActionType {
CACHE_CLEANUP = "CACHE_CLEANUP",
CACHE_PURGE_ITEM = "CACHE_PRUGE_ITEM"
}
export function cleanupCache(): Action<CacheActionType> {
return {
type: CacheActionType.CACHE_CLEANUP
};
}
@MrMjauh
MrMjauh / Gradle
Last active June 27, 2019 22:03
Gradle Example
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
id 'jacoco'
id 'checkstyle'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.codecamos'