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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 1x 1x 1x | import React, { useEffect } from "react"; import { Fab, Grid, Menu, MenuItem, Tooltip } from "@mui/material"; import { ConceptForm } from "../components"; import { AppState } from "../../../redux"; import { resetConceptFormAction, retrieveConceptAction, upsertAllMappingsErrorSelector, upsertConceptAndMappingsAction, upsertConceptAndMappingsLoadingSelector, upsertConceptAndMappingsProgressSelector, upsertConceptErrorsSelector, viewConceptErrorsSelector, viewConceptLoadingSelector } from "../redux"; import { APIConcept, apiConceptToConcept, APIMapping, Concept } from "../types"; import { Redirect, useLocation, useParams } from "react-router"; import { connect } from "react-redux"; import Header from "../../../components/Header"; import { debug, ProgressOverlay, useAnchor, usePrevious, useQueryParams, CONTEXT } from "../../../utils"; import { DeleteSweepOutlined as DeleteIcon, MoreVert as MenuIcon, Pageview as PageViewIcon, RestoreFromTrashOutlined as RestoreIcon } from "@mui/icons-material"; import { Link } from "react-router-dom"; import { APIDictionary } from "../../dictionaries"; import { dictionarySelector, makeRetrieveDictionaryAction, retrieveDictionaryLoadingSelector } from "../../dictionaries/redux"; interface StateProps { fetchLoading: boolean; loading: boolean; concept?: APIConcept; linkedDictionary?: APIDictionary; mappings: APIMapping[]; fetchErrors?: {}; errors?: {}; allMappingErrors?: { errors: string }[]; progress?: string; } interface ActionProps { retrieveDictionary: ( ...args: Parameters<ReturnType<typeof makeRetrieveDictionaryAction>> ) => void; retrieveConcept: (...args: Parameters<typeof retrieveConceptAction>) => void; upsertConcept: ( ...args: Parameters<typeof upsertConceptAndMappingsAction> ) => void; resetConceptForm: () => void; } type Props = StateProps & ActionProps; interface ConceptPageQueryParams { conceptClass: string; linkedDictionary: string; } const CreateOrEditConceptPage: React.FC<Props> = ({ retrieveConcept, retrieveDictionary, concept, linkedDictionary, mappings, fetchLoading, fetchErrors, errors, loading, upsertConcept, resetConceptForm, allMappingErrors = [], progress }) => { const { pathname: url } = useLocation(); const { concept: conceptId } = useParams<{ concept: string }>(); const { conceptClass, linkedDictionary: linkedDictionaryUrl } = useQueryParams<ConceptPageQueryParams>(); const previouslyLoading = usePrevious(loading); const [menuAnchor, handleMenuClick, handleMenuClose] = useAnchor(); const sourceUrl = url.substring(0, url.indexOf("concepts/")); const conceptUrl = url.replace("/edit", ""); const anyMappingsErrors = !!allMappingErrors.length && allMappingErrors.some(value => value); let context = conceptId ? CONTEXT.edit : CONTEXT.create; let originallyEditing = context === CONTEXT.edit; // we created a concept, but there were some errors with mappings, so we switch to edit mode if (!loading && previouslyLoading && concept && (errors || anyMappingsErrors)) context = CONTEXT.edit; const defaultLocale = linkedDictionary?.default_locale; const supportedLocales = linkedDictionary?.supported_locales; const status = !errors && anyMappingsErrors ? "Some mappings were not updated or added. Fix the errors and retry." : progress; useEffect(() => { // only retrieve the concept if the context was edit at the beginning // otherwise we obviously have nothing to edit if (originallyEditing) retrieveConcept(conceptUrl); retrieveDictionary(linkedDictionaryUrl); // usually doing the following is a mistake and will bite us later // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect( () => resetConceptForm(), // eslint-disable-next-line react-hooks/exhaustive-deps [] ); // everything went hunky-dory, and we should redirect the user to the view concept page if (!loading && previouslyLoading && concept && !errors && !anyMappingsErrors) return ( <Redirect to={`${concept.version_url}${ linkedDictionaryUrl ? `?linkedDictionary=${linkedDictionaryUrl}` : "" }`} /> ); return ( <Header allowImplicitNavigation title={ context === CONTEXT.edit ? "Edit " + (concept ? concept.display_name : "concept") : "Create concept" } > <ProgressOverlay delayRender loading={fetchLoading}> <Grid id="editConceptPage" item xs={8} component="div"> <ConceptForm conceptClass={conceptClass} context={context} status={status} savedValues={ context === CONTEXT.edit ? apiConceptToConcept(concept, mappings) : undefined } loading={loading} errors={errors} allMappingErrors={allMappingErrors} supportLegacyMappings={originallyEditing} onSubmit={(data: Concept) => upsertConcept(data, sourceUrl, linkedDictionaryUrl) } defaultLocale={defaultLocale} supportedLocales={supportedLocales} /> </Grid> {context !== CONTEXT.edit ? null : ( <> <Tooltip title="Menu"> <Fab onClick={handleMenuClick} color="primary" className="fab"> <MenuIcon /> </Fab> </Tooltip> <Menu anchorEl={menuAnchor} keepMounted open={Boolean(menuAnchor)} onClose={handleMenuClose} > <MenuItem> <PageViewIcon /> <Link replace className="link" to={`${conceptUrl}?linkedDictionary=${linkedDictionaryUrl}`} > Discard changes and view </Link> </MenuItem> <MenuItem disabled={loading} onClick={() => { const rawConcept = apiConceptToConcept(concept, [], false); if (rawConcept) upsertConcept( { ...rawConcept, retired: !concept?.retired }, sourceUrl, linkedDictionaryUrl ); else debug("Retiring failed: rawConcept is undefined"); }} > {concept?.retired ? ( <> <RestoreIcon /> Unretire concept </> ) : ( <> <DeleteIcon /> Retire concept </> )} </MenuItem> </Menu> </> )} </ProgressOverlay> </Header> ); }; const mapStateToProps = (state: AppState) => ({ concept: state.concepts.concept, linkedDictionary: dictionarySelector(state), mappings: state.concepts.mappings, loading: upsertConceptAndMappingsLoadingSelector(state), fetchLoading: viewConceptLoadingSelector(state) || retrieveDictionaryLoadingSelector(state), fetchErrors: viewConceptErrorsSelector(state), errors: upsertConceptErrorsSelector(state), allMappingErrors: upsertAllMappingsErrorSelector(state), progress: upsertConceptAndMappingsProgressSelector(state) }); const mapActionsToProps = { resetConceptForm: resetConceptFormAction, retrieveDictionary: makeRetrieveDictionaryAction(true), retrieveConcept: retrieveConceptAction, upsertConcept: upsertConceptAndMappingsAction }; export default connect<StateProps, ActionProps, unknown, AppState>( mapStateToProps, mapActionsToProps )(CreateOrEditConceptPage); |