All files / src/apps/dictionaries/pages ViewDictionaryPage.tsx

16.67% Statements 3/18
0% Branches 0/15
0% Functions 0/5
17.65% Lines 3/17

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 187 188 189 190 191 192 193 194                                                                                                                        1x                                                                                                                                                                                                                                   1x                     1x                
import React, { useEffect } from "react";
import { DictionaryDetails, DictionaryForm } from "../components";
import { Grid, Paper, Typography } from "@mui/material";
import { connect } from "react-redux";
import {
  APIDictionary,
  apiDictionaryToDictionary,
  APIDictionaryVersion,
  DictionaryVersion
} from "../types";
import {
  orgsSelector,
  profileSelector
} from "../../authentication/redux/reducer";
import { APIOrg, APIProfile, canModifyContainer } from "../../authentication";
 
import {
  createDictionaryVersionAction,
  createDictionaryVersionErrorSelector,
  createDictionaryVersionLoadingSelector,
  dictionarySelector,
  editDictionaryVersionAction,
  retrieveDictionaryAndDetailsAction,
  retrieveDictionaryErrorSelector,
  retrieveDictionaryLoadingSelector,
  retrieveDictionaryVersionLoadingSelector,
  retrieveDictionaryVersionsAction
} from "../redux";
import { AppState } from "../../../redux";
import { useLocation, useParams } from "react-router-dom";
import { CONTEXT } from "../../../utils";
import { ProgressOverlay } from "../../../utils/components";
import { EditButton } from "../../containers/components/EditButton";
import { EDIT_BUTTON_TITLE } from "../redux/constants";
import ContainerReleasedVersions from "../../containers/components/ContainerReleasedVersions";
import Header from "../../../components/Header";
interface Props {
  profile?: APIProfile;
  usersOrgs?: APIOrg[];
  dictionaryLoading: boolean;
  dictionary?: APIDictionary;
  retrieveDictionaryAndDetails: (
    ...args: Parameters<typeof retrieveDictionaryAndDetailsAction>
  ) => void;
  createDictionaryVersion: (
    ...args: Parameters<typeof createDictionaryVersionAction>
  ) => void;
  editDictionaryVersion: (
    ...args: Parameters<typeof editDictionaryVersionAction>
  ) => void;
  retrieveDictionaryVersions: (
    ...args: Parameters<typeof retrieveDictionaryVersionsAction>
  ) => void;
  versions: APIDictionaryVersion[];
  versionsLoading: boolean;
  createVersionLoading: boolean;
  createVersionError?: { detail: string };
  retrieveDictionaryErrors?: {};
}
 
export const ViewDictionaryPage: React.FC<Props> = ({
  profile,
  usersOrgs = [],
  dictionaryLoading,
  dictionary,
  retrieveDictionaryAndDetails,
  versions,
  versionsLoading,
  createDictionaryVersion,
  editDictionaryVersion,
  retrieveDictionaryVersions,
  createVersionLoading,
  createVersionError,
  retrieveDictionaryErrors
}: Props) => {
  const { pathname: url } = useLocation();
  const { ownerType, owner } = useParams<{
    ownerType: string;
    owner: string;
  }>();
 
  useEffect(() => {
    retrieveDictionaryAndDetails(url);
  }, [url, retrieveDictionaryAndDetails]);
 
  const canEditDictionary = canModifyContainer(
    ownerType,
    owner,
    profile,
    usersOrgs
  );
  const showEditButton = canEditDictionary; // redundant, yes, but we don't want to couple them
 
  const { name } = dictionary || {};
  return (
    <Header
      title={` Your Dictionaries > ${name}`}
      backUrl="/user/collections/"
      backText="Back to dictionaries"
      justifyChildren="space-around"
    >
      <ProgressOverlay
        delayRender
        loading={dictionaryLoading}
        error={
          retrieveDictionaryErrors
            ? "Could not load dictionary. Refresh the page to retry"
            : undefined
        }
      >
        <Grid id="viewDictionaryPage" item xs={5} component="div">
          <Paper className="fieldsetParent">
            <fieldset style={{ minWidth: "0" }}>
              <Typography component="legend" variant="h5" gutterBottom>
                General Details
              </Typography>
              <DictionaryForm
                context={CONTEXT.view}
                savedValues={apiDictionaryToDictionary(dictionary)}
                profile={profile}
                usersOrgs={usersOrgs}
                loading={true}
              />
            </fieldset>
          </Paper>
        </Grid>
        <Grid item xs={5} container spacing={2}>
          <Grid item xs={12} component="div">
            {dictionary ? (
              <DictionaryDetails dictionary={dictionary} />
            ) : (
              <span>Couldn't find dictionary details</span>
            )}
          </Grid>
 
          <Grid item xs={12} component="div">
            {versionsLoading ? (
              "Loading versions..."
            ) : (
              <ContainerReleasedVersions
                versions={versions}
                dictionary={dictionary}
                showCreateVersionButton={canEditDictionary}
                createVersion={async (data: DictionaryVersion) => {
                  const response: any = await createDictionaryVersion(
                    url,
                    data
                  );
                  if (response) {
                    retrieveDictionaryVersions(url);
                  }
                }}
                createVersionLoading={createVersionLoading}
                createVersionError={createVersionError}
                url={url}
                editVersion={async (data: DictionaryVersion) => {
                  const response: any = await editDictionaryVersion(url, data);
                  if (response) {
                    retrieveDictionaryVersions(url);
                  }
                }}
                type={"Dictionary"}
              />
            )}
          </Grid>
        </Grid>
        {!showEditButton ? null : (
          <EditButton url={`${url}edit/`} title={EDIT_BUTTON_TITLE} />
        )}
      </ProgressOverlay>
    </Header>
  );
};
 
export const mapStateToProps = (state: AppState) => ({
  profile: profileSelector(state),
  usersOrgs: orgsSelector(state),
  dictionaryLoading: retrieveDictionaryLoadingSelector(state),
  dictionary: dictionarySelector(state),
  versions: state.dictionaries.versions,
  versionsLoading: retrieveDictionaryVersionLoadingSelector(state),
  createVersionLoading: createDictionaryVersionLoadingSelector(state),
  createVersionError: createDictionaryVersionErrorSelector(state),
  retrieveDictionaryErrors: retrieveDictionaryErrorSelector(state)
});
export const mapDispatchToProps = {
  retrieveDictionaryAndDetails: retrieveDictionaryAndDetailsAction,
  createDictionaryVersion: createDictionaryVersionAction,
  editDictionaryVersion: editDictionaryVersionAction,
  retrieveDictionaryVersions: retrieveDictionaryVersionsAction
};
 
export default connect(mapStateToProps, mapDispatchToProps)(ViewDictionaryPage);