Skip to content

Commit

Permalink
feat: move locale to employee profile page (#245)
Browse files Browse the repository at this point in the history
Refs: closes #244

## Summary

 Move locale to employee profile page.

## Changes

- Add section `Preferences` to the employee profile page 
- Add locale within `Preferences` section
- Remove locale from sidebar
  • Loading branch information
joaotomaspinheiro authored Jun 30, 2024
1 parent b1fd570 commit 3da2c95
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 40 deletions.
7 changes: 0 additions & 7 deletions web/src/domain/locale.ts

This file was deleted.

15 changes: 15 additions & 0 deletions web/src/lib/constants/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Supported locales.
*/
export enum SupportedLocales {
EN = "en",
PT = "pt",
}

/**
* Locale names.
*/
export enum LocaleNames {
EN = "English",
PT = "Português",
}
29 changes: 22 additions & 7 deletions web/src/lib/utils/i8n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { derived, get, writable, type Writable } from "svelte/store";
import en from "../../locales/en.json";
import pt from "../../locales/pt.json";
import schema from "../../locales/schema.json";
import { SupportedLocales } from "../../domain/locale";
import { LocaleNames, SupportedLocales } from "../constants/locale";

/**
* Identifiers of the locale schema.
Expand All @@ -14,6 +14,11 @@ type LocaleTextID = keyof (typeof schema)["properties"];
*/
const DEFAULT_LOCALE: SupportedLocales = SupportedLocales.EN;

/**
* The standard locale name in which the application is configured.
*/
const DEFAULT_LOCALE_NAME: LocaleNames = LocaleNames.EN;

/**
* Map of the supported locales with their respective configuration.
*/
Expand Down Expand Up @@ -59,6 +64,22 @@ export function getSupportedLocale(locale: string): SupportedLocales {
return DEFAULT_LOCALE;
}

/**
* Retrieves the name of a supported locale.
* @param locale Source locale.
* @returns Name of `locale` if its a supported locale, otherwise {@link DEFAULT_LOCALE_NAME}.
*/
export function getSupportedLocaleName(locale: string): LocaleNames {
switch (locale) {
case "en":
return LocaleNames.EN;
case "pt":
return LocaleNames.PT;
}

return DEFAULT_LOCALE_NAME;
}

/**
* Inits a custom store for the application locale.
* It synchronizes the store value with local storage.
Expand Down Expand Up @@ -88,9 +109,6 @@ function _locale(): Writable<string> {
// Set the supported locale in the lang attribute of the HTML element.
document.documentElement.setAttribute("lang", supportedLocale);

// Refresh current page when switching locales.
location.reload();

set(supportedLocale);
},
update(cb) {
Expand All @@ -102,9 +120,6 @@ function _locale(): Writable<string> {
// Set the supported locale in the lang attribute of the HTML element.
document.documentElement.setAttribute("lang", supportedLocale);

// Refresh current page when switching locales.
location.reload();

set(supportedLocale);
},
};
Expand Down
2 changes: 2 additions & 0 deletions web/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"save": "Save",
"preview": "Preview",
"work": "Work",
"preferences": "Preferences",
"language": "Language",
"confirm": "Confirm",
"selectLocation": "Select location",
"location": "Location",
Expand Down
2 changes: 2 additions & 0 deletions web/src/locales/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"save": "Guardar",
"preview": "Pré-visualização",
"work": "Trabalho",
"preferences": "Preferências",
"language": "Idioma",
"selectLocation": "Selecionar localização",
"location": "Localização",
"location.placeholder": "Selecione uma localização",
Expand Down
6 changes: 6 additions & 0 deletions web/src/locales/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"work": {
"type": "string"
},
"preferences": {
"type": "string"
},
"language": {
"type": "string"
},
"selectLocation": {
"type": "string"
},
Expand Down
19 changes: 0 additions & 19 deletions web/src/routes/backOffice/components/sidebar/LocaleSelect.svelte

This file was deleted.

2 changes: 0 additions & 2 deletions web/src/routes/backOffice/components/sidebar/SideBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import url from "../../../../lib/stores/url";
import { t } from "../../../../lib/utils/i8n";
import { BackOfficeRoutes } from "../../../constants/routes";
import LocaleSelect from "./LocaleSelect.svelte";
</script>

<nav>
Expand Down Expand Up @@ -90,7 +89,6 @@
</Link>
</li>
</ul>
<LocaleSelect />
</nav>

<style>
Expand Down
25 changes: 23 additions & 2 deletions web/src/routes/backOffice/employees/components/EmployeeForm.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import Button from "../../../../lib/components/Button.svelte";
import { t } from "../../../../lib/utils/i8n";
import { locale, t } from "../../../../lib/utils/i8n";
import DetailsFields from "../../../../lib/components/details/DetailsFields.svelte";
import DetailsSection from "../../../../lib/components/details/DetailsSection.svelte";
import DetailsContent from "../../../../lib/components/details/DetailsContent.svelte";
Expand All @@ -19,6 +19,8 @@
import { rolesOptions } from "../constants/roles";
import Option from "../../../../lib/components/Option.svelte";
import { formatTime24H } from "../../../../lib/utils/date";
import { isViewingSelf } from "../../../../lib/utils/auth";
import { LocaleNames } from "../../../../lib/constants/locale";
/**
* The back route.
Expand Down Expand Up @@ -68,6 +70,7 @@
location: GeoJSONFeaturePoint,
scheduleStart: string,
scheduleEnd: string,
selectedLocale: string,
) => void;
/**
Expand Down Expand Up @@ -351,6 +354,7 @@
const location = formData.get("location") ?? "";
const scheduleStart = formData.get("scheduleStart") ?? "";
const scheduleEnd = formData.get("scheduleEnd") ?? "";
const selectedLocale = formData.get("locale") ?? "";
// Check if all fields are strings.
if (
Expand All @@ -364,7 +368,8 @@
typeof scheduleStart !== "string" ||
typeof scheduleEnd !== "string" ||
typeof password !== "string" ||
typeof confirmPassword !== "string"
typeof confirmPassword !== "string" ||
typeof selectedLocale !== "string"
) {
return;
}
Expand Down Expand Up @@ -475,6 +480,7 @@
},
scheduleStart,
scheduleEnd,
selectedLocale,
);
}
</script>
Expand Down Expand Up @@ -692,6 +698,21 @@
{/if}
</DetailsFields>
</DetailsSection>

<!-- Preferences -->
{#if action === "edit" && employee && isViewingSelf(employee.id)}
<DetailsSection label={$t("preferences")}>
<DetailsFields>
<!-- Locale -->
<FormControl label={$t("language")}>
<Select name="locale" value={$locale}>
<Option value="en">{LocaleNames.EN}</Option>
<Option value="pt">{LocaleNames.PT}</Option>
</Select>
</FormControl>
</DetailsFields>
</DetailsSection>
{/if}
</DetailsContent>
</form>

Expand Down
15 changes: 13 additions & 2 deletions web/src/routes/backOffice/employees/details/Employee.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Spinner from "../../../../lib/components/Spinner.svelte";
import { Link, navigate } from "svelte-routing";
import Button from "../../../../lib/components/Button.svelte";
import { t } from "../../../../lib/utils/i8n";
import { getSupportedLocaleName, locale, t } from "../../../../lib/utils/i8n";
import DetailsHeader from "../../../../lib/components/details/DetailsHeader.svelte";
import ecomapHttpClient from "../../../../lib/clients/ecomap/http";
import type { Employee } from "../../../../domain/employees";
Expand Down Expand Up @@ -100,9 +100,10 @@
employee.geoJson.properties.wayName,
employee.geoJson.properties.municipalityName,
)}
{@const isSelf = isViewingSelf(employee.id)}

<DetailsHeader to="" title={`${employee.firstName} ${employee.lastName}`}>
{#if isViewingSelf(employee.id)}
{#if isSelf}
<Button
variant="secondary"
onClick={() => (openUpdatePasswordModal = true)}
Expand Down Expand Up @@ -151,6 +152,16 @@
/>
</DetailsFields>
</DetailsSection>
{#if isSelf}
<DetailsSection label={$t("preferences")}>
<DetailsFields>
<Field
label={$t("language")}
value={getSupportedLocaleName($locale)}
/>
</DetailsFields>
</DetailsSection>
{/if}
<DetailsSection label={$t("additionalInfo")}>
<DetailsFields>
<Field
Expand Down
10 changes: 9 additions & 1 deletion web/src/routes/backOffice/employees/edit/EditEmployee.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<script lang="ts">
import { navigate } from "svelte-routing";
import { BackOfficeRoutes } from "../../../constants/routes";
import { t } from "../../../../lib/utils/i8n";
import { locale, t } from "../../../../lib/utils/i8n";
import Spinner from "../../../../lib/components/Spinner.svelte";
import Card from "../../components/Card.svelte";
import ecomapHttpClient from "../../../../lib/clients/ecomap/http";
import type { GeoJSONFeaturePoint } from "../../../../domain/geojson";
import type { Employee } from "../../../../domain/employees";
import { getToastContext } from "../../../../lib/contexts/toast";
import EmployeeForm from "../components/EmployeeForm.svelte";
import { isViewingSelf } from "../../../../lib/utils/auth";
/**
* Employee ID.
Expand Down Expand Up @@ -51,6 +52,7 @@
* @param location Employee location.
* @param scheduleStart Employee scheduleStart.
* @param scheduleEnd Employee scheduleEnd.
* @param selectedLocale Selected locale for the application.
*/
async function updateEmployee(
username: string,
Expand All @@ -61,6 +63,7 @@
location: GeoJSONFeaturePoint,
scheduleStart: string,
scheduleEnd: string,
selectedLocale: string,
) {
isSubmittingForm = true;
Expand Down Expand Up @@ -110,6 +113,11 @@
return;
}
// If the employee is viewing themselves, update the application locale.
if (isViewingSelf(id)) {
locale.set(selectedLocale);
}
toast.show({
type: "success",
title: $t("employees.update.success"),
Expand Down

0 comments on commit 3da2c95

Please sign in to comment.