Skip to content

Instantly share code, notes, and snippets.

View phucdph's full-sized avatar
🎯
Focusing

Phuc Dang phucdph

🎯
Focusing
View GitHub Profile
@phucdph
phucdph / BasicForm.tsx
Last active April 4, 2020 14:09
[Formik] Basic Input
const BasicForm = () => {
const handleSubmit = React.useCallback((values: IFormValues) => {
console.log(values);
}, []);
return (
<Formik<IFormValues>
initialValues={{
username: "",
name: "",
@phucdph
phucdph / BasicForm.tsx
Created April 4, 2020 13:57
[Formik] Define
interface IFormValues {
username: string;
name: string;
email: string;
password: string;
password_confirmation: string;
}
@phucdph
phucdph / TodoList.tsx
Created April 1, 2020 04:45
[Todolist] UI
import * as React from "react";
import { ITodo } from "./services/typings";
interface IProps {
data: ITodo[];
isLoading: boolean;
onGetTodos: () => void;
}
const TodoList = (props: IProps) => {
@phucdph
phucdph / TodoListContainer.tsx
Created April 1, 2020 04:38
[Todolist] Container
import * as React from "react";
import TodoList from "./TodoList";
import { useTodos } from "./hooks";
const TodoListContainer = () => {
const { data, isLoading, dispatchGetTodos } = useTodos();
return (
<TodoList data={data} isLoading={isLoading} onGetTodos={dispatchGetTodos} />
);
@phucdph
phucdph / hooks.ts
Created April 1, 2020 04:09
[Todolist] Hooks
import { useDispatch, useSelector } from "react-redux";
import { useCallback } from "react";
import { getTodos, getTodosSuccess, getTodosFail } from "./actions";
import { todoListStateSelector } from "./selectors";
import todoService from "./services/todoService";
export const useTodos = () => {
const dispatch = useDispatch();
const dispatchGetTodos = useCallback(async () => {
@phucdph
phucdph / selectors.ts
Created April 1, 2020 04:08
[Todolist] selector
import { TodoListState } from "./state";
export const todoListStateSelector = (state: any): TodoListState => state.todo;
@phucdph
phucdph / reducers.ts
Created April 1, 2020 04:00
[Todolist] Reducers
import { initialState, TodoListState } from "./state";
import { handleActions, Action } from "redux-actions";
import { getTodos, getTodosSuccess, getTodosFail } from "./actions";
import { ITodo } from "./services/typings";
const reducers = handleActions(
new Map([
[
getTodos,
(state: TodoListState, action: Action<{}>) => ({
@phucdph
phucdph / actions.ts
Created April 1, 2020 03:55
[Todolist] Redux actions
import { createAction } from "redux-actions";
const getTodos = createAction("GET_TODOS");
const getTodosSuccess = createAction("GET_TODOS_SUCCESS");
const getTodosFail = createAction("GET_TODOS_FAIL");
export { getTodos, getTodosSuccess, getTodosFail };
@phucdph
phucdph / todoService.ts
Created April 1, 2020 03:52
[TodoList] Todo service
import { ITodo } from "./typings";
export default {
getTodos: async (): Promise<ITodo[]> => {
const res = await fetch("https://jsonplaceholder.typicode.com/todos");
const json = await res.json();
return json;
}
};
@phucdph
phucdph / index.d.ts
Last active April 1, 2020 03:48
[Todolist] Type of todo
export interface ITodo {
id: string;
title: string;
completed: boolean;
}