-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add saveButton and publishButton
- Loading branch information
1 parent
44d2de3
commit 96c55eb
Showing
2 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { updateQuestionService } from '@/api' | ||
import { useGetComponentInfo, useGetPageInfo } from '@/hooks' | ||
import { LoadingOutlined } from '@ant-design/icons' | ||
import { Button, Space, message } from 'antd' | ||
import { FC } from 'react' | ||
|
||
/** 保存按钮 */ | ||
const SaveButton: FC = () => { | ||
const { id } = useParams() | ||
const { componentList = [] } = useGetComponentInfo() | ||
const pageInfo = useGetPageInfo() | ||
|
||
/** 发送保存请求 */ | ||
const { loading, run: save } = useRequest( | ||
async () => { | ||
if (!id) return | ||
await updateQuestionService(id, { ...pageInfo, componentList }) | ||
}, | ||
{ manual: true }, | ||
) | ||
|
||
/** 快捷键 */ | ||
useKeyPress(['ctrl.s', 'meta.s'], (event: KeyboardEvent) => { | ||
event.preventDefault() | ||
if (!loading) save() | ||
}) | ||
|
||
/** 自动保存 */ | ||
useDebounceEffect( | ||
() => { | ||
save() | ||
}, | ||
[componentList, pageInfo], | ||
{ | ||
wait: 1000, | ||
}, | ||
) | ||
|
||
return ( | ||
<Button | ||
onClick={save} | ||
disabled={loading} | ||
icon={loading ? <LoadingOutlined /> : null} | ||
> | ||
保存 | ||
</Button> | ||
) | ||
} | ||
|
||
/** 发布按钮 */ | ||
const PublishButton: FC = () => { | ||
const nav = useNavigate() | ||
const { id } = useParams() | ||
const { componentList = [] } = useGetComponentInfo() | ||
const pageInfo = useGetPageInfo() | ||
|
||
/** 发布请求 */ | ||
const { loading, run: publishData } = useRequest( | ||
async () => { | ||
if (!id) return | ||
await updateQuestionService(id, { | ||
...pageInfo, | ||
componentList, | ||
isPublished: true, // 表示问卷已经发布 | ||
}) | ||
}, | ||
{ | ||
manual: true, | ||
onSuccess() { | ||
message.success('发布成功') | ||
nav('/question/stat/' + id) // 发布成功,跳转到统计页面 | ||
}, | ||
}, | ||
) | ||
|
||
return ( | ||
<Button | ||
type="primary" | ||
onClick={publishData} | ||
disabled={loading} | ||
icon={loading ? <LoadingOutlined /> : null} | ||
> | ||
发布 | ||
</Button> | ||
) | ||
} | ||
|
||
/** 头部按钮集合 */ | ||
const HeaderButton: FC = () => { | ||
return ( | ||
<Space> | ||
<SaveButton /> | ||
<PublishButton /> | ||
</Space> | ||
) | ||
} | ||
|
||
export default HeaderButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters