Skip to content

Instantly share code, notes, and snippets.

@thomaslombart
thomaslombart / Posts.js
Created August 29, 2020 14:19
An example of a Posts app written with React
import React from "react"
import { addPost } from "./api"
function Posts() {
const [posts, addLocalPost] = React.useReducer((s, a) => [...s, a], [])
const [formData, setFormData] = React.useReducer((s, a) => ({ ...s, ...a }), {
title: "",
content: "",
})
@thomaslombart
thomaslombart / Todos.js
Created August 29, 2020 14:12
A to-do app written with React
import React from "react";
function Todos({ todos: originalTodos }) {
const filters = ["all", "active", "done"];
const [input, setInput] = React.useState("");
const [todos, setTodos] = React.useState(originalTodos || []);
const [activeFilter, setActiveFilter] = React.useState(filters[0]);
const addTodo = (e) => {
if (e.key === "Enter" && input.length > 0) {
@thomaslombart
thomaslombart / counter-enzyme.test.js
Last active August 29, 2020 13:57
An example of Counter in React with two different testing tools
import React from "react";
import { shallow } from "enzyme";
import Counter from "./counter";
describe("<Counter />", () => {
it("properly increments and decrements the counter", () => {
const wrapper = shallow(<Counter />);
expect(wrapper.state("count")).toBe(0);
@thomaslombart
thomaslombart / Switch.vue
Created August 24, 2020 16:03
An example of a toggle switch with Vue 3
<template>
<label class="container">
<input
v-bind="$attrs"
class="input"
type="checkbox"
:checked="checked"
@change="$emit('update:checked', $event.target.checked)"
/>
<span class="switch"></span>
@thomaslombart
thomaslombart / dialogflow-agent-data.js
Last active June 25, 2020 14:47
Save data in conversation for dialogflow-fulfillment Node.js library
// agent is an instance of WebhookClient
function setContextData(agent) {
const handler = {
set(target, property, value) {
const parameters = agent.context.get("data")
? agent.context.get("data").parameters
: {};
parameters[property] = value;
@thomaslombart
thomaslombart / stack.js
Last active November 27, 2018 15:46
Example of a stack implemented with test-driven development
class Stack {
constructor (capacity) {
this.items = []
this.capacity = capacity
}
isEmpty () {
return this.items.length === 0
}