Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify dialog handling #296

Merged
merged 2 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add ability to reset own password - #289 by @dominik-zeglen
- Move mutation state to mutation - #297 by @dominik-zeglen
- Add table sorting - #292 by @dominik-zeglen
- Unify dialog handling - #296 by @dominik-zeglen

## 2.0.0

Expand Down
39 changes: 17 additions & 22 deletions src/attributes/views/AttributeCreate/AttributeCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
remove,
updateAtIndex
} from "@saleor/utils/lists";
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
import AttributePage from "../../components/AttributePage";
import AttributeValueDeleteDialog from "../../components/AttributeValueDeleteDialog";
import AttributeValueEditDialog, {
Expand All @@ -22,10 +23,10 @@ import { AttributeCreateMutation } from "../../mutations";
import { AttributeCreate } from "../../types/AttributeCreate";
import {
attributeAddUrl,
AttributeAddUrlDialog,
AttributeAddUrlQueryParams,
attributeListUrl,
attributeUrl
attributeUrl,
AttributeAddUrlDialog
} from "../../urls";

interface AttributeDetailsProps {
Expand All @@ -51,24 +52,10 @@ const AttributeDetails: React.FC<AttributeDetailsProps> = ({ params }) => {

const id = params.id ? parseInt(params.id, 0) : undefined;

const closeModal = () =>
navigate(
attributeAddUrl({
...params,
action: undefined,
id: undefined
}),
true
);

const openModal = (action: AttributeAddUrlDialog, valueId?: string) =>
navigate(
attributeAddUrl({
...params,
action,
id: valueId
})
);
const [openModal, closeModal] = createDialogActionHandlers<
AttributeAddUrlDialog,
AttributeAddUrlQueryParams
>(navigate, attributeAddUrl, params);

const handleValueDelete = () => {
setValues(remove(values[params.id], values, areValuesEqual));
Expand Down Expand Up @@ -159,9 +146,17 @@ const AttributeDetails: React.FC<AttributeDetailsProps> = ({ params }) => {
})
}
onValueAdd={() => openModal("add-value")}
onValueDelete={id => openModal("remove-value", id)}
onValueDelete={id =>
openModal("remove-value", {
id
})
}
onValueReorder={handleValueReorder}
onValueUpdate={id => openModal("edit-value", id)}
onValueUpdate={id =>
openModal("edit-value", {
id
})
}
saveButtonBarState={attributeCreateOpts.status}
values={values.map((value, valueIndex) => ({
__typename: "AttributeValue" as "AttributeValue",
Expand Down
36 changes: 13 additions & 23 deletions src/attributes/views/AttributeDetails/AttributeDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { commonMessages } from "@saleor/intl";
import { maybe } from "@saleor/misc";
import { ReorderEvent } from "@saleor/types";
import { move } from "@saleor/utils/lists";
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
import AttributeDeleteDialog from "../../components/AttributeDeleteDialog";
import AttributePage from "../../components/AttributePage";
import AttributeValueDeleteDialog from "../../components/AttributeValueDeleteDialog";
Expand All @@ -29,8 +30,8 @@ import { AttributeValueUpdate } from "../../types/AttributeValueUpdate";
import {
attributeListUrl,
attributeUrl,
AttributeUrlDialog,
AttributeUrlQueryParams
AttributeUrlQueryParams,
AttributeUrlDialog
} from "../../urls";

interface AttributeDetailsProps {
Expand All @@ -43,25 +44,10 @@ const AttributeDetails: React.FC<AttributeDetailsProps> = ({ id, params }) => {
const notify = useNotifier();
const intl = useIntl();

const closeModal = () =>
navigate(
attributeUrl(id, {
...params,
action: undefined,
id: undefined,
ids: undefined
}),
true
);

const openModal = (action: AttributeUrlDialog, valueId?: string) =>
navigate(
attributeUrl(id, {
...params,
action,
id: valueId
})
);
const [openModal, closeModal] = createDialogActionHandlers<
AttributeUrlDialog,
AttributeUrlQueryParams
>(navigate, params => attributeUrl(id, params), params);

const handleDelete = (data: AttributeDelete) => {
if (data.attributeDelete.errors.length === 0) {
Expand Down Expand Up @@ -200,11 +186,15 @@ const AttributeDetails: React.FC<AttributeDetailsProps> = ({ id, params }) => {
}}
onValueAdd={() => openModal("add-value")}
onValueDelete={id =>
openModal("remove-value", id)
openModal("remove-value", {
id
})
}
onValueReorder={handleValueReorder}
onValueUpdate={id =>
openModal("edit-value", id)
openModal("edit-value", {
id
})
}
saveButtonBarState={
attributeUpdateOpts.status
Expand Down
33 changes: 12 additions & 21 deletions src/attributes/views/AttributeList/AttributeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import usePaginator, {
} from "@saleor/hooks/usePaginator";
import { getSortParams } from "@saleor/utils/sort";
import createSortHandler from "@saleor/utils/handlers/sortHandler";
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
import { PAGINATE_BY } from "../../../config";
import useBulkActions from "../../../hooks/useBulkActions";
import { maybe } from "../../../misc";
Expand All @@ -34,10 +35,10 @@ import { AttributeBulkDelete } from "../../types/AttributeBulkDelete";
import {
attributeAddUrl,
attributeListUrl,
AttributeListUrlDialog,
AttributeListUrlFilters,
AttributeListUrlQueryParams,
attributeUrl
attributeUrl,
AttributeListUrlDialog
} from "../../urls";
import { getSortQueryVariables } from "./sort";

Expand Down Expand Up @@ -76,24 +77,10 @@ const AttributeList: React.FC<AttributeListProps> = ({ params }) => {
: 0
: parseInt(params.activeTab, 0);

const closeModal = () =>
navigate(
attributeListUrl({
...params,
action: undefined,
ids: undefined
}),
true
);

const openModal = (action: AttributeListUrlDialog, ids?: string[]) =>
navigate(
attributeListUrl({
...params,
action,
ids
})
);
const [openModal, closeModal] = createDialogActionHandlers<
AttributeListUrlDialog,
AttributeListUrlQueryParams
>(navigate, attributeListUrl, params);

const changeFilterField = (filter: AttributeListUrlFilters) => {
reset();
Expand Down Expand Up @@ -181,7 +168,11 @@ const AttributeList: React.FC<AttributeListProps> = ({ params }) => {
toolbar={
<IconButton
color="primary"
onClick={() => openModal("remove", listElements)}
onClick={() =>
openModal("remove", {
ids: listElements
})
}
>
<DeleteIcon />
</IconButton>
Expand Down
39 changes: 17 additions & 22 deletions src/categories/views/CategoryDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import usePaginator, {
createPaginationState
} from "@saleor/hooks/usePaginator";
import { commonMessages } from "@saleor/intl";
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
import { PAGINATE_BY } from "../../config";
import { maybe } from "../../misc";
import { TypedProductBulkDeleteMutation } from "../../products/mutations";
Expand All @@ -36,8 +37,8 @@ import {
categoryAddUrl,
categoryListUrl,
categoryUrl,
CategoryUrlDialog,
CategoryUrlQueryParams
CategoryUrlQueryParams,
CategoryUrlDialog
} from "../urls";

export interface CategoryDetailsProps {
Expand Down Expand Up @@ -128,24 +129,10 @@ export const CategoryDetails: React.FC<CategoryDetailsProps> = ({
);
};

const closeModal = () =>
navigate(
categoryUrl(id, {
...params,
action: undefined,
ids: undefined
}),
true
);

const openModal = (action: CategoryUrlDialog, ids?: string[]) =>
navigate(
categoryUrl(id, {
...params,
action,
ids
})
);
const [openModal, closeModal] = createDialogActionHandlers<
CategoryUrlDialog,
CategoryUrlQueryParams
>(navigate, params => categoryUrl(id, params), params);

const handleBulkProductDelete = (data: productBulkDelete) => {
if (data.productBulkDelete.errors.length === 0) {
Expand Down Expand Up @@ -240,15 +227,23 @@ export const CategoryDetails: React.FC<CategoryDetailsProps> = ({
subcategoryListToolbar={
<IconButton
color="primary"
onClick={() => openModal("delete-categories", listElements)}
onClick={() =>
openModal("delete-categories", {
ids: listElements
})
}
>
<DeleteIcon />
</IconButton>
}
productListToolbar={
<IconButton
color="primary"
onClick={() => openModal("delete-products", listElements)}
onClick={() =>
openModal("delete-products", {
ids: listElements
})
}
>
<DeleteIcon />
</IconButton>
Expand Down
37 changes: 10 additions & 27 deletions src/categories/views/CategoryList/CategoryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ import { maybe } from "@saleor/misc";
import { ListViews } from "@saleor/types";
import { getSortParams } from "@saleor/utils/sort";
import createSortHandler from "@saleor/utils/handlers/sortHandler";
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
import { CategoryListPage } from "../../components/CategoryListPage/CategoryListPage";
import { useCategoryBulkDeleteMutation } from "../../mutations";
import { useRootCategoriesQuery } from "../../queries";
import { CategoryBulkDelete } from "../../types/CategoryBulkDelete";
import {
categoryAddUrl,
categoryListUrl,
CategoryListUrlDialog,
CategoryListUrlFilters,
CategoryListUrlQueryParams,
categoryUrl
categoryUrl,
CategoryListUrlDialog
} from "../../urls";
import {
areFiltersApplied,
Expand Down Expand Up @@ -91,24 +92,10 @@ export const CategoryList: React.FC<CategoryListProps> = ({ params }) => {
);
};

const closeModal = () =>
navigate(
categoryListUrl({
...params,
action: undefined,
ids: undefined
}),
true
);

const openModal = (action: CategoryListUrlDialog, ids?: string[]) =>
navigate(
categoryListUrl({
...params,
action,
ids
})
);
const [openModal, closeModal] = createDialogActionHandlers<
CategoryListUrlDialog,
CategoryListUrlQueryParams
>(navigate, categoryListUrl, params);

const handleTabChange = (tab: number) => {
reset();
Expand Down Expand Up @@ -187,13 +174,9 @@ export const CategoryList: React.FC<CategoryListProps> = ({ params }) => {
<IconButton
color="primary"
onClick={() =>
navigate(
categoryListUrl({
...params,
action: "delete",
ids: listElements
})
)
openModal("delete", {
ids: listElements
})
}
>
<DeleteIcon />
Expand Down
Loading