This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Steigerwald
authored and
Daniel Steigerwald
committed
Nov 19, 2018
1 parent
e59d83c
commit cb1f892
Showing
2 changed files
with
21 additions
and
45 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 |
---|---|---|
@@ -1,51 +1,28 @@ | ||
// @flow | ||
import * as React from 'react'; | ||
import React from 'react'; | ||
import { DeleteButton } from './core/buttons'; | ||
import withMutation from './core/withMutation'; | ||
import withConfirm, { type Confirm } from './core/withConfirm'; | ||
import useConfirm from '../hooks/useConfirm'; | ||
import Router from 'next/router'; | ||
import type { Href } from '../browser/sitemap'; | ||
import { pipe } from 'ramda'; | ||
import DeleteWebMutation, { | ||
type DeleteWebCommit, | ||
} from '../mutations/DeleteWebMutation'; | ||
import { useDeleteWebMutation } from '../mutations/DeleteWebMutation'; | ||
|
||
type DeleteWebProps = {| | ||
id: string, | ||
confirm: Confirm, | ||
commit: DeleteWebCommit, | ||
pending: boolean, | ||
|}; | ||
export default function DeleteWeb(props: {| id: string |}) { | ||
const confirm = useConfirm(); | ||
const [commit, , pending] = useDeleteWebMutation(); | ||
|
||
class DeleteWeb extends React.PureComponent<DeleteWebProps> { | ||
handleCompleted = ({ deleteWeb }) => { | ||
const id = deleteWeb && deleteWeb.web && deleteWeb.web.id; | ||
if (id == null) return; | ||
const href: Href = { | ||
pathname: '/', | ||
}; | ||
function onSuccess() { | ||
const href: Href = { pathname: '/' }; | ||
// $FlowFixMe Wrong libdef. | ||
Router.replace(href); | ||
}; | ||
|
||
handleOnPress = () => { | ||
if (!this.props.confirm()) return; | ||
const input = { id: this.props.id }; | ||
this.props.commit(input, this.handleCompleted); | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<DeleteButton | ||
color="danger" | ||
disabled={this.props.pending} | ||
onPress={this.handleOnPress} | ||
/> | ||
); | ||
function handleOnPress() { | ||
if (!confirm()) return; | ||
const input = { id: props.id }; | ||
commit(input, onSuccess); | ||
} | ||
} | ||
|
||
export default pipe( | ||
withConfirm, | ||
withMutation(DeleteWebMutation), | ||
)(DeleteWeb); | ||
return ( | ||
<DeleteButton color="danger" disabled={pending} onPress={handleOnPress} /> | ||
); | ||
} |
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