use thunk to get targetName

This commit is contained in:
felinae98 2022-10-09 21:09:17 +08:00
parent af002ad3e5
commit 8db0ed3fe1
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
2 changed files with 21 additions and 16 deletions

View File

@ -4,7 +4,7 @@ import {
} from '@arco-design/web-react';
import useForm from '@arco-design/web-react/es/Form/useForm';
import { IconInfoCircle } from '@arco-design/web-react/icon';
import { useAppSelector } from '../../app/hooks';
import { useAppDispatch, useAppSelector } from '../../app/hooks';
import { selectPlatformConf } from '../globalConf/globalConfSlice';
import { CategoryConfig, SubscribeConfig } from '../../utils/type';
import getTargetName from '../targetName/targetNameReq';
@ -67,6 +67,7 @@ function SubscribeModal({
const platformConf = useAppSelector(selectPlatformConf);
const [updateSub] = useUpdateSubMutation();
const [newSub] = useNewSubMutation();
const dispatch = useAppDispatch();
const onSubmit = () => {
form.validate().then((value: SubscribeConfig) => {
@ -115,7 +116,7 @@ function SubscribeModal({
setPlatformStates(platform);
form.setFieldValue('cats', []);
if (!platformConf[platform].hasTarget) {
getTargetName(platform, 'default').then((res) => {
dispatch(getTargetName(platform, 'default')).then((res) => {
form.setFieldsValue({
targetName: res,
target: '',
@ -168,7 +169,7 @@ function SubscribeModal({
{ required: hasTarget, message: '请输入账号' },
{
validator: (value, callback) => new Promise<void>((resolve) => {
getTargetName(form.getFieldValue('platformName'), value)
dispatch(getTargetName(form.getFieldValue('platformName'), value))
.then((res) => {
if (res) {
form.setFieldsValue({

View File

@ -1,15 +1,19 @@
import { RootState, store } from '../../app/store';
import { AppThunk } from '../../app/store';
import { baseUrl } from '../../utils/urls';
export default async function getTargetName(platformName: string, target: string) {
const url = `${baseUrl}target_name?platformName=${platformName}&target=${target}`;
const state = store.getState() as RootState;
const authToken = state.auth.token;
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
const resObj = await res.json();
return resObj.targetName as string;
}
// eslint-disable-next-line
export const getTargetName =
(platformName: string, target: string): AppThunk<Promise<string>> => async (_, getState) => {
const url = `${baseUrl}target_name?platformName=${platformName}&target=${target}`;
const state = getState();
const authToken = state.auth.token;
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
const resObj = await res.json();
return resObj.targetName as string;
};
export default getTargetName;