Skip to content

Commit

Permalink
fix: prevent duplicated submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
xsteadybcgo committed Aug 27, 2021
1 parent e045a26 commit b2d2e86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/pages/Projects/SettingField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const SettingField: React.ForwardRefRenderFunction<unknown, ISettingField> = (
const [editable, setEditable] = useState(false)
const [errorMsg, setErrorMsg] = useState('')
const [value, setValue] = useState(defaultValue)
const canSubmit = useRef(true)
const intRef = useRef<HTMLInputElement>(null!)
useImperativeHandle<unknown, IRefReturnType>(ref, () => ({ value: value }), [
value,
Expand All @@ -32,12 +33,18 @@ const SettingField: React.ForwardRefRenderFunction<unknown, ISettingField> = (
}, [defaultValue])

const onHandleConfirm = () => {
handleConfirm().then((errMsg) => {
if (!errMsg) {
setEditable(false)
}
setErrorMsg(errMsg)
})
if (!canSubmit.current) return
canSubmit.current = false
handleConfirm()
.then((errMsg) => {
if (!errMsg) {
setEditable(false)
}
setErrorMsg(errMsg)
})
.finally(() => {
canSubmit.current = true
})
}

return (
Expand Down
7 changes: 6 additions & 1 deletion src/pages/Projects/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Projects: FC<{}> = () => {
const [tabNum, setTabNum] = useState(0)
const [viewType, switchToView] = useState<'setting' | 'request'>('request')
const [projectInfo, setProjectInfo] = useState<Project[]>([])
const delBtnIsDisabled = useRef(false)
const [deleteModalVisible, setDeleteModalVisible] = useState(false)
const { updateMenu } = useContext(DashboardContext)
const nameRef = useRef<IRefReturnType>(null)
Expand Down Expand Up @@ -88,6 +89,8 @@ const Projects: FC<{}> = () => {
}

const handleDelProject = () => {
if (delBtnIsDisabled.current) return
delBtnIsDisabled.current = true
apiDelProject({ id: projectInfo[tabNum].id }).then(
() => {
message.success(t('tip.delete'))
Expand All @@ -100,7 +103,9 @@ const Projects: FC<{}> = () => {
() => {
message.success(t('tip.fail'))
}
)
).finally(() => {
delBtnIsDisabled.current = false
})
setDeleteModalVisible(false)
}

Expand Down
11 changes: 8 additions & 3 deletions src/shared/components/CreateProjectBtn/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dispatch, FC, useState, useContext } from 'react'
import { Dispatch, FC, useState, useContext, useRef } from 'react'
import { message, Modal, Select } from 'antd'
import { apiCreateProject } from '../../../core/data/api'
import { useTranslation } from 'react-i18next'
Expand All @@ -15,11 +15,11 @@ const CreateProjectModal: FC<{
setVisible: Dispatch<boolean>
closeCallBack?: () => void
}> = ({ visible = false, chain, setVisible, closeCallBack }) => {

const [projectName, setProjectName] = useState('')
const [selectedTeam, setSelectTeam] = useState('')
const [selectedChain, setSelectChain] = useState('')
const [isValidProjectName, setIsValid] = useState(true)
const createBtnIsDisabled = useRef(false)
const { t } = useTranslation()
const { user, updateUser } = useApi()
const { chains, updateMenu } = useContext(DashboardContext)
Expand Down Expand Up @@ -48,7 +48,7 @@ const CreateProjectModal: FC<{
setSelectChain('')
}

const _generateTeamOfChain = (chain?:string) => {
const _generateTeamOfChain = (chain?: string) => {
for (let type in chains) {
for (let i = 0; i < chains[type].length; i++) {
if (chains[type][i].name === chain) {
Expand All @@ -60,6 +60,8 @@ const CreateProjectModal: FC<{
}

const _createProject = () => {
if (createBtnIsDisabled.current) return
createBtnIsDisabled.current = true
apiCreateProject({
userId: user.id,
chain: chain || selectedChain,
Expand All @@ -78,6 +80,9 @@ const CreateProjectModal: FC<{
.catch((res) => {
message.error(t(res.msg))
})
.finally(() => {
createBtnIsDisabled.current = false
})
setVisible(false)
}

Expand Down

0 comments on commit b2d2e86

Please sign in to comment.