Commit 00f04378 authored by XieZhiXiong's avatar XieZhiXiong

fix: 修复设置默认地址后没有刷新地址列表的问题

parent bed2cfcb
......@@ -2,10 +2,10 @@
* @Author: XieZhiXiong
* @Date: 2021-08-05 14:23:40
* @LastEditors: XieZhiXiong
* @LastEditTime: 2021-08-09 16:53:25
* @LastEditTime: 2021-08-18 11:04:54
* @Description: 地址单选框组
*/
import React, { useState, useEffect, useMemo } from 'react';
import React, { useState, useEffect, useMemo, useRef } from 'react';
import { Radio, Button, Modal, message, RadioChangeEvent } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { PublicApi } from '@/services/api';
......@@ -86,6 +86,9 @@ const AddressRadioGroup: React.FC<IProps> = (props) => {
const context = React.useContext(AddressSelectContext);
// 记录是否是新增或编辑操作
const actionFlagRef = useRef<boolean>(false);
const triggerChange = (value: AddressValueType) => {
if (onChange) {
onChange(value);
......@@ -123,7 +126,7 @@ const AddressRadioGroup: React.FC<IProps> = (props) => {
triggerChange(undefined);
}
if (isDefaultAddress && defaultItem) {
if (isDefaultAddress && defaultItem && !actionFlagRef.current) {
const { shipperName, receiverName, ...rest } = defaultItem;
const next = {
name: shipperName || receiverName,
......@@ -141,8 +144,15 @@ const AddressRadioGroup: React.FC<IProps> = (props) => {
});
};
const refresh = () => {
context ? context?.refresh() : getAddressList();
const refresh = (actionFlag?: boolean) => {
if (context) {
context?.refresh(actionFlag);
} {
// 重新获取列表,把新添加的内容加载出来
// 不触发默认值提交
actionFlagRef.current = true;
getAddressList();
}
};
useEffect(() => {
......@@ -224,7 +234,7 @@ const AddressRadioGroup: React.FC<IProps> = (props) => {
try {
const res = addressType === 2 ? await PublicApi.getLogisticsShipperAddressGet({ id: `${id}`}) : await PublicApi.getLogisticsReceiverAddressGet({ id: `${id}`});
if (res.code === 1000) {
addressType === 2
const updateRes = addressType === 2
? await PublicApi.postLogisticsShipperAddressUpdate({
...(res.data as GetLogisticsShipperAddressGetResponse),
isDefault: 1,
......@@ -233,6 +243,9 @@ const AddressRadioGroup: React.FC<IProps> = (props) => {
...(res.data as GetLogisticsReceiverAddressGetResponse),
isDefault: 1,
});
if (updateRes.code === 1000) {
refresh(true);
}
}
} catch (error) {
console.warn(error);
......
......@@ -256,7 +256,7 @@ const AddressSelect: React.FC<IProps> = (props) => {
name: values.name,
phone: values.phone,
};
// 重新获取列表,新添加的内容加载出来
// 重新获取列表,新添加的内容加载出来
// 不触发默认值提交
actionFlagRef.current = true;
getAddressList();
......@@ -295,7 +295,7 @@ const AddressSelect: React.FC<IProps> = (props) => {
name: values.name,
phone: values.phone,
};
// 重新获取列表,新添加的内容加载出来
// 重新获取列表,新添加的内容加载出来
// 不触发默认值提交
actionFlagRef.current = true;
getAddressList();
......@@ -437,6 +437,11 @@ const AddressSelect: React.FC<IProps> = (props) => {
}
};
const onRefresh = (actionFlag?: boolean) => {
actionFlagRef.current = !!actionFlag;
getAddressList();
};
const options = useMemo(() => {
return list.map((item) => ({
value: item.id,
......@@ -448,7 +453,7 @@ const AddressSelect: React.FC<IProps> = (props) => {
<AddressSelectContextProvider
value={{
addressList: list,
refresh: getAddressList,
refresh: onRefresh,
}}
>
<div className={styles['address-select']}>
......
......@@ -15,5 +15,5 @@ export interface AddressSelectContextProps {
/**
* 重新加载列表
*/
refresh: () => void,
refresh: (actionFlag?: boolean) => void,
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment