Skip to content

Commit

Permalink
feat: save user preferences for language
Browse files Browse the repository at this point in the history
  • Loading branch information
ajohn25 committed Nov 12, 2024
1 parent 96e192a commit 994d097
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 8 deletions.
6 changes: 6 additions & 0 deletions libs/gql-schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ const rootSchema = `
TEMPLATE
}
enum Language {
en
es
}
input BulkUpdateScriptInput {
searchString: String!
replaceString: String!
Expand Down Expand Up @@ -98,6 +103,7 @@ const rootSchema = `
email: String!
cell: String!
notificationFrequency: String!
language: Language!
oldPassword: String
newPassword: String
}
Expand Down
1 change: 1 addition & 0 deletions libs/gql-schema/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const schema = `
terms: Boolean
isSuperadmin: Boolean!
notificationFrequency: String!
language: Language!
isSuspended: Boolean!
}
Expand Down
7 changes: 7 additions & 0 deletions libs/spoke-codegen/src/graphql/session.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ query CurrentUserSuperAdmin {
}
}

query GetCurrentUserLanguage {
currentUser {
id
language
}
}

query CurrentUserOrganizationRoles($organizationId: String!) {
currentUser {
id
Expand Down
19 changes: 19 additions & 0 deletions migrations/20240920042142_add_user_language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function up(knex) {
return knex.schema.alterTable("user", (t) => {
t.enu("language", ["en", "es"]).notNullable().defaultTo("en");
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function down(knex) {
return knex.schema.alterTable("user", (t) => {
t.dropColumn("language");
});
};
14 changes: 14 additions & 0 deletions src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ export enum NotificationFrequencyType {
Daily = "DAILY",
None = "NONE"
}

export enum Language {
English = "en",
Spanish = "es"
}

export const languageEnumToLabel = (value: Language) => {
switch (value) {
case Language.Spanish:
return "Español";
default:
return "English";
}
};
2 changes: 1 addition & 1 deletion src/containers/TexterTodoList/components/TexterRequest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class TexterRequest extends React.Component {
return (
<Paper>
<div style={{ padding: "20px" }}>
<h3> No texts available right now </h3>
<h3>{this.props.t("no texts error")} </h3>
<p> Watch out for an announcement when new texts are available! </p>
</div>
</Paper>
Expand Down
23 changes: 20 additions & 3 deletions src/containers/UserEdit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import React from "react";
import Form from "react-formal";
import * as yup from "yup";

import { NotificationFrequencyType } from "../../api/user";
import {
Language,
languageEnumToLabel,
NotificationFrequencyType
} from "../../api/user";
import GSForm from "../../components/forms/GSForm";
import GSSubmitButton from "../../components/forms/GSSubmitButton";
import SpokeFormField from "../../components/forms/SpokeFormField";
Expand Down Expand Up @@ -43,7 +47,8 @@ const styles = StyleSheet.create({
class UserEdit extends React.Component {
state = {
user: {
notificationFrequency: NotificationFrequencyType.All
notificationFrequency: NotificationFrequencyType.All,
language: Language.English
},
changePasswordDialog: false,
successDialog: false,
Expand Down Expand Up @@ -175,7 +180,8 @@ class UserEdit extends React.Component {
firstName: yup.string().required(),
lastName: yup.string().required(),
cell: yup.string().required(),
notificationFrequency: yup.string().required()
notificationFrequency: yup.string().required(),
language: yup.string().required()
};
const password = yup.string().required();
const passwordConfirm = (refField = "password") =>
Expand Down Expand Up @@ -297,6 +303,16 @@ class UserEdit extends React.Component {
})
)}
/>
<SpokeFormField
label="Language"
name="language"
{...dataTest("language")}
type="select"
choices={Object.values(Language).map((option) => ({
value: option,
label: languageEnumToLabel(option)
}))}
/>
</span>
)}
{(authType === UserEditMode.Login ||
Expand Down Expand Up @@ -447,6 +463,7 @@ const mutations = {
cell
email
notificationFrequency
language
}
}
`,
Expand Down
8 changes: 7 additions & 1 deletion src/routes.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable react/no-unstable-nested-components */
import { gql } from "@apollo/client";
import i18n from "i18next";
import React from "react";
import { Redirect, Route, Switch, withRouter } from "react-router-dom";

Expand Down Expand Up @@ -54,11 +55,16 @@ class ProtectedInner extends React.Component {
query currentUser {
currentUser {
id
language
}
}
`
})
.then((result) => result.data.currentUser.id)
.then((result) => {
const { id: userId, language: userLanguage } = result.data.currentUser;
i18n.changeLanguage(userLanguage);
return userId;
})
.then(() => this.setState({ isAuthed: true }))
.catch((_err) => history.push(loginUrl));
}
Expand Down
7 changes: 7 additions & 0 deletions src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ enum CampaignBuilderMode {
TEMPLATE
}

enum Language {
en
es
}

input BulkUpdateScriptInput {
searchString: String!
replaceString: String!
Expand Down Expand Up @@ -64,6 +69,7 @@ input UserInput {
email: String!
cell: String!
notificationFrequency: String!
language: Language!
oldPassword: String
newPassword: String
}
Expand Down Expand Up @@ -366,6 +372,7 @@ type User {
terms: Boolean
isSuperadmin: Boolean!
notificationFrequency: String!
language: Language!
isSuspended: Boolean!
}

Expand Down
6 changes: 4 additions & 2 deletions src/server/api/root-mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ const rootMutations = {
last_name: userData.lastName,
email: userData.email,
cell: userData.cell,
notification_frequency: userData.notificationFrequency
notification_frequency: userData.notificationFrequency,
language: userData.language
},
["id", "auth0_id"]
);
Expand All @@ -494,7 +495,8 @@ const rootMutations = {
last_name: userData.lastName,
email: userData.email,
cell: userData.cell,
notification_frequency: userData.notificationFrequency
notification_frequency: userData.notificationFrequency,
language: userData.language
};
} else {
userData = member;
Expand Down
1 change: 1 addition & 0 deletions src/server/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const resolvers = {
"assignedCell",
"terms",
"notificationFrequency",
"language",
"isSuspended"
]),
isSuperadmin: (userRecord, _, { user: authUser }) => {
Expand Down
3 changes: 2 additions & 1 deletion src/server/local-auth-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ const signup: AuthHelper = async (options) => {
last_name: capitalizeWord(reqBody.lastName),
cell: reqBody.cell,
is_superadmin: false,
notification_frequency: reqBody.notificationFrequency
notification_frequency: reqBody.notificationFrequency,
language: reqBody.language
})
.returning("*");
resolve(user);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27814,6 +27814,11 @@ vm-browserify@^1.0.1, vm-browserify@^1.1.2:
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==

[email protected]:
version "3.1.0"
resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-3.1.0.tgz#614f7fbf8d801f0bb5f0661f5b2f5785750e4f09"
integrity sha512-Dhxzh5HZuiHQhbvTW9AMetFfBHDMYpo23Uo9btPXgdYP+3T5S+p+jgNy7spra+veYhBP2dCSgxR/i2Y02h5/6w==

vscode-json-languageservice@^4.1.6:
version "4.2.1"
resolved "https://registry.yarnpkg.com/vscode-json-languageservice/-/vscode-json-languageservice-4.2.1.tgz#94b6f471ece193bf4a1ef37f6ab5cce86d50a8b4"
Expand Down

0 comments on commit 994d097

Please sign in to comment.