Skip to content

Instantly share code, notes, and snippets.

View tudorilisoi's full-sized avatar

Tudor Ilisoi tudorilisoi

View GitHub Profile
fetch('/api/recipe').then(res => {
const data = res.json()
//do smthng with react
return data
})
.then(parsedJSON => {
fetch(`/api/recipes/${data.id}`)
})
@tudorilisoi
tudorilisoi / index.js
Created May 13, 2020 14:18
Quiz app boilerplate
const STORE = {
score: 0,
currentQuestionIndex: 0,
questions: [
{
title: 'How many players in a team, y\'all?',
answers: [
'12',
'11',
@tudorilisoi
tudorilisoi / index.js
Created May 10, 2020 13:42
TODO list
/* const listItemTemp = ('<li>' +
'<span class="shopping-item"></span>' +
'<div class="shopping-item-controls">' +
'<button class="shopping-item-toggle">' +
'<span class="button-label">check</span>' +
'</button>' +
'<button class="shopping-item-delete">' +
'<span class="button-label">delete</span>' +
'</button>' +
'</div>' +
@tudorilisoi
tudorilisoi / FeatureItem.jsx
Last active March 27, 2020 19:49
a React propTypes example
import React, { Component } from 'react';
import slugify from 'slugify';
import pTypes from 'prop-types';
// This object will allow us to
// easily convert numbers into US dollar values
// React propTypes
const USCurrencyFormat = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
function Async({promiseFn, fallback, errFallback, children}){
const [state, setState] = useState('initial')
useEffect(()=>{
promiseFn().then(()=>{
setState('resolved')
}).catch(()=>{
setState('error')
})
})
import React, { useState, useEffect, useContext } from 'react';
const AppContext = React.createContext(null);
export default AppContext;
export const defaultData = {
loadedItems:[]
}
// Context provider
export const AppContextProvider = ({ initialData, children }) => {
@tudorilisoi
tudorilisoi / yup-validate-object.js
Created January 27, 2020 18:27
yup-validate-object
import * as yup from 'yup';
const getPostSchema = (catIDs) => {
const schema = yup.object().shape({
title: yup.string()
.label('Titlul')
.required().min(10).max(255),
content: yup.string()
.label('Textul')
.required().min(20),
cat_id: yup.number()
// this wraps an async express route handler function into a try-catch
// it returns a new function which handles failures gracefully
// by sending a JSON encoded message and a HTTP 500 (internal server error) status code
function expressTryCatchWrapper(requestHandler) {
return async function (req, resp, next) {
try {
await requestHandler(req, resp, next)
} catch (ex) {
console.error(`expressTryCatch ERROR ${req.url}`, ex)
if (resp.headersSent) {
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const helmet = require("helmet");
const { NODE_ENV } = require("./config");
const app = express();
const morganOption = NODE_ENV === "production" ? " tiny" : "common";
export const withLayout = (C, cProps = {}) => {
const name = `withLayout-${C.displayName || C.name}`
function WithLayout(props) {
return (
<Layout {...props}>
<C {...cProps} />
</Layout>
)
}
WithLayout.displayName = name