From 6e243bdf236b6b2465a5aacb17aed8a08f5e2332 Mon Sep 17 00:00:00 2001 From: Loi Phan Date: Mon, 11 Sep 2023 14:02:05 +0700 Subject: [PATCH] Change naming function --- javascript-practice/src/js/helpers/helpers.js | 16 ++++++++++++---- javascript-practice/src/js/views/view.js | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/javascript-practice/src/js/helpers/helpers.js b/javascript-practice/src/js/helpers/helpers.js index 5f58efc..5c5596e 100644 --- a/javascript-practice/src/js/helpers/helpers.js +++ b/javascript-practice/src/js/helpers/helpers.js @@ -63,10 +63,18 @@ export const convertDataObjectToModel = (data) => { return { id, ...object.data }; }; -export const getURL = () => { +export const getSubdirectoryURL = () => { const url = window.location.href; - const parts = url.split('/'); - const lastPart = parts.pop(); // Lấy phần tử cuối cùng trong mảng - return lastPart; + const parts = url.split('/'); // Results: ['http:', '', 'example.com', ''] + + const subDirectory = parts[3]; // Get subdirectory url only + + // Remove query behind subDirectory + const index = subDirectory.indexOf('?'); + if (index !== -1) { + return subDirectory.substring(0, index); + } + + return subDirectory; }; diff --git a/javascript-practice/src/js/views/view.js b/javascript-practice/src/js/views/view.js index e9e04d5..d98bd38 100644 --- a/javascript-practice/src/js/views/view.js +++ b/javascript-practice/src/js/views/view.js @@ -1,11 +1,11 @@ import RegisterView from './registerView'; import LoginView from './loginView'; -import { getURL } from '../helpers/helpers'; +import { getSubdirectoryURL } from '../helpers/helpers'; import { URL } from '../constants/constant'; export default class View { constructor() { - switch (getURL()) { + switch (getSubdirectoryURL()) { case URL.LOGIN: this.loginView = new LoginView(); break;