Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leafty committed Jan 3, 2025
1 parent f8c6d93 commit 9f06c78
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 152 deletions.
28 changes: 14 additions & 14 deletions client/src/dataset/Dataset.present.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { groupBy, isEmpty } from "lodash-es";
import { useState } from "react";
import { Helmet } from "react-helmet";
import { Link, useNavigate } from "react-router-dom-v5-compat";
import { Link, useLocation, useNavigate } from "react-router-dom-v5-compat";
import {
Button,
Card,
Expand Down Expand Up @@ -271,21 +271,22 @@ function DisplayInfoTable(props) {
}

function ErrorAfterCreation(props) {
const location = useLocation();

const editButton = (
<Link
className="float-right me-1 mb-1"
id="editDatasetTooltip"
to={(location) =>
cleanModifyLocation(location, { dataset: props.dataset })
}
to={cleanModifyLocation(location)}
state={{ dataset: props.dataset }}
>
<Button size="sm" color="danger" className="btn-icon-text">
<FontAwesomeIcon icon={faPen} color="dark" /> Edit
</Button>
</Link>
);

return props.location.state && props.location.state.errorOnCreation ? (
return location.state && location.state.errorOnCreation ? (
<ErrorAlert>
<strong>Error on creation</strong>
<br />
Expand Down Expand Up @@ -345,6 +346,8 @@ function EditDatasetButton({
locked,
maintainer,
}) {
const location = useLocation();

if (!insideProject || !maintainer) return null;
if (locked) {
return (
Expand All @@ -368,14 +371,8 @@ function EditDatasetButton({
className="float-right mb-1"
id="editDatasetTooltip"
data-cy="edit-dataset-button"
to={(location) =>
cleanModifyLocation(location, {
dataset,
files,
isFilesFetching,
filesFetchError,
})
}
to={cleanModifyLocation(location)}
state={{ dataset, files, isFilesFetching, filesFetchError }}
>
<Button
className="btn-outline-rk-pink icon-button"
Expand Down Expand Up @@ -500,7 +497,10 @@ export default function DatasetView(props) {
}
>
<Col>
<ErrorAfterCreation location={props.location} dataset={dataset} />
<ErrorAfterCreation
// location={props.location}
dataset={dataset}
/>
{props.insideProject ? null : (
<Helmet>
<title>{pageTitle}</title>
Expand Down
4 changes: 2 additions & 2 deletions client/src/dataset/DatasetFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ export { mapDataset, getDatasetAuthors, getUpdatedDatasetImage };
* @param {*} location - current location object
* @param {*} newState - any new state to set
*/
export function cleanModifyLocation(location, newState) {
export function cleanModifyLocation(location) {
return {
...location,
pathname: location.pathname.endsWith("/")
? location.pathname + "modify"
: location.pathname + "/modify",
state: newState,
// state: newState,
};
}
41 changes: 28 additions & 13 deletions client/src/features/project/dataset/DatasetModify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import cx from "classnames";
import React from "react";
import type { FieldErrors } from "react-hook-form";
import { SubmitHandler, useForm } from "react-hook-form";
import { useHistory } from "react-router-dom";
// import { useHistory } from "react-router-dom";
import {
type NavigateFunction,
useLocation,
useNavigate,
} from "react-router-dom-v5-compat";
import { Button, FormGroup, UncontrolledAlert } from "reactstrap";

import { ExternalLink } from "../../../components/ExternalLinks";
Expand Down Expand Up @@ -70,7 +75,8 @@ export type PostSubmitProps = {
datasetId: string;
dispatch: AppDispatch;
fetchDatasets: (forceRefetch: boolean, versionUrl: string) => Promise<void>;
history: DatasetModifyProps["history"];
// history: DatasetModifyProps["history"];
navigate: NavigateFunction;
projectPathWithNamespace: string;
state?: unknown;
versionUrl: string;
Expand All @@ -79,17 +85,21 @@ async function redirectAfterSubmit({
datasetId,
dispatch,
fetchDatasets,
history,
// history,
navigate,
projectPathWithNamespace,
state,
versionUrl,
}: PostSubmitProps) {
dispatch(reset());
await fetchDatasets(true, versionUrl);
history.push({
pathname: `/projects/${projectPathWithNamespace}/datasets/${datasetId}`,
navigate(`/projects/${projectPathWithNamespace}/datasets/${datasetId}`, {
state,
});
// history.push({
// pathname: `/projects/${projectPathWithNamespace}/datasets/${datasetId}`,
// state,
// });
}

type DatasetCreateSubmitGroupProps = {
Expand Down Expand Up @@ -359,9 +369,7 @@ export interface DatasetModifyProps extends DatasetModifyDisplayProps {
existingFiles: DatasetModifyFormProps["existingFiles"];
externalUrl: string;
fetchDatasets: PostSubmitProps["fetchDatasets"];
history: ReturnType<typeof useHistory>;
initialized: boolean;
location: { pathname: string };
metadataVersion: number | undefined;
notifications: unknown;
onCancel: () => void;
Expand All @@ -380,15 +388,18 @@ export default function DatasetModify(props: DatasetModifyProps) {
defaultBranch,
externalUrl,
fetchDatasets,
history,
location,
// history,
// location,
metadataVersion,
overviewCommitsUrl,
projectPathWithNamespace,
setSubmitting,
versionUrl,
} = props;

const location = useLocation();
const navigate = useNavigate();

const edit = dataset != null;
const slug = dataset?.slug;

Expand Down Expand Up @@ -487,7 +498,8 @@ export default function DatasetModify(props: DatasetModifyProps) {
await redirectAfterSubmit({
datasetId: dataset?.slug ?? response.data.slug,
fetchDatasets,
history,
// history,
navigate,
projectPathWithNamespace,
state: undefined,
dispatch,
Expand Down Expand Up @@ -524,7 +536,8 @@ export default function DatasetModify(props: DatasetModifyProps) {
await redirectAfterSubmit({
datasetId: response.data.slug,
fetchDatasets,
history,
// history,
navigate,
projectPathWithNamespace,
state: { errorOnCreation: true },
dispatch,
Expand All @@ -541,7 +554,8 @@ export default function DatasetModify(props: DatasetModifyProps) {
await redirectAfterSubmit({
datasetId: dataset?.slug ?? response.data.slug,
fetchDatasets,
history,
// history,
navigate,
projectPathWithNamespace,
state: undefined,
dispatch,
Expand Down Expand Up @@ -599,7 +613,8 @@ export default function DatasetModify(props: DatasetModifyProps) {
edit,
externalUrl,
fetchDatasets,
history,
// history,
navigate,
metadataVersion,
slug,
postDatasetMutation,
Expand Down
8 changes: 4 additions & 4 deletions client/src/features/project/dataset/ProjectDatasetImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { StateModelProject } from "../project.types";
type ProjectDatasetImportProps = {
client: DatasetImportProps["client"];
fetchDatasets: DatasetImportProps["fetchDatasets"];
history: DatasetImportProps["history"];
location: DatasetImportProps["location"];
// history: DatasetImportProps["history"];
// location: DatasetImportProps["location"];
model: unknown;
notifications: unknown;
params: unknown;
Expand All @@ -33,8 +33,8 @@ function ProjectDatasetImport(props: ProjectDatasetImportProps) {
client={props.client}
externalUrl={externalUrl}
fetchDatasets={props.fetchDatasets}
history={props.history}
location={props.location}
// history={props.history}
// location={props.location}
projectPathWithNamespace={projectPathWithNamespace}
toggleNewDataset={props.toggleNewDataset}
/>
Expand Down
32 changes: 16 additions & 16 deletions client/src/features/project/dataset/ProjectDatasetNewEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { SerializedError } from "@reduxjs/toolkit";
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
import React from "react";
import { useHistory } from "react-router-dom";
import { useLocation, useNavigate } from "react-router-dom-v5-compat";
import { Alert, Button, Col } from "reactstrap";

import { ACCESS_LEVELS } from "../../../api-client";
Expand Down Expand Up @@ -49,8 +49,6 @@ type ChangeDatasetProps = {
apiVersion: string | undefined;
client: DatasetPostClient;
fetchDatasets: PostSubmitProps["fetchDatasets"];
history: ReturnType<typeof useHistory>;
location: { pathname: string };
model: unknown;
notifications: unknown;
metadataVersion: number | undefined;
Expand Down Expand Up @@ -113,7 +111,9 @@ function ProjectDatasetNewEdit(props: ProjectDatasetNewEditProps) {
projectUrlProps
);

const { dataset, history, submitting, setSubmitting } = props;
const { dataset, submitting, setSubmitting } = props;

const navigate = useNavigate();

const onCancel = React.useCallback(() => {
const targetPath = { path: projectPathWithNamespace };
Expand All @@ -123,8 +123,8 @@ function ProjectDatasetNewEdit(props: ProjectDatasetNewEditProps) {
dataset: dataset.slug,
})
: Url.get(Url.pages.project.datasets.base, { ...targetPath });
history.push({ pathname });
}, [dataset, history, projectPathWithNamespace]);
navigate(pathname);
}, [dataset, navigate, projectPathWithNamespace]);

if (accessLevel < ACCESS_LEVELS.MAINTAINER) {
return (
Expand Down Expand Up @@ -178,8 +178,8 @@ function ProjectDatasetNewEdit(props: ProjectDatasetNewEditProps) {
externalUrl={projectMetadata.externalUrl}
fetchDatasets={props.fetchDatasets}
initialized={true}
history={props.history}
location={props.location}
// history={props.history}
// location={props.location}
metadataVersion={props.metadataVersion}
notifications={props.notifications}
onCancel={onCancel}
Expand All @@ -198,7 +198,9 @@ function ProjectDatasetNew(
props: Omit<ChangeDatasetProps, "submitting" | "setSubmitting"> &
ProjectDatasetNewOnlyProps
) {
const location = props.location;
// const location = props.location;
const location = useLocation();

const project = useLegacySelector<StateModelProject>(
(state) => state.stateModel.project
);
Expand Down Expand Up @@ -230,8 +232,8 @@ function ProjectDatasetNew(
apiVersion={props.apiVersion}
client={props.client}
fetchDatasets={props.fetchDatasets}
history={props.history}
location={props.location}
// history={props.history}
// location={props.location}
metadataVersion={props.metadataVersion}
model={props.model}
notifications={props.notifications}
Expand All @@ -253,7 +255,7 @@ function ProjectDatasetEditForm(
DatasetModifyDisplayProps &
ProjectDatasetEditOnlyProps
) {
const location = props.location;
const location = useLocation();
const project = useLegacySelector<StateModelProject>(
(state) => state.stateModel.project
);
Expand All @@ -279,8 +281,6 @@ function ProjectDatasetEditForm(
datasetId={props.datasetId}
fetchDatasets={props.fetchDatasets}
files={files}
history={props.history}
location={props.location}
metadataVersion={props.metadataVersion}
model={props.model}
notifications={props.notifications}
Expand Down Expand Up @@ -341,8 +341,8 @@ function ProjectDatasetEdit(props: ProjectDatasetEditProps) {
datasetId={datasetId}
fetchDatasets={props.fetchDatasets}
files={props.files}
history={props.history}
location={props.location}
// history={props.history}
// location={props.location}
metadataVersion={props.metadataVersion}
model={props.model}
notifications={props.notifications}
Expand Down
16 changes: 8 additions & 8 deletions client/src/features/project/dataset/ProjectDatasetShow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ type ProjectDatasetShowProps = {
datasetCoordinator: unknown;
datasetId: string;
graphStatus: boolean;
history: unknown;
location: unknown;
// history: unknown;
// location: unknown;
model: unknown;
projectInsideKg: boolean;
};
Expand All @@ -63,10 +63,10 @@ type ProjectDatasetViewProps = {
externalUrl: string;
fileContentUrl: string;
graphStatus: boolean;
history: unknown;
// history: unknown;
httpProjectUrl: string;
lineagesUrl: string;
location: unknown;
// location: unknown;
lockStatus: unknown;
logged: unknown;
maintainer: boolean;
Expand Down Expand Up @@ -181,13 +181,13 @@ function ProjectDatasetView(props: ProjectDatasetViewProps) {
fetchError={kgFetchError}
fetchedKg={kgDataset != null}
fileContentUrl={props.fileContentUrl}
history={props.history}
// history={props.history}
httpProjectUrl={props.httpProjectUrl}
identifier={datasetId}
insideProject={true}
lineagesUrl={props.lineagesUrl}
loadingDatasets={loadingDatasets}
location={props.location}
// location={props.location}
lockStatus={props.lockStatus}
logged={props.logged}
maintainer={props.maintainer}
Expand Down Expand Up @@ -238,10 +238,10 @@ function ProjectDatasetShow(props: ProjectDatasetShowProps) {
externalUrl={projectMetadata.externalUrl}
fileContentUrl={fileContentUrl}
graphStatus={props.graphStatus}
history={props.history}
// history={props.history}
httpProjectUrl={httpProjectUrl}
lineagesUrl={lineagesUrl}
location={props.location}
// location={props.location}
lockStatus={lockStatus}
logged={user.logged}
maintainer={maintainer}
Expand Down
Loading

0 comments on commit 9f06c78

Please sign in to comment.