Skip to content

Instantly share code, notes, and snippets.

View intojs's full-sized avatar

Daniel Dughila intojs

View GitHub Profile
@intojs
intojs / View.js
Created November 21, 2020 00:36
React MVC - View
class View {
#counterEl;
#incrementBtnEl;
constructor(incrementBtnClick) {
this.#counterEl = document.getElementById("counter");
this.#incrementBtnEl = document.getElementById("increment-button");
this.#incrementBtnEl.addEventListener("click", incrementBtnClick);
@intojs
intojs / Model.js
Created November 21, 2020 00:35
React MVC - Model
class Model {
#view;
#counter = 0;
attach(view) {
this.#view = view;
this.#notify();
}
@intojs
intojs / Main.js
Created November 21, 2020 00:34
React MVC - Main
class Main {
static initialize() {
const subject = new Subject();
const observer = new Observer(subject);
subject.attach(observer);
}
}
Main.initialize();
@intojs
intojs / Observer.js
Created November 21, 2020 00:33
React MVC - Observer
class Observer {
#subject;
constructor(subject) {
this.#subject = subject;
}
update(changedSubject) {
if (changedSubject === this.#subject) {
console.log("Updated");
@intojs
intojs / Subject.js
Last active November 21, 2020 00:32
React MVC - Subject
class Subject {
#observers = new Set();
attach(observer) {
this.#observers.add(observer);
this.#notify();
}
detach(observer) {
@intojs
intojs / loanCalculatorComponent.ts
Created December 1, 2019 02:49
Loan Calculator Component
export const LoanCalculator: FC = () => {
const [loanCalculation, setLoanCalculation] = useState<CalculateLoanRes | null>(null);
const onSubmit = async (values: LoanCalculatorFormValues) => {
const {
email,
loanAmount,
loanTerm,
lifeInsuranceOptIn,
} = values;
@intojs
intojs / loanRouter.ts
Created December 1, 2019 02:47
Loan Router
Context.initialize({
loanRepo: new InMemoryLoanRepo(),
emailServ: new InMemoryEmailServ(),
});
const schema = Joi.object({
emailAddress: Joi
.string()
.email()
.required(),
@intojs
intojs / InMemoryLoanRepo.ts
Created December 1, 2019 02:31
In Memory Loan Repository
export class InMemoryLoanRepo implements LoanRepo {
readonly calculations: LoanCalculation[] = [];
findOne(address: EmailAddress): Promise<LoanCalculation | undefined> {
const predicate = (c: LoanCalculation) => c.emailAddress.value === address.value;
const calculation = this.calculations.find(predicate);
return Promise.resolve(calculation);
}
@intojs
intojs / EmailServ.ts
Created December 1, 2019 02:29
Email Service
import { CalculateLoanRes } from '../useCases/CalculateLoanRes';
export interface EmailServ {
send(email: string): Promise<string>;
generateEmail(calculation: CalculateLoanRes): string;
}
@intojs
intojs / LoanRepo.ts
Created December 1, 2019 02:28
Loan Repo
export interface LoanRepo {
findOne(address: EmailAddress): Promise<LoanCalculation | undefined>;
save(calculation: LoanCalculation): Promise<void>;
}