Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas Lombart thomaslombart

View GitHub Profile
@thomaslombart
thomaslombart / Posts.js
Created Aug 29, 2020
An example of a Posts app written with React
View Posts.js
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 Aug 29, 2020
A to-do app written with React
View Todos.js
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 Aug 29, 2020
An example of Counter in React with two different testing tools
View counter-enzyme.test.js
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 Aug 24, 2020
An example of a toggle switch with Vue 3
View Switch.vue
<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 Jun 25, 2020
Save data in conversation for dialogflow-fulfillment Node.js library
View dialogflow-agent-data.js
// 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 Nov 27, 2018
Example of a stack implemented with test-driven development
View stack.js
class Stack {
constructor (capacity) {
this.items = []
this.capacity = capacity
}
isEmpty () {
return this.items.length === 0
}