diff --git a/typescript-practice/src/ts/global/types.ts b/typescript-practice/src/ts/global/types.ts index 7307404..51de0c4 100644 --- a/typescript-practice/src/ts/global/types.ts +++ b/typescript-practice/src/ts/global/types.ts @@ -1,4 +1,14 @@ -export interface DataObject { +export interface IDataObject { id: string; - data: object; + data: T; +} + +export class DataObject { + public id: string; + public data: T; + + constructor(dataObject: IDataObject) { + this.id = dataObject?.id ?? null; + this.data = dataObject.data; + } } diff --git a/typescript-practice/src/ts/helpers/connect.ts b/typescript-practice/src/ts/helpers/connect.ts new file mode 100644 index 0000000..724a999 --- /dev/null +++ b/typescript-practice/src/ts/helpers/connect.ts @@ -0,0 +1,33 @@ +import { TIME_OUT_ERROR } from '../constants/message'; +import { TIME_OUT_SEC } from '../constants/config'; +import FirebaseService from '../services/firebaseService'; + +/** + * A waiting function with s second + * @param {number} s The time will be waiting + * @returns {Promise} A Promise will be only reject after s second + */ +export const timeout = (s: number): Promise => { + return new Promise((_, reject) => { + setTimeout(() => { + FirebaseService.disconnect(); + reject(TIME_OUT_ERROR); + }, s * 1000); + }); +}; + +/** + * A function waiting the action need to be perform and throw error after s second. + * @param {Function} action The action need to be perform. + * @returns { Object || Error } Return the any object from Firebase or Error + */ +export const timeOutConnect = async ( + action: Promise, +): Promise => { + const result: T | string = await Promise.race([ + action, + timeout(TIME_OUT_SEC), + ]); + + return result; +}; diff --git a/typescript-practice/src/ts/helpers/data.ts b/typescript-practice/src/ts/helpers/data.ts new file mode 100644 index 0000000..2f0ac13 --- /dev/null +++ b/typescript-practice/src/ts/helpers/data.ts @@ -0,0 +1,19 @@ +import { DataObject } from '../global/types'; + +export const createIdUser = (): number => { + return new Date().getTime(); +}; + +export const convertDataObjectToModel = (dataObj: DataObject): T => { + const { id } = dataObj; + + return { id, ...dataObj.data } as T; +}; + +export const convertModelToDataObject = >( + model: T, +): T => { + const { id, ...data } = model; + + return { id, data } as T; +}; diff --git a/typescript-practice/src/ts/helpers/helpers.ts b/typescript-practice/src/ts/helpers/helpers.ts deleted file mode 100644 index 26b62f5..0000000 --- a/typescript-practice/src/ts/helpers/helpers.ts +++ /dev/null @@ -1,85 +0,0 @@ -import * as MESSAGE from '../constants/message'; -import { TIME_OUT_SEC, REGEX } from '../constants/config'; -import FirebaseService from '../services/firebaseService'; -import { DataObject } from '../global/types'; - -/** - * Validate password - * @param {string} password Password input - * @returns {boolean} Return true if validate password success, otherwise return false - */ -export const isValidPassword = (password: string): boolean => { - return REGEX.PASSWORD.test(password); -}; - -export const isValidateEmail = (email: string): RegExpMatchArray | null => { - return String(email).toLowerCase().match(REGEX.EMAIL); -}; - -export const compare2Password = ( - password: string, - passwordConfirm: string, -): boolean => { - return password === passwordConfirm; -}; - -/** - * A waiting function with s second - * @param {number} s The time will be waiting - * @returns {Promise} A Promise will be only reject after s second - */ -export const timeout = (s: number): Promise => { - return new Promise((_, reject) => { - setTimeout(() => { - FirebaseService.disconnect(); - reject(MESSAGE.TIME_OUT_ERROR); - }, s * 1000); - }); -}; - -export const createIdUser = (): number => { - return new Date().getTime(); -}; - -/** - * A function waiting the action need to be perform and throw error after s second. - * @param {Function} action The action need to be perform. - * @returns { Object || Error } Return the any object from Firebase or Error - */ -export const timeOutConnect = async ( - action: Promise, -): Promise => { - const result: T | string = await Promise.race([ - action, - timeout(TIME_OUT_SEC), - ]); - - return result; -}; - -export const renderRequiredText = (field: string, element: Element): void => { - const markup: string = ` -

${MESSAGE.REQUIRED_MESSAGE(field)}

- `; - - element.insertAdjacentHTML('afterend', markup); -}; - -export const redirectToLoginPage = (): void => { - window.location.replace('/login'); -}; - -export const convertDataObjectToModel = (dataObj: { - id: string; - data: object; -}): DataObject => { - const { id } = dataObj; - - return { id, ...dataObj.data } as DataObject; -}; - -export const convertModelToDataObject = (model: T): T => { - const { id, ...data } = model; - - return { id, data } as T; -}; diff --git a/typescript-practice/src/ts/helpers/redirect.ts b/typescript-practice/src/ts/helpers/redirect.ts new file mode 100644 index 0000000..e19d01c --- /dev/null +++ b/typescript-practice/src/ts/helpers/redirect.ts @@ -0,0 +1,3 @@ +export const redirectToLoginPage = (): void => { + window.location.replace('/login'); +}; diff --git a/typescript-practice/src/ts/helpers/validatorForm.ts b/typescript-practice/src/ts/helpers/validatorForm.ts new file mode 100644 index 0000000..f5d9326 --- /dev/null +++ b/typescript-practice/src/ts/helpers/validatorForm.ts @@ -0,0 +1,30 @@ +import { REGEX } from '../constants/config'; +import * as MESSAGE from '../constants/message'; + +/** + * Validate password + * @param {string} password Password input + * @returns {boolean} Return true if validate password success, otherwise return false + */ +export const isValidPassword = (password: string): boolean => { + return REGEX.PASSWORD.test(password); +}; + +export const isValidateEmail = (email: string): RegExpMatchArray | null => { + return String(email).toLowerCase().match(REGEX.EMAIL); +}; + +export const compare2Password = ( + password: string, + passwordConfirm: string, +): boolean => { + return password === passwordConfirm; +}; + +export const renderRequiredText = (field: string, element: Element): void => { + const markup: string = ` +

${MESSAGE.REQUIRED_MESSAGE(field)}

+ `; + + element.insertAdjacentHTML('afterend', markup); +}; diff --git a/typescript-practice/src/ts/models/user.ts b/typescript-practice/src/ts/models/user.ts index f4129a6..03ff9b8 100644 --- a/typescript-practice/src/ts/models/user.ts +++ b/typescript-practice/src/ts/models/user.ts @@ -1,4 +1,4 @@ -import { createIdUser } from '../helpers/helpers'; +import { createIdUser } from '../helpers/data'; export default class User { private id: number; diff --git a/typescript-practice/src/ts/services/commonService.ts b/typescript-practice/src/ts/services/commonService.ts index 69c70c2..92f39cd 100644 --- a/typescript-practice/src/ts/services/commonService.ts +++ b/typescript-practice/src/ts/services/commonService.ts @@ -1,12 +1,12 @@ +import { timeOutConnect } from '../helpers/connect'; import { - timeOutConnect, convertDataObjectToModel, convertModelToDataObject, -} from '../helpers/helpers'; +} from '../helpers/data'; import { DataObject } from '../global/types'; import FirebaseService from './firebaseService'; -export default class CommonService { +export default class CommonService> { private defaultPath: string = '/'; private firebaseService = FirebaseService; @@ -65,7 +65,7 @@ export default class CommonService { if (typeof results === 'object') { // Convert format object return results.map((data) => { - const tempData = data as DataObject; + const tempData = data as DataObject; return convertDataObjectToModel(tempData); }); @@ -89,7 +89,7 @@ export default class CommonService { // Convert format object return results.map((data) => { - const tempData = data as DataObject; + const tempData = data as DataObject; return convertDataObjectToModel(tempData); });