All files / app/features/communities EditCommunityInfoPage.tsx

95.23% Statements 40/42
57.14% Branches 8/14
90% Functions 9/10
94.87% Lines 37/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 1755x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x       5x 5x   5x             5x       5x                     4x         11x 11x 11x             11x             11x   2x               1x     1x             1x       11x   2x 2x 2x 2x                                                                                                                                                            
import { styled, Typography } from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import HtmlMeta from "components/HtmlMeta";
import ImageInput from "components/ImageInput";
import MarkdownInput from "components/MarkdownInput";
import PageTitle from "components/PageTitle";
import Redirect from "components/Redirect";
import Snackbar from "components/Snackbar";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useForm } from "react-hook-form";
import { routeToCommunity } from "routes";
import { service } from "service";
import { theme } from "theme";
 
import { Community } from "../../proto/communities_pb";
import { Page } from "../../proto/pages_pb";
import { communityKey } from "../queryKeys";
import CommunityBase from "./CommunityBase";
 
const StyledForm = styled("form")(() => ({
  display: "grid",
  paddingBottom: theme.spacing(5),
  rowGap: theme.spacing(1),
  width: "100%",
}));
 
const StyledHelperText = styled(Typography)(() => ({
  textAlign: "center",
}));
 
const StyledUpdateButton = styled(Button)(() => ({
  justifySelf: "end",
}));
 
interface UpdatePageData {
  communityId: string;
  content: string;
  pageId: string;
  communityPhotoKey?: string;
}
 
export default function EditCommunityPage({
  communityId,
}: {
  communityId: number;
}) {
  const { t } = useTranslation([GLOBAL, COMMUNITIES]);
  const queryClient = useQueryClient();
  const router = useRouter();
  const {
    control,
    handleSubmit,
    register,
 
    formState: { errors },
  } = useForm<UpdatePageData>();
 
  const {
    error,
    isPending,
    isSuccess,
    mutate: updatePage,
  } = useMutation<Page.AsObject, RpcError, UpdatePageData>({
    mutationFn: ({ communityPhotoKey, content, pageId }) => {
      return service.pages.updatePage({
        content,
        pageId: +pageId,
        photoKey: communityPhotoKey,
      });
    },
 
    onSuccess(newPageData, { communityId }) {
      queryClient.setQueryData<Community.AsObject | undefined>(
        communityKey(+communityId),
        (community) =>
          community
            ? {
                ...community,
                mainPage: newPageData,
              }
            : undefined,
      );
      queryClient.invalidateQueries({ queryKey: communityKey(+communityId) });
    },
  });
 
  const onSubmit = handleSubmit(
    (data) => {
      updatePage(data);
      const currentPath = router.asPath;
      const newPath = currentPath.replace("/edit", "");
      router.push(newPath);
    },
    (errors) => {
      Iif (errors.communityPhotoKey) {
        window.scroll({ top: 0, behavior: "smooth" });
      }
    },
  );
 
  return (
    <CommunityBase communityId={communityId}>
      {({ community }) => {
        return community.mainPage?.canEdit ? (
          <>
            <HtmlMeta title={t("communities:edit_info_page_title")} />
            <PageTitle>{t("communities:edit_info_page_title")}</PageTitle>
            {(error || errors.communityPhotoKey) && (
              <Alert severity="error">
                {error?.message || errors.communityPhotoKey?.message || ""}
              </Alert>
            )}
            <StyledForm onSubmit={onSubmit}>
              <ImageInput
                alt={t("communities:community_image_input_alt")}
                control={control}
                id="community-image-input"
                initialPreviewSrc={community.mainPage.photoUrl || undefined}
                name="communityPhotoKey"
                type="rect"
              />
              <StyledHelperText variant="body1">
                {community.mainPage.photoUrl
                  ? t("communities:upload_helper_text_replace")
                  : t("communities:upload_helper_text")}
              </StyledHelperText>
              <Typography id="content-label" variant="h2">
                {t("communities:page_content_field_label")}
              </Typography>
              <MarkdownInput
                control={control}
                defaultValue={community.mainPage.content}
                labelId="content-label"
                id="content"
                name="content"
                imageUpload
                required={t("communities:page_content_required")}
              />
              <input
                id="pageId"
                {...register("pageId")}
                type="hidden"
                value={community.mainPage.pageId}
              />
              <input
                id="communityId"
                {...register("communityId")}
                type="hidden"
                value={community.communityId}
              />
              <StyledUpdateButton loading={isPending} type="submit">
                {t("global:save")}
              </StyledUpdateButton>
            </StyledForm>
            {isSuccess && (
              <Snackbar severity="success">
                {t("communities:edit_info_page_success_message")}
              </Snackbar>
            )}
          </>
        ) : (
          <Redirect
            to={routeToCommunity(community.communityId, community.slug, "info")}
          />
        );
      }}
    </CommunityBase>
  );
}