Browse Source

fix: 修正选择仓库后清空库位管理的机制

xyh 2 years ago
parent
commit
306b20a11a

+ 4 - 4
cypress/e2e/matter.cy.ts

@@ -14,11 +14,11 @@ import {
 
 const basicData = {
   id: '5010',
-  storageLocationCode: 'B3-10-4',
+  storageLocationCode: 'D6-3-51',
   wllbCode: '3599020010010141',
   wllbClass: null,
   wllbName: '科密EP-9000 一二维码有线USB扫描枪;科密EP-9000 收银农资',
-  storageLocationName: 'B3-10-4',
+  storageLocationName: 'D6-3-51',
   size: null,
   tldId: null,
   materialType: null,
@@ -77,7 +77,7 @@ describe('库位绑定管理', function () {
     validateExport(TABLE_NAME);
   });
 
-  it('operation', function () {
+  it.only('operation', function () {
     const {validateAdd, validateEdit} = validatePut(
       'matter_modal',
       TABLE_NAME,
@@ -102,6 +102,6 @@ describe('库位绑定管理', function () {
       },
     ]);
 
-    validateDelete(TABLE_NAME, '库位绑定', {title: basicData.wllbName});
+    validateDelete(TABLE_NAME, '库位绑定', {title: 'page-1'});
   });
 });

+ 14 - 3
packages/app/src/hooks/use-options/index.ts

@@ -13,7 +13,7 @@ import {
 } from '@models';
 import {useQuery} from '@tanstack/react-query';
 import {useLatest} from 'ahooks';
-import {useEffect, useMemo, useState} from 'react';
+import {useEffect, useMemo, useRef, useState} from 'react';
 import {debounce} from 'lodash-es';
 
 type Options<T extends {id: number | string}, P extends unknown[]> = {
@@ -68,11 +68,22 @@ export function useStorageOptions(
   findValue?: (state: StorageListData) => string,
   options?: {id: string; clear: () => void},
 ) {
+  const prevId = useRef(options?.id);
+
   useEffect(
     function () {
-      options?.clear();
+      // 如果之前为空 说明是第一次进入 此时不清除
+      if (!prevId.current) {
+        prevId.current = options?.id;
+        return;
+      }
+
+      if (prevId.current !== options?.id) {
+        prevId.current = options?.id;
+        options?.clear();
+      }
     },
-    [options?.clear, options?.id],
+    [options],
   );
 
   return useQueryOptions({

+ 2 - 2
packages/app/src/pages/matter/table/hooks.tsx

@@ -58,7 +58,7 @@ export function useHandle(refetch: () => void) {
       key: 'id',
       width: DOUBLE_BTN_WIDTH,
       fixed: 'right',
-      render(_, {id, wllbName}) {
+      render(_, {id, wllbCode}) {
         return (
           <>
             <Button
@@ -73,7 +73,7 @@ export function useHandle(refetch: () => void) {
               type='text'
               danger
               loading={id === pendingId}
-              onClick={onDelete(id, wllbName)}
+              onClick={onDelete(id, wllbCode)}
             >
               删除
             </Button>

+ 2 - 3
packages/app/src/pages/matter/table/modal/Info.tsx

@@ -10,12 +10,13 @@ import {
 type Props = {id: string};
 
 const Info: FC<Props> = function ({id}) {
-  const {control, watch, setValue} = useFormContextState();
   const [{data: materialOptions, isFetching}, onMaterialSearch] =
     useDictionaryWidthCode('物料字典', {
       findValue: state => state.code,
     });
   useWatchId(id, onMaterialSearch);
+
+  const {control, watch, setValue} = useFormContextState();
   const {data: warehouseOptions, isFetching: warehouseIsFetching} =
     useDictionaryOptions('仓库');
   const warehouseId = watch('warehouseId');
@@ -42,7 +43,6 @@ const Info: FC<Props> = function ({id}) {
         label='所属仓库'
         control={control}
         data={warehouseOptions}
-        showSearch
         loading={warehouseIsFetching}
       />
       <ModalSelect
@@ -50,7 +50,6 @@ const Info: FC<Props> = function ({id}) {
         label='所属库位'
         control={control}
         data={storageOtions}
-        showSearch
         loading={isStorageFetching}
       />
     </>

+ 8 - 3
packages/app/src/pages/matter/table/modal/hooks.ts

@@ -37,13 +37,18 @@ export function useFormState({
     },
     resolver: yupResolver(validate),
   });
-  const {clearErrors, handleSubmit} = formInstance;
+  const {clearErrors, handleSubmit, reset} = formInstance;
 
   useEffect(
     function () {
-      visible && clearErrors();
+      if (!visible) {
+        clearErrors();
+        // 关闭modal后所有值重置
+        // 确保第二次进入后仓库id为空 不会触发库位id清空
+        reset();
+      }
     },
-    [clearErrors, visible],
+    [clearErrors, reset, visible],
   );
 
   const [isLoading, {addMutate, editMutate}] = usePutData({