如何重构此调用刷新状态

发布于 2025-02-03 05:27:49 字数 2684 浏览 1 评论 0原文

我想知道如何改善此调用,以免始终重复相同的句子以刷新状态...
我不需要任何巨大的重构,只有类似的输入:您需要将此呼叫放入功能中,并在需要时给它打电话...类似的东西...

export const CategoriesPage = () => {
  const [categories, setCategories] = useState<Category[]>([]);
  const [showModal, setShowModal] = useState(false);

  const handleCreateCategory = (newCategory: CategoryCreate, file: File) => {
    createCategoryHelper(newCategory, file)
      .then(() => {
        getCategoriesHelper().then(setCategories);
      })
      .finally(() => handleClose());
  };

  const handleDeleteCategory = (categoryId: Id) => {
    SwalHelper.delete().then(() => {
      deleteCategoryHelper(categoryId).then(() =>
        getCategoriesHelper().then(setCategories)
      );
    });
  };

  const handleClose = () => {
    setShowModal(false);
  };

  const handleModal = () => {
    setShowModal(true);
  };

  useEffect(() => {
    getCategoriesHelper().then(setCategories);
  }, []);

  return (
    <>
      <PageTitle title="Categories" />
      <FilterBar>
        <Button type="button" background="green" onClick={handleModal}>
          + Add new
        </Button>
      </FilterBar>
      {showModal && (
        <ModalPortal onClose={handleClose}>
          <CreateCategoryForm
            createCategory={(category, file: File) => {
              handleCreateCategory(category, file);
            }}
          />
        </ModalPortal>
      )}

      <ListGrid columns={3}>
        {categories.map((category) => {
          const { id: categoryId } = category;
          return (
            <CategoryCard
              key={categoryId}
              {...category}
              onClick={() => handleDeleteCategory(categoryId)}
            />
          );
        })}
      </ListGrid>
    </>
  );
};

  1. 当组件为 安装 ,在使用效果上,为了创建列表而填充状态。
  2. 当类别是 创建 时,我再次致电SetState以刷新列表。
  3. delete 上相同,然后再次刷新以更新列表。

三次调用同一句子

getCategoriesHelper().then(setCategories)

这是GetCategoriesHelper:

export const getCategoriesHelper = async () => {
  const service = new CategoryServiceImplementation(apiConfig);
  const uploadImageService = new AmplifyS3Service();
  const repository = new CategoryRepositoryImplementation(
    service,
    uploadImageService
  );
  const useCase = new GetCategoriesUseCaseImplementation(repository);
  return await useCase.getCategories();
};

有什么方法可以使此代码更加干净,可重复使用?
提前致谢!

I want to know how improve this calls in order to not repeat always the same sentence to refresh the state...
I don't need any huge refactor, only inputs like: you need to put this call inside a function and call it when you want... something like this...

export const CategoriesPage = () => {
  const [categories, setCategories] = useState<Category[]>([]);
  const [showModal, setShowModal] = useState(false);

  const handleCreateCategory = (newCategory: CategoryCreate, file: File) => {
    createCategoryHelper(newCategory, file)
      .then(() => {
        getCategoriesHelper().then(setCategories);
      })
      .finally(() => handleClose());
  };

  const handleDeleteCategory = (categoryId: Id) => {
    SwalHelper.delete().then(() => {
      deleteCategoryHelper(categoryId).then(() =>
        getCategoriesHelper().then(setCategories)
      );
    });
  };

  const handleClose = () => {
    setShowModal(false);
  };

  const handleModal = () => {
    setShowModal(true);
  };

  useEffect(() => {
    getCategoriesHelper().then(setCategories);
  }, []);

  return (
    <>
      <PageTitle title="Categories" />
      <FilterBar>
        <Button type="button" background="green" onClick={handleModal}>
          + Add new
        </Button>
      </FilterBar>
      {showModal && (
        <ModalPortal onClose={handleClose}>
          <CreateCategoryForm
            createCategory={(category, file: File) => {
              handleCreateCategory(category, file);
            }}
          />
        </ModalPortal>
      )}

      <ListGrid columns={3}>
        {categories.map((category) => {
          const { id: categoryId } = category;
          return (
            <CategoryCard
              key={categoryId}
              {...category}
              onClick={() => handleDeleteCategory(categoryId)}
            />
          );
        })}
      </ListGrid>
    </>
  );
};

  1. When component is mounting, on useEffect, fills the state with response in order to create a list.
  2. When a category is created, I call to setState again to refresh the list.
  3. Same on delete, on then, refresh again to update the list.

Three times calling the same sentence

getCategoriesHelper().then(setCategories)

This is getCategoriesHelper:

export const getCategoriesHelper = async () => {
  const service = new CategoryServiceImplementation(apiConfig);
  const uploadImageService = new AmplifyS3Service();
  const repository = new CategoryRepositoryImplementation(
    service,
    uploadImageService
  );
  const useCase = new GetCategoriesUseCaseImplementation(repository);
  return await useCase.getCategories();
};

Is there any way to make this code much cleaner and reusable?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2025-02-10 05:27:50

一切都是写的,所有呼叫都是旨在做的

Everything is write, and all calls are made as they are designed to do

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文