Skip to content

Instantly share code, notes, and snippets.

View newerton's full-sized avatar
🏖️
Working

Newerton newerton

🏖️
Working
View GitHub Profile
import { Author, Book, Genre } from '../entities';
import { IGenericRepository } from './generic-repository.abstract';
export abstract class IDataServices {
abstract authors: IGenericRepository<Author>;
abstract books: IGenericRepository<Book>;
abstract genres: IGenericRepository<Genre>;
}
export abstract class IGenericRepository<T> {
abstract getAll(): Promise<T[]>;
abstract get(id: string): Promise<T>;
abstract create(item: T): Promise<T>;
abstract update(id: string, item: T);
}
export class Author {
firstName: string;
lastName: string;
}
export class Genre {
name: string;
}
export class Book {
@newerton
newerton / connected-login-form.tsx
Created September 15, 2022 18:48
Apply Dependency inversion principle (DIP)
import api from '~/common/api'
const ConnectedLoginForm = () => {
const handleSubmit = async (email, password) => {
await api.login(email, password)
}
return (
<LoginForm onSubmit={handleSubmit} />
)
@newerton
newerton / login-form-2.tsx
Created September 15, 2022 18:47
Apply Dependency inversion principle (DIP)
type Props = {
onSubmit: (email: string, password: string) => Promise<void>
}
const LoginForm = ({ onSubmit }: Props) => {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const handleSubmit = async (evt) => {
evt.preventDefault()
@newerton
newerton / login-form.tsx
Created September 15, 2022 18:46
Dependency inversion principle (DIP)
import api from '~/common/api'
const LoginForm = () => {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const handleSubmit = async (evt) => {
evt.preventDefault()
await api.login(email, password)
}
@newerton
newerton / video-list-3.tsx
Created September 15, 2022 18:13
Apply Interface segregation principle (ISP)
type Props = {
items: Array<Video | LiveStream>
}
const VideoList = ({ items }) => {
return (
<ul>
{items.map(item => {
if ('coverUrl' in item) {
// it's a video
@newerton
newerton / thumbnail-2.tsx
Created September 15, 2022 18:11
Apply Interface segregation principle (ISP)
type Props = {
coverUrl: string
}
const Thumbnail = ({ coverUrl }: Props) => {
return <img src={coverUrl} />
}
@newerton
newerton / video-list-2.tsx
Created September 15, 2022 18:10
Apply Interface segregation principle (ISP)
type Props = {
items: Array<Video | LiveStream>
}
const VideoList = ({ items }) => {
return (
<ul>
{items.map(item => {
if ('coverUrl' in item) {
// it's a video
@newerton
newerton / live-stream.tsx
Created September 15, 2022 18:10
Interface segregation principle (ISP)
type LiveStream = {
name: string
previewUrl: string
}