Skip to content

Commit

Permalink
OH2-305 | Suppliers create and update (#596)
Browse files Browse the repository at this point in the history
* feature/OH2-305 | Supplier creation and edit

* fix:Rename route paths
  • Loading branch information
SilverD3 authored Jun 5, 2024
1 parent 876b088 commit bc434ff
Show file tree
Hide file tree
Showing 21 changed files with 758 additions and 7 deletions.
32 changes: 31 additions & 1 deletion src/components/accessories/admin/suppliers/Suppliers.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
import React from "react";

import SuppliersTable from "./suppliersTable";
import { useNavigate } from "react-router";
import { useTranslation } from "react-i18next";
import { SupplierDTO } from "../../../../generated";
import { PATHS } from "../../../../consts";
import Button from "../../button/Button";

export const Suppliers = () => {
return <SuppliersTable />;
const navigate = useNavigate();
const { t } = useTranslation();

const handleEdit = (row: SupplierDTO) => {
navigate(PATHS.admin_suppliers_edit.replace(":id", `${row.supId}`), {
state: row,
});
};

return (
<SuppliersTable
onEdit={handleEdit}
headerActions={
<Button
onClick={() => {
navigate(PATHS.admin_suppliers_new);
}}
type="button"
variant="contained"
color="primary"
>
{t("supplier.addSupplier")}
</Button>
}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch, useSelector } from "react-redux";
import { SupplierDTO } from "../../../../../generated";
import { ApiResponse } from "../../../../../state/types";
import { IState } from "../../../../../types";
import { useLocation, useNavigate, useParams } from "react-router";
import { PATHS } from "../../../../../consts";
import SupplierForm from "../supplierForm/SupplierForm";
import { getInitialFields } from "../supplierForm/consts";
import { updateSupplier } from "../../../../../state/suppliers/actions";

export const EditSupplier = () => {
const dispatch = useDispatch();
const { t } = useTranslation();
const { state }: { state: SupplierDTO | undefined } = useLocation();
const { id } = useParams();
const update = useSelector<IState, ApiResponse<SupplierDTO>>(
(state) => state.suppliers.update
);
const navigate = useNavigate();

const handleSubmit = (value: SupplierDTO) => {
dispatch(updateSupplier(value));
};

useEffect(() => {
if (state?.supId !== Number(id)) {
navigate(PATHS.admin_suppliers);
}
}, [id, state]);

return (
<SupplierForm
creationMode={false}
onSubmit={handleSubmit}
isLoading={!!update.isLoading}
resetButtonLabel={t("common.cancel")}
submitButtonLabel={t("supplier.updateSupplier")}
fields={getInitialFields(state)}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./EditSupplier";
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useTranslation } from "react-i18next";
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { SupplierDTO } from "../../../../../generated";
import { IState } from "../../../../../types";
import { ApiResponse } from "../../../../../state/types";
import SupplierForm from "../supplierForm/SupplierForm";
import { getInitialFields } from "../supplierForm/consts";
import { createSupplier } from "../../../../../state/suppliers/actions";

export const NewSupplier = () => {
const dispatch = useDispatch();
const { t } = useTranslation();
const create = useSelector<IState, ApiResponse<SupplierDTO>>(
(state) => state.suppliers.create
);

const handleSubmit = (value: SupplierDTO) => {
dispatch(createSupplier(value));
};

return (
<SupplierForm
creationMode
onSubmit={handleSubmit}
isLoading={!!create.isLoading}
resetButtonLabel={t("common.cancel")}
submitButtonLabel={t("supplier.saveSupplier")}
fields={getInitialFields(undefined)}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./NewSupplier";
Loading

0 comments on commit bc434ff

Please sign in to comment.