Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(header): Avoid need to match certain format for urls #163

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/header-component/src/components/dc-header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "@stencil/core";
import { syncCurrentUser } from "../../services/session";
import { getGuestActions } from "../../utils/config";
import { addSlash } from "../../utils/normalise";

type User = {
id: number;
Expand Down Expand Up @@ -109,8 +110,13 @@ export class Header {

// A return promise here will force to wait before first render
componentWillLoad() {
// Make sure to get the logo from prop or use default
this._logo = this.logo || this._defaultLogo;
this._logoSmall = this.logosmall || this._defaultLogoSmall;
// normalise the links to make sure they match the expected format
this.homepage = addSlash(this.homepage);
this.community = addSlash(this.community);
// Allow to fetch users session
this.syncCurrentUser();
}

Expand Down
6 changes: 3 additions & 3 deletions packages/header-component/src/services/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sanitiseSSOUrl } from "../utils/sanitise";
import { trimSlash } from "../utils/normalise";

// https://stackoverflow.com/a/33829607/1422380
const getCSRFToken = async (discourseEndpoint) => {
Expand Down Expand Up @@ -42,7 +42,7 @@ const getCurrentUser = async (discourseEndpoint, { csrfToken }) => {

export const syncCurrentUser = async (community) => {
let currentUser;
let discourseEndpoint = sanitiseSSOUrl(community);
let discourseEndpoint = trimSlash(community);

try {
const csrfToken = await getCSRFToken(discourseEndpoint);
Expand All @@ -55,7 +55,7 @@ export const syncCurrentUser = async (community) => {
};

export const logout = async (community, username) => {
let discourseEndpoint = sanitiseSSOUrl(community);
let discourseEndpoint = trimSlash(community);
const url = `${discourseEndpoint}/session/${username}`;
const csrfToken = await getCSRFToken(discourseEndpoint);

Expand Down
4 changes: 2 additions & 2 deletions packages/header-component/src/utils/community.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { sanitiseSSOUrl } from "./sanitise";
import { trimSlash } from "./normalise";

/**
* preffix a given string with the base community URL.
*
* @param str typically a url that needs to be preffixed with community base url
*/
export const preffixCommunityURL = (community, str) =>
`${sanitiseSSOUrl(community)}/${str}`;
`${trimSlash(community)}/${str}`;

/**
* Takes an object with avatar_template (typically user) and return a full
Expand Down
19 changes: 19 additions & 0 deletions packages/header-component/src/utils/normalise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const trimSlash = (url: string) => {
if (url === "/") {
return window.location.origin;
}

if (url.charAt(url.length - 1) === "/") {
return url.slice(0, -1);
}

return url;
};

export const addSlash = (url: string) => {
if (url.charAt(url.length - 1) !== "/") {
return `${url}/`;
}

return url;
};
11 changes: 0 additions & 11 deletions packages/header-component/src/utils/sanitise.ts

This file was deleted.