Skip to content

Commit

Permalink
Misc changes to improve the routing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Dec 11, 2024
1 parent 0e9acf1 commit 15d2b2a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
6 changes: 2 additions & 4 deletions frontend/components/page/PageBreadcrumbs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,11 @@ const eventStore = useEventStore();
let organization: Organization;
let group: Group;
let event: Event;
const organizationRegex = /^(http:\/\/localhost:\d+|https?:\/\/[\w.-]+)(\/[a-z]{2})?\/organizations\/[0-9a-fA-F-]+(\/about)?$/;
const organizationRegex =
/^(http:\/\/localhost:\d+|https?:\/\/[\w.-]+)(\/[a-z]{2})?\/organizations\/[0-9a-fA-F-]+(\/about)?$/;
if (organizationRegex.test(url)) {
pageType = "organization";
// Extract the UUID from the URL using regex
const match = url.match(organizationRegex);
const uuid = match ? match[3] : null; // Adjust group index as needed
await organizationStore.fetchById(id);
organization = organizationStore.organization;
} else if (url.includes("/organizations/") && url.includes("/groups/")) {
Expand Down
1 change: 1 addition & 0 deletions frontend/stores/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const useGroupStore = defineStore("group", {

if (status.value === "success") {
const group = data.value!;

this.group.id = group.id;
this.group.name = group.name;
this.group.tagline = group.tagline;
Expand Down
1 change: 1 addition & 0 deletions frontend/stores/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const useOrganizationStore = defineStore("organization", {

if (status.value === "success") {
const organization = data.value!;

this.organization.id = organization.id;
this.organization.orgName = organization.orgName;
this.organization.name = organization.name;
Expand Down
18 changes: 8 additions & 10 deletions frontend/tests/specs/organizationLogic.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { describe, it, expect, jest } from '@jest/globals';
// Mock fetchById function and organization data directly.
const mockFetchById = vi.fn();
const mockOrganization = { id: "test-uuid", name: "Test Organization" };

// Mock fetchById function and organization data directly
const mockFetchById = jest.fn();
const mockOrganization = { id: 'test-uuid', name: 'Test Organization' };

// Mock PageBreadcrumbs logic
jest.mock('@/components/page/PageBreadcrumbs.vue', () => ({
fetchById: mockFetchById,
organization: mockOrganization,
}));
// Mock PageBreadcrumbs logic.
vi.mock("@/components/page/PageBreadcrumbs.vue", () => ({
fetchById: mockFetchById,
organization: mockOrganization,
}));
23 changes: 12 additions & 11 deletions frontend/utils/routeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import locales from "~/locales";

// Define the supported locales. Adjust as needed to match your project.
const LOCALES = ['en', 'fr', 'de'];
const localCodes = locales.map((l) => l.code);

/**
* Normalize a given path by removing leading and trailing slashes.
*/
function normalizePath(path: string): string {
return path.replace(/^\/|\/$/g, '');
return path.replace(/^\/|\/$/g, "");
}


/**
* Remove the leading locale segment from the given route segments if present.
* For example, ['en', 'organizations'] -> ['organizations'].
*/
function removeLocaleSegment(segments: string[]): string[] {
if (segments.length > 0 && LOCALES.includes(segments[0])) {
if (segments.length > 0 && localCodes.includes(segments[0])) {
return segments.slice(1);
}
return segments;
Expand All @@ -27,22 +26,24 @@ export function isRouteActive(routePath: string): boolean {
const currentPath = normalizePath(route.path);
const targetPath = normalizePath(routePath);

let currentSegments = currentPath.split('/');
let targetSegments = targetPath.split('/');
let currentSegments = currentPath.split("/");
let targetSegments = targetPath.split("/");

// Remove locale segments from both current and target paths
// Remove locale segments from both current and target paths.
currentSegments = removeLocaleSegment(currentSegments);
targetSegments = removeLocaleSegment(targetSegments);

// If current route is shorter than the target, it cannot match
// If current route is shorter than the target, it cannot match.
if (currentSegments.length < targetSegments.length) {
return false;
}

// Check if the target segments match the end of the current segments
// Check if the target segments match the end of the current segments.
return targetSegments.every(
(segment, index) =>
currentSegments[currentSegments.length - targetSegments.length + index] === segment
currentSegments[
currentSegments.length - targetSegments.length + index
] === segment
);
}

Expand Down

0 comments on commit 15d2b2a

Please sign in to comment.