Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
added: alert on add the existed dat. (#601)
Browse files Browse the repository at this point in the history
Co-authored-by: Karissa McKelvey <[email protected]>
  • Loading branch information
AtuyL and okdistribute authored Mar 26, 2020
1 parent 82c05f3 commit cbcaa20
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
15 changes: 11 additions & 4 deletions app/actions/dat-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export default class DatMiddleware {
if (key) {
key = encode(key)
if (this.dats[key]) {
return this.dispatch({
this.dispatch({ type: 'ADD_DAT_ERROR:EXISTED' })
throw this.dispatch({
type: 'ADD_DAT_ERROR',
key,
error: new Error('Dat with same key already added.')
Expand All @@ -122,7 +123,8 @@ export default class DatMiddleware {
for (let key in this.dats) {
const dat = this.dats[key]
if (dat.path === path) {
return this.dispatch({
this.dispatch({ type: 'ADD_DAT_ERROR:EXISTED' })
throw this.dispatch({
type: 'ADD_DAT_ERROR',
key,
error: new Error('Dat with same path already added.')
Expand Down Expand Up @@ -257,6 +259,10 @@ export default class DatMiddleware {

async downloadSparseDat ({ key }) {
key = encode(key)
if (this.dats[key]) {
this.dispatch({ type: 'ADD_DAT_ERROR:EXISTED' })
return
}
const path = joinPath(this.downloadsDir, key)

this.dispatch({ type: 'ADD_DAT', key, path })
Expand Down Expand Up @@ -313,13 +319,14 @@ export default class DatMiddleware {
}

removeDatInternally (key) {
const { dat } = this.dats[key]
this.dispatch({ type: 'REMOVE_DAT', key })

const { dat } = this.dats[key] || {}
if (!dat) return // maybe was deleted
delete this.dats[key]
if (dat.mirrorProgress) {
dat.mirrorProgress.destroy()
}
this.dispatch({ type: 'REMOVE_DAT', key })

for (const con of dat.network.connections) {
con.removeAllListeners()
Expand Down
1 change: 1 addition & 0 deletions app/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const copyLink = link => {
return { type: 'DIALOGS_LINK_COPY' }
}
export const closeShareDat = () => ({ type: 'DIALOGS_LINK_CLOSE' })
export const closeAlert = () => ({ type: 'DIALOGS_ALERT_CLOSE' })

export const createDat = () => dispatch => {
showOpenDialog({
Expand Down
1 change: 1 addition & 0 deletions app/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default connect(mapStateToProps, mapDispatchToProps)(function ({

<Dialog.LinkContainer />
<Dialog.ConfirmContainer />
<Dialog.AlertContainer />
<DragDropContainer />
</Fragment>
)
Expand Down
30 changes: 26 additions & 4 deletions app/components/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import styled from 'styled-components'
import Icon from './icon'
import * as Button from './button'

const Overlay = styled.div`
background: rgba(0, 0, 0, 0.25);
`

const Inner = styled.div`
min-width: 25rem;
max-width: 32rem;
Expand Down Expand Up @@ -117,14 +121,15 @@ function CloseButton ({ onExit }) {
onClick={onExit}
className='absolute pointer pa0 top-0 right-0 h2 w2 bg-transparent tc exit btn-close'
aria-label='Close Modal'
style={{ outline: 0 }}
>
<Icon name='cross' />
</button>
)
}

export const Link = ({ link, copied, onCopy, onExit }) => (
<div
<Overlay
className='modal fixed items-center justify-center top-0 left-0 h-100 w-100 z-9999'
style={{ display: link ? 'flex' : 'none' }}
>
Expand Down Expand Up @@ -160,11 +165,11 @@ export const Link = ({ link, copied, onCopy, onExit }) => (
</p>
<CloseButton onExit={onExit} />
</Inner>
</div>
</Overlay>
)

export const Confirm = ({ dat, onConfirm, onExit }) => (
<div
<Overlay
className='modal fixed items-center justify-center top-0 left-0 h-100 w-100 z-9999'
style={{ display: dat ? 'flex' : 'none' }}
>
Expand All @@ -188,5 +193,22 @@ export const Confirm = ({ dat, onConfirm, onExit }) => (
</p>
<CloseButton onExit={onExit} />
</Inner>
</div>
</Overlay>
)

export const Alert = ({ alert, onExit }) => (
<Overlay
className='modal fixed items-center justify-center top-0 left-0 h-100 w-100 z-9999'
style={{ display: alert ? 'flex' : 'none' }}
>
<Inner className='relative flex flex-column justify-center'>
<p className='mt3 mb4 f5 color-neutral-70'>{alert}</p>
<p>
<Button.Green className='fr btn-confirm' onClick={onExit} autoFocus>
CLOSE
</Button.Green>
</p>
<CloseButton onExit={onExit} />
</Inner>
</Overlay>
)
14 changes: 12 additions & 2 deletions app/containers/dialog.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Link, Confirm } from '../components/dialog'
import { Link, Confirm, Alert } from '../components/dialog'
import {
copyLink,
closeShareDat,
confirmDeleteDat,
cancelDeleteDat
cancelDeleteDat,
closeAlert
} from '../actions'
import { connect } from 'react-redux'

Expand All @@ -27,3 +28,12 @@ export const ConfirmContainer = connect(
onExit: () => dispatch(cancelDeleteDat())
})
)(Confirm)

export const AlertContainer = connect(
state => ({
alert: state.dialogs.alert
}),
dispatch => ({
onExit: () => dispatch(closeAlert())
})
)(Alert)
23 changes: 21 additions & 2 deletions app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const redatApp = (state = generateDefaultState(), action) => {
},
screen: SCREEN.DATS
}
case 'ADD_DAT_ERROR:EXISTED':
return {
...state,
dialogs: {
...state.dialogs,
alert: 'The DAT is already in the list.'
}
}
case 'ADD_DAT_ERROR':
case 'WRITE_METADATA_ERROR':
return {
Expand All @@ -87,8 +95,11 @@ const redatApp = (state = generateDefaultState(), action) => {
}
}
case 'REMOVE_DAT':
const { [action.key]: del, ...dats } = state.dats
return { ...state, dats }
if (state.dats[action.key]) {
const { [action.key]: del, ...dats } = state.dats
return { ...state, dats }
}
return state
case 'INSPECT_DAT':
return {
...state,
Expand Down Expand Up @@ -268,6 +279,14 @@ const redatApp = (state = generateDefaultState(), action) => {
}
}
}
case 'DIALOGS_ALERT_CLOSE':
return {
...state,
dialogs: {
...state.dialogs,
alert: null
}
}
case 'TOGGLE_PAUSE':
const dat = state.dats[action.key]
return {
Expand Down

0 comments on commit cbcaa20

Please sign in to comment.