|
@@ -0,0 +1,104 @@
|
|
|
+import {getNoticeInfo, semiManufacturesAdd} from '@apis';
|
|
|
+import {yupResolver} from '@hookform/resolvers/yup';
|
|
|
+import {useQueryDataInfo} from '@hooks';
|
|
|
+import {NoticeListData} from '@models';
|
|
|
+import {userStore} from '@stores';
|
|
|
+import {useMutation, useQueryClient} from '@tanstack/react-query';
|
|
|
+import {message} from 'antd';
|
|
|
+import {useEffect} from 'react';
|
|
|
+import {useForm, useFormContext} from 'react-hook-form';
|
|
|
+import {number, object} from 'yup';
|
|
|
+import {useStore} from 'zustand';
|
|
|
+
|
|
|
+type FormState = {
|
|
|
+ putInStoragenum: number,
|
|
|
+};
|
|
|
+
|
|
|
+const validate = object({
|
|
|
+ putInStoragenum: number().typeError('请输入数字').min(1, '不能少于1个'),
|
|
|
+});
|
|
|
+
|
|
|
+function useQuery(onClose: () => void, onFetch: () => void) {
|
|
|
+ return useMutation({
|
|
|
+ mutationFn: semiManufacturesAdd,
|
|
|
+ onSuccess({msg}) {
|
|
|
+ if (msg === '200') {
|
|
|
+ onClose();
|
|
|
+ onFetch();
|
|
|
+ message.success('入库成功');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+function useInfoData(id: string) {
|
|
|
+ const client = useQueryClient();
|
|
|
+
|
|
|
+ function getData() {
|
|
|
+ return client.getQueryData<NoticeListData>([getNoticeInfo.name, id]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return getData;
|
|
|
+}
|
|
|
+
|
|
|
+export function useFormState(
|
|
|
+ {id, visible, onClose, onFetch}:
|
|
|
+ {id: string, visible: boolean, onClose: () => void, onFetch: () => void},
|
|
|
+) {
|
|
|
+ const formInstance = useForm<FormState>({
|
|
|
+ defaultValues: {
|
|
|
+ putInStoragenum: 0,
|
|
|
+ },
|
|
|
+ resolver: yupResolver(validate),
|
|
|
+ });
|
|
|
+ const {control, handleSubmit, clearErrors} = formInstance;
|
|
|
+
|
|
|
+ useEffect(function() {
|
|
|
+ if (visible)
|
|
|
+ clearErrors();
|
|
|
+ }, [clearErrors, visible]);
|
|
|
+
|
|
|
+ const {mutate, isLoading} = useQuery(onClose, onFetch);
|
|
|
+ const getInfoData = useInfoData(id);
|
|
|
+ const userId = useStore(userStore, state => String(state.id));
|
|
|
+
|
|
|
+ const onSubmit = handleSubmit(function({putInStoragenum}) {
|
|
|
+ const info = getInfoData();
|
|
|
+
|
|
|
+ if (!info)
|
|
|
+ message.error('未获取到信息');
|
|
|
+ else {
|
|
|
+ const {wllbClass, companyNumber, wbs, materialId, noticeId} = info;
|
|
|
+ mutate({
|
|
|
+ userId,
|
|
|
+ companyNumber,
|
|
|
+ wbs,
|
|
|
+ wllbClass,
|
|
|
+ noticeId,
|
|
|
+ materialId,
|
|
|
+ warehousingNum: String(putInStoragenum),
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return [{control, isLoading, formInstance}, {onSubmit}] as const;
|
|
|
+}
|
|
|
+
|
|
|
+export function useControl() {
|
|
|
+ const {control} = useFormContext<FormState>();
|
|
|
+
|
|
|
+ return control;
|
|
|
+}
|
|
|
+
|
|
|
+export function useWatchId(id: string) {
|
|
|
+ const {setValue} = useFormContext<FormState>();
|
|
|
+ const data = useQueryDataInfo({
|
|
|
+ queryFn: getNoticeInfo,
|
|
|
+ params: [id],
|
|
|
+ enabled: Boolean(id),
|
|
|
+ });
|
|
|
+
|
|
|
+ useEffect(function() {
|
|
|
+ setValue('putInStoragenum', Number(data?.num ?? '0'));
|
|
|
+ }, [data, setValue]);
|
|
|
+}
|