All files / app/features/communities EditCommunityInfoPage.tsx

94.44% Statements 34/36
63.15% Branches 12/19
87.5% Functions 7/8
94.28% Lines 33/35

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 175 176 177 178 179 180 181 182 183 184 185 186 1877x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x     7x 7x 7x 7x 7x   7x   7x                                                         7x         12x 12x 12x 12x             12x   2x               1x     1x             1x         12x   2x                 12x     16x                                                                                                                                                                
import { Typography } from "@material-ui/core";
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 { communityKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL } from "i18n/namespaces";
import { Community } from "proto/communities_pb";
import { Page } from "proto/pages_pb";
import { useForm } from "react-hook-form";
import { useMutation, useQueryClient } from "react-query";
import { routeToCommunity } from "routes";
import { service } from "service";
import makeStyles from "utils/makeStyles";
 
import CommunityBase from "./CommunityBase";
 
const useStyles = makeStyles((theme) => ({
  root: {
    display: "flex",
    justifyContent: "space-between",
  },
  imageUploadhelperText: {
    textAlign: "center",
  },
  form: {
    display: "grid",
    paddingBottom: theme.spacing(5),
    rowGap: theme.spacing(1),
    width: "100%",
  },
  uploadImageButton: {
    justifySelf: "end",
  },
  updateButton: {
    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 classes = useStyles();
  const queryClient = useQueryClient();
  const { control, handleSubmit, register, errors } = useForm<UpdatePageData>();
 
  const {
    error,
    isLoading,
    isSuccess,
    mutate: updatePage,
  } = useMutation<Page.AsObject, RpcError, UpdatePageData>(
    ({ 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(communityKey(+communityId));
      },
    }
  );
 
  const onSubmit = handleSubmit(
    (data) => {
      updatePage(data);
    },
    (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>
            )}
            <form className={classes.form} 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"
              />
              <Typography
                className={classes.imageUploadhelperText}
                variant="body1"
              >
                {community.mainPage.photoUrl
                  ? t("communities:upload_helper_text_replace")
                  : t("communities:upload_helper_text")}
              </Typography>
              <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")}
              />
              {errors.content && (
                <Typography color="error" variant="body2">
                  {errors.content.message}
                </Typography>
              )}
              <input
                id="pageId"
                name="pageId"
                type="hidden"
                ref={register}
                value={community.mainPage.pageId}
              />
              <input
                id="communityId"
                name="communityId"
                type="hidden"
                ref={register}
                value={community.communityId}
              />
              <Button
                loading={isLoading}
                className={classes.updateButton}
                type="submit"
              >
                {t("global:update")}
              </Button>
            </form>
            {isSuccess && (
              <Snackbar severity="success">
                {t("communities:edit_info_page_success_message")}
              </Snackbar>
            )}
          </>
        ) : (
          <Redirect
            to={routeToCommunity(community.communityId, community.slug, "info")}
          />
        );
      }}
    </CommunityBase>
  );
}