This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class View { | |
#counterEl; | |
#incrementBtnEl; | |
constructor(incrementBtnClick) { | |
this.#counterEl = document.getElementById("counter"); | |
this.#incrementBtnEl = document.getElementById("increment-button"); | |
this.#incrementBtnEl.addEventListener("click", incrementBtnClick); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model { | |
#view; | |
#counter = 0; | |
attach(view) { | |
this.#view = view; | |
this.#notify(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main { | |
static initialize() { | |
const subject = new Subject(); | |
const observer = new Observer(subject); | |
subject.attach(observer); | |
} | |
} | |
Main.initialize(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Observer { | |
#subject; | |
constructor(subject) { | |
this.#subject = subject; | |
} | |
update(changedSubject) { | |
if (changedSubject === this.#subject) { | |
console.log("Updated"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Subject { | |
#observers = new Set(); | |
attach(observer) { | |
this.#observers.add(observer); | |
this.#notify(); | |
} | |
detach(observer) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const LoanCalculator: FC = () => { | |
const [loanCalculation, setLoanCalculation] = useState<CalculateLoanRes | null>(null); | |
const onSubmit = async (values: LoanCalculatorFormValues) => { | |
const { | |
email, | |
loanAmount, | |
loanTerm, | |
lifeInsuranceOptIn, | |
} = values; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Context.initialize({ | |
loanRepo: new InMemoryLoanRepo(), | |
emailServ: new InMemoryEmailServ(), | |
}); | |
const schema = Joi.object({ | |
emailAddress: Joi | |
.string() | |
.email() | |
.required(), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CalculateLoanRes } from '../useCases/CalculateLoanRes'; | |
export interface EmailServ { | |
send(email: string): Promise<string>; | |
generateEmail(calculation: CalculateLoanRes): string; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface LoanRepo { | |
findOne(address: EmailAddress): Promise<LoanCalculation | undefined>; | |
save(calculation: LoanCalculation): Promise<void>; | |
} |