-
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.
Firebreak: Centralised profiles (#387)
Co-authored-by: Sam Dudley <samuel.dudley@digital.trade.gov.uk> Co-authored-by: Cameron Lamb <cameron.lamb@digital.trade.gov.uk>
- Loading branch information
1 parent
44a19b3
commit 9654364
Showing
10 changed files
with
227 additions
and
4 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
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,138 @@ | ||
(function () { | ||
/** | ||
* | ||
* @param {String} html | ||
* @returns {Node} | ||
*/ | ||
function htmlTemplate(html) { | ||
const template = document.createElement("template"); | ||
template.innerHTML = html; | ||
|
||
return template.content.cloneNode(true); | ||
} | ||
|
||
/** | ||
* | ||
* @returns {String} | ||
*/ | ||
function getProfileCardUrl() { | ||
const metaName = "profile-card-url"; | ||
const meta = document.querySelector(`meta[name="${metaName}"]`); | ||
|
||
return meta.content; | ||
} | ||
|
||
/** | ||
* | ||
* @returns {Node} | ||
*/ | ||
async function getProfileCard() { | ||
const url = getProfileCardUrl(); | ||
|
||
const response = await fetch(url, { | ||
credentials: "include", | ||
cache: "default", | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Error fetching profile card"); | ||
} | ||
|
||
const html = await response.text(); | ||
|
||
return htmlTemplate(html); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
class ProfileCard extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
const css = ` | ||
<style> | ||
:host { | ||
display: inline-block; | ||
width: 100%; | ||
} | ||
a { | ||
color: inherit; | ||
text-decoration: none; | ||
} | ||
ul { | ||
padding: 0; | ||
list-style-type: none; | ||
} | ||
.profile-card { | ||
font-family: "GDS Transport", arial, sans-serif; | ||
height: 40px; | ||
display: flex; | ||
flex-direction: row-reverse; | ||
gap: 0.5rem; | ||
align-items: center; | ||
container-type: inline-block; | ||
} | ||
.profile-photo { | ||
height: 100%; | ||
object-fit: cover; | ||
aspect-ratio: 1 / 1; | ||
} | ||
.profile-details { | ||
text-align: right; | ||
} | ||
.profile-name { | ||
text-decoration: underline; | ||
} | ||
.profile-completion { | ||
font-size: 0.875rem; | ||
} | ||
.loading .profile-photo { | ||
background-color: lightgray; | ||
} | ||
</style> | ||
<div class="profile-card loading"> | ||
<div class="profile-photo"></div> | ||
<div class="profile-details"> | ||
<ul> | ||
<li class="profile-name"></li> | ||
<li>Loading...</li> | ||
</ul> | ||
</div> | ||
</div> | ||
`; | ||
|
||
const cssTemplate = htmlTemplate(css); | ||
|
||
this.attachShadow({ mode: "open" }); | ||
this.shadowRoot.append(cssTemplate); | ||
} | ||
|
||
async connectedCallback() { | ||
const profileName = this.getAttribute("profile-name"); | ||
|
||
if (profileName) { | ||
this.find(".profile-name").innerHTML = profileName; | ||
} | ||
|
||
let html = null; | ||
|
||
try { | ||
html = await getProfileCard(); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
|
||
if (html) { | ||
this.shadowRoot.querySelector(".loading").style.display = "none"; | ||
this.shadowRoot.append(html); | ||
} | ||
} | ||
|
||
find(selector) { | ||
return this.shadowRoot.querySelector(selector); | ||
} | ||
} | ||
|
||
customElements.define("profile-card", ProfileCard); | ||
})(); |
31 changes: 31 additions & 0 deletions
31
src/peoplefinder/templates/peoplefinder/components/profile-card.html
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,31 @@ | ||
{% comment %} | ||
context: | ||
profile (Person) | ||
profile_url (str) | ||
no_photo_url (str) | ||
{% endcomment %} | ||
<a href="{{ profile_url }}" target="_blank"> | ||
<div class="profile-card" data-profile-uuid="{{ profile.slug }}"> | ||
{% if profile and profile.photo_small %} | ||
<img class="profile-photo" | ||
src="{{ profile.photo_small.url }}" | ||
alt="Photo of {{ profile.full_name }}"> | ||
{% else %} | ||
<img class="profile-photo" src="{{ no_photo_url }}" alt="No photo"> | ||
{% endif %} | ||
<ul class="profile-details"> | ||
{% if profile %} | ||
<li class="profile-name">Hi {{ profile.preferred_first_name }}</li> | ||
<li class="profile-completion"> | ||
View your profile | ||
{% if profile.profile_completion < 100 %} | ||
({{ profile.profile_completion }}% complete) | ||
{% endif %} | ||
</li> | ||
{% else %} | ||
<li class="profile-not-found">No profile found</li> | ||
{% endif %} | ||
</ul> | ||
</div> | ||
</div> | ||
</a> |
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