Skip to content

Instantly share code, notes, and snippets.

@willie-hung
willie-hung / page.tsx
Last active July 12, 2023 17:39
post_5
interface Todo {
userId: number;
id: number;
title: string;
completed: boolean;
}
async function getData() {
const res = await fetch("https://jsonplaceholder.typicode.com/todos");
function fetchData(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = "Here are some mock data 🤡";
if (data) {
resolve(data);
} else {
reject("Data not found");
}
}, 2000);
function fetchUser(userId: number): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (userId === 1) {
resolve("Sung-Jie Hung");
} else {
reject("User not found");
}
}, 2000);
});
function fetchUser(userId: number): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (userId === 1) {
resolve("Sung-Jie Hung");
} else {
reject("User not found");
}
}, 2000);
});
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: YOUR_API_KEY,
authDomain: YOUR_AUTH_DOMAIN,
projectId: YOUR_PROJECT_ID,
storageBucket: YOUR_STORAGE_BUCKET,
messagingSenderId: YOUR_SENDER_ID,
appId: YOUR_APP_ID,
const firebaseConfig = {
apiKey: YOUR_API_KEY,
authDomain: YOUR_AUTH_DOMAIN,
projectId: YOUR_PROJECT_ID,
storageBucket: YOUR_STORAGE_BUCKET,
messagingSenderId: YOUR_SENDER_ID,
appId: YOUR_APP_ID,
measurementId: YOUR_MEASUREMENT_ID,
};
@willie-hung
willie-hung / addData.ts
Last active July 13, 2023 16:50
post_7
import { db } from "./firebaseConfig";
import { collection, doc, getDocs, updateDoc } from "firebase/firestore";
async function addData(name: string, age: number, college: string) {
const objectsRef = collection(db, "test");
try {
const querySnapshot = await getDocs(objectsRef);
querySnapshot.forEach(async (docSnapshot) => {
const documentId = docSnapshot.id;
import { db } from "./firbaseConfig";
import {
collection,
getDocs,
query,
where,
} from "firebase/firestore";
const queryFirestore = async (name: string) => {
const objectsRef = collection(db, "test");
import { useQuery } from "@tanstack/react-query";
import axios from "axios";
interface Todo {
id: number;
title: string;
userId: number;
completed: boolean;
}
import useTodos from "./hooks/useTodos";
const TodoList = () => {
const { data, error, isLoading } = useTodos();
if (isLoading) return <p>Loading...</p>;
if (error) return <p>{error.message}</p>;
return (