-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from RooftopAcademy/15-10-2021
15 - 10 - 2021 / Users
- Loading branch information
Showing
72 changed files
with
1,115 additions
and
166 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/js/ | ||
/node_modules/ | ||
node_modules/ | ||
build/ | ||
public/js/ |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import User from "../interfaces/User"; | ||
|
||
import API from "./API"; | ||
import Router from "./Router"; | ||
import Storage from "./Storage"; | ||
|
||
export default class Users extends Storage<User> { | ||
constructor() { | ||
super("user", { | ||
id: null, | ||
email: "", | ||
password: "", | ||
}); | ||
} | ||
|
||
public async loginUser(email: string, password: string): Promise<void | string> { | ||
const response = await API.fetchAPI(Router.createURL("/users"), { email, password }); | ||
if (API.isApiError(response)) { | ||
return response.message | ||
} | ||
this.updateStorage({ | ||
id: response.id, | ||
email, | ||
password | ||
}) | ||
} | ||
|
||
public async registerUser(email: string, password: string): Promise<void | string> { | ||
const response = await API.fetchAPI(Router.createURL("/users"), { email, password }, "PUT"); | ||
if (API.isApiError(response)) { | ||
return response.message | ||
} | ||
this.updateStorage({ | ||
id: response.id, | ||
email, | ||
password | ||
}) | ||
} | ||
|
||
public isLogued(): boolean { | ||
return !!this.getStorage().id; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import app from "../main"; | ||
|
||
// HTML | ||
const LOGIN_FORM: HTMLFormElement = document.getElementById( | ||
"js-loginForm" | ||
) as HTMLFormElement; | ||
|
||
export default async function loginHandler(): Promise<void> { | ||
const { email, password } = Object.fromEntries( | ||
new FormData(LOGIN_FORM).entries() | ||
) as { | ||
[index: string]: string; | ||
}; | ||
const response = await app.users.loginUser(email, password); | ||
|
||
return alert(response ? response : "Logueado con Éxito"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import app from "../main"; | ||
|
||
// HTML | ||
const REGISTER_FORM: HTMLFormElement = document.getElementById( | ||
"js-registerForm" | ||
) as HTMLFormElement; | ||
|
||
export default async function registerHandler(): Promise<void> { | ||
const { email, password, passwordRepeat } = Object.fromEntries( | ||
new FormData(REGISTER_FORM).entries() | ||
) as { [index: string]: string }; | ||
|
||
if (password === passwordRepeat) { | ||
const response = await app.users.registerUser(email, password); | ||
|
||
return alert(response ? response : "Registrado con Éxito"); | ||
} | ||
|
||
alert("Las contraseñas no coinciden."); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default interface User { | ||
id: number, | ||
email: string, | ||
password: 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import loginHandler from "../handlers/loginHandler"; | ||
import registerHandler from "../handlers/registerHandler"; | ||
|
||
// HTML | ||
const LOGIN_FORM_BTN: HTMLElement = document.getElementById("js-loginFormBTN"); | ||
const REGISTER_FORM: HTMLElement = document.getElementById("js-registerForm"); | ||
|
||
// EVENTS | ||
const REGISTER_FORM_BTN_EVENT: string = "click"; | ||
const LOGIN_FORM_BTN_EVENT: string = "click"; | ||
|
||
export default function loginRegisterListener(): void { | ||
LOGIN_FORM_BTN.addEventListener(LOGIN_FORM_BTN_EVENT, loginHandler); | ||
REGISTER_FORM.addEventListener(REGISTER_FORM_BTN_EVENT, registerHandler); | ||
} |
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Filter from "./Filter"; | ||
|
||
const fs = require("fs"); | ||
|
||
const { DATABASE_DIR } = require("../config"); | ||
|
||
export default class DataBase extends Filter { | ||
private table: string; | ||
private readonly tableNames = ["config", "products", "users", "favorites"]; | ||
|
||
constructor(tableName: string) { | ||
super(); | ||
this.table = this.returnTable(tableName); | ||
} | ||
|
||
protected getTable(): any { | ||
return this.table; | ||
} | ||
|
||
protected setTable(tableName: string, tableData: any): void { | ||
fs.writeFileSync(this.getTablePath(tableName), JSON.stringify(tableData)); | ||
} | ||
|
||
protected returnTable(tableName: string): any { | ||
if (this.tableNames.includes(tableName)) { | ||
return JSON.parse(fs.readFileSync(this.getTablePath(tableName), "utf8")); | ||
} | ||
} | ||
|
||
protected getConfig(configEntry: string): any { | ||
return this.returnConfig()[configEntry]; | ||
} | ||
|
||
protected setConfig(configEntry: string, configData: any): void { | ||
const config = this.returnConfig(); | ||
config[configEntry] = configData; | ||
this.setTable("config", config); | ||
} | ||
|
||
private getTablePath(tableName: string): string { | ||
return `${DATABASE_DIR}/${tableName}.json`; | ||
} | ||
|
||
private returnConfig(): any { | ||
return this.returnTable("config"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default class Filter { | ||
protected inRange( | ||
elem: number, | ||
min: number = null, | ||
max: number = null | ||
): boolean { | ||
return (min ? elem >= min : true) && (max ? elem <= max : true); | ||
} | ||
} |
Oops, something went wrong.