| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import {otherOut} from '@apis';
- import {yupResolver} from '@hookform/resolvers/yup';
- import {userStore} from '@stores';
- import {useMutation} from '@tanstack/react-query';
- import {message} from 'antd';
- import {FormEvent} from 'react';
- import {useForm} from 'react-hook-form';
- import {number, object, string} from 'yup';
- import {useStore} from 'zustand';
- type FormState = {
- wllbCode: string;
- storageLocationCode: string;
- num: number;
- wbs: string;
- accountSleeve: string;
- warehouse: string;
- };
- const validate = object({
- wllbCode: string().required('请输入物料编号'),
- warehouse: string().required('请选择出库仓库'),
- storageLocationCode: string().required('请选择出库库位'),
- wbs: string().required('请输入wbs编号'),
- accountSleeve: string().required('请选择所属公司'),
- num: number().typeError('请输入出库数量').min(1, '出库数量不能小于1个'),
- });
- export function useFormState() {
- const {control, reset, handleSubmit, clearErrors, watch} = useForm<FormState>({
- resolver: yupResolver(validate),
- defaultValues: {
- wllbCode: '',
- storageLocationCode: '',
- num: 1,
- warehouse: '',
- wbs: '',
- accountSleeve: '',
- },
- });
- function onReset(e: FormEvent) {
- e.preventDefault();
- reset();
- }
- const {mutate, isLoading} = useMutation({
- mutationFn: otherOut,
- onSuccess({msg}) {
- if (msg === '200') {
- reset();
- clearErrors();
- message.success('出库成功');
- }
- },
- });
- const {userId, department} = useStore(userStore, state => ({
- userId: String(state.id),
- department: state.department,
- }));
- const onSubmit = handleSubmit(function ({
- wllbCode,
- storageLocationCode,
- num,
- wbs,
- accountSleeve,
- }) {
- mutate({
- userId,
- department,
- wllbCode,
- storageLocationCode,
- num: String(num),
- wbs,
- accountSleeve,
- });
- });
- return [
- {isLoading, control},
- {onSubmit, onReset, watch},
- ] as const;
- }
|