Skip to content

Instantly share code, notes, and snippets.

@longsangstan
longsangstan / useSave.ts
Last active May 22, 2020 00:38
react hook for post/put
import { useState } from "react";
import urljoin from "url-join";
const useSave = <T>(method: "post" | "put") => {
const [isSaving, setIsSaving] = useState(false);
const [error, setError] = useState("");
const save = async (endpoint: string, body: T) => {
setError("");
@longsangstan
longsangstan / useGet.ts
Last active May 22, 2020 00:38
react hook for get
import { useCallback, useEffect, useState } from "react";
const useGet = <T>(endpoint: string) => {
const [data, setData] = useState<T | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const fetchData = useCallback(async () => {
setError("");
setIsLoading(true);
@longsangstan
longsangstan / s3ListAllObjects.js
Created September 10, 2018 07:14
List all objects in s3
const s3 = require("../aws").s3;
/**
* List all objects in the specified S3 folder.
* @param {String} Bucket
* @param {String} Prefix
* @param {String} NextContinuationToken
* @param {Array} PreviousContents
* @returns {Promise<Array>} A promise containing an array of all objects in the folder
*/
@longsangstan
longsangstan / image.resize.in.github.flavored.markdown.md
Last active September 5, 2018 15:57 — forked from uupaa/image.resize.in.github.flavored.markdown.md
image resize in github flavored markdown.

Image source

https://gyazo.com/eb5c5741b6a9a16c692170a41a49c858.png

import { MaskedViewIOS, View } from "react-native";
import LinearGradient from "react-native-linear-gradient";
// ...
const MaskedView = Platform.OS == "ios" ? MaskedViewIOS : View;
render = () => {
return (
<MaskedView
var { height, width } = Dimensions.get("window");
const isIpad = Platform.OS == "ios" && height / width == 4 / 3;
////
import Modal from "react-native-modalbox";
const { width, height } = Dimensions.get("window");
////
renderModal = () => {
let marginTop = height / 2 - 20;
@longsangstan
longsangstan / dynamodb-modify-all.js
Created April 28, 2017 09:16
dynamodb modify all items
var scanAll = require("./scanAll");
var docClient = require("../aws").docClient;
var co = require("co");
var modifyAll = co.wrap(function*(table, action) {
var items = yield scanAll(table);
var promises = [];
for (var i = 0; i < items.length; i++) {
items[i] = action(items[i]);
@longsangstan
longsangstan / react-text-file-reader.js
Created April 27, 2017 03:30
A React component to read text file.
import React from "react";
/*
Read a text file and out put the content.
Example Usage:
var myTxt = require("./myTxt.txt");
...
@longsangstan
longsangstan / dynamodb-query-all.js
Created April 26, 2017 07:51
dynamodb query all items
var docClient = require("../aws").docClient;
var co = require("co");
var queryAll = co.wrap(function*(
params,
previousItems,
lastEvaluatedKey
) {
var items = previousItems || [];