Skip to content

Commit

Permalink
feat(web-ui): add error messages to chat creation (#840)
Browse files Browse the repository at this point in the history
Co-authored-by: Johan Book <{ID}+{username}@users.noreply.github.com>
  • Loading branch information
johanbook and Johan Book authored Jul 13, 2024
1 parent 89d8fb2 commit b611fe5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 7 additions & 0 deletions services/web-ui/public/locales/en/chat-create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"create": {
"button": "Create",
"error": "There was an error when creating the chat",
"success": "Chat created"
}
}
7 changes: 7 additions & 0 deletions services/web-ui/public/locales/sv/chat-create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"create": {
"button": "Skapa chat",
"error": "Något gick fel när din chat skulle skapas",
"success": "Chat skapad"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ReactElement, useState } from "react";
import { useNavigate } from "react-router";

import { Autocomplete, TextField } from "@mui/material";

import { chatsApi, organizationsApi } from "src/apis";
import { chatsApi, organizationsApi, profileApi } from "src/apis";
import { Button } from "src/components/ui";
import { useMutation, useQuery } from "src/core/query";
import { useTranslation } from "src/core/i18n";
import { CacheKeysConstants, useMutation, useQuery } from "src/core/query";
import { useSnackbar } from "src/core/snackbar";
import { ErrorView } from "src/views/ErrorView";

import { CreateChatPageNav } from "./CreateChatPage.nav";
Expand All @@ -16,14 +19,28 @@ interface Option {
}

export function CreateChatPageContainer(): ReactElement {
const navigate = useNavigate();
const snackbar = useSnackbar();
const { t } = useTranslation("chat-create");

const { error, data, isPending } = useQuery({
queryKey: ["members"],
queryFn: () => organizationsApi.getCurrentOrganizationMembers(),
});

const currentProfileQuery = useQuery({
queryKey: [CacheKeysConstants.CurrentProfile],
queryFn: () => profileApi.getCurrentProfile(),
});

const [members, setMembers] = useState<Option[]>([]);

const createChatMutation = useMutation({
onError: () => snackbar.error(t("create.error")),
onSuccess: () => {
snackbar.success(t("create.success"));
navigate("/chat");
},
mutationFn: () =>
chatsApi.createConversation({
createChatCommand: {
Expand All @@ -32,23 +49,25 @@ export function CreateChatPageContainer(): ReactElement {
}),
});

if (error) {
if (error || currentProfileQuery.error) {
return (
<CreateChatPageNav>
<ErrorView />
</CreateChatPageNav>
);
}

if (isPending) {
if (isPending || currentProfileQuery.isPending) {
return (
<CreateChatPageNav>
<CreateChatPageSkeleton />
</CreateChatPageNav>
);
}

const options = data.map((x) => ({ id: x.profileId, label: x.name }));
const options = data
.map((x) => ({ id: x.profileId, label: x.name }))
.filter((x) => x.id !== currentProfileQuery.data.id);

return (
<CreateChatPageNav>
Expand All @@ -67,7 +86,7 @@ export function CreateChatPageContainer(): ReactElement {
sx={{ mt: 2 }}
variant="contained"
>
Create
{t("create.button")}
</Button>
</CreateChatPageNav>
);
Expand Down

0 comments on commit b611fe5

Please sign in to comment.