track when publishState.hasChanged; only update when true

This commit is contained in:
Travis Eden 2018-10-28 18:25:53 -04:00
parent e9c60f652f
commit 3378e4289f
9 changed files with 43 additions and 16 deletions

View file

@ -20,6 +20,13 @@ export function setUpdateTrue () {
}; };
} }
export function setHasChanged (status) {
return {
type: actions.SET_HAS_CHANGED,
data: status,
};
}
export function updateMetadata (name, value) { export function updateMetadata (name, value) {
return { return {
type: actions.METADATA_UPDATE, type: actions.METADATA_UPDATE,

View file

@ -12,3 +12,4 @@ export const PUBLISH_START = 'PUBLISH_START';
export const CLAIM_AVAILABILITY = 'CLAIM_AVAILABILITY'; export const CLAIM_AVAILABILITY = 'CLAIM_AVAILABILITY';
export const SET_UPDATE_TRUE = 'SET_UPDATE_TRUE'; export const SET_UPDATE_TRUE = 'SET_UPDATE_TRUE';
export const ABANDON_CLAIM = 'ABANDON_CLAIM'; export const ABANDON_CLAIM = 'ABANDON_CLAIM';
export const SET_HAS_CHANGED = 'SET_HAS_CHANGED';

View file

@ -5,9 +5,10 @@ import View from './view';
const mapStateToProps = ({ show, publish }) => { const mapStateToProps = ({ show, publish }) => {
return { return {
file : publish.file, file: publish.file,
isUpdate: publish.isUpdate, isUpdate: publish.isUpdate,
asset : selectAsset(show), hasChanged: publish.hasChanged,
asset: selectAsset(show),
}; };
}; };

View file

@ -32,8 +32,8 @@ class PublishDetails extends React.Component {
} }
} }
onCancel () { onCancel () {
const { isUpdate, clearFile, history } = this.props; const { isUpdate, hasChanged, clearFile, history } = this.props;
if (isUpdate) { if (isUpdate || !hasChanged) {
history.push('/'); history.push('/');
} else { } else {
if (confirm(SAVE)) { if (confirm(SAVE)) {

View file

@ -12,9 +12,10 @@ const mapStateToProps = props => {
} }
return { return {
disabled: publish.disabled, disabled: publish.disabled,
file : publish.file, file: publish.file,
status : publish.status.status, status: publish.status.status,
isUpdate: publish.isUpdate, isUpdate: publish.isUpdate,
hasChanged: publish.hasChanged,
uri, uri,
}; };
}; };

View file

@ -8,7 +8,7 @@ import { SAVE } from '../../constants/confirmation_messages';
class PublishTool extends React.Component { class PublishTool extends React.Component {
render () { render () {
const {disabled, file, isUpdate, uri, status} = this.props; const {disabled, file, isUpdate, hasChanged, uri, status} = this.props;
if (disabled) { if (disabled) {
return ( return (
<PublishDisabledMessage /> <PublishDisabledMessage />
@ -23,6 +23,7 @@ class PublishTool extends React.Component {
return ( return (
<React.Fragment> <React.Fragment>
<Prompt <Prompt
when={hasChanged}
message={SAVE} message={SAVE}
/> />
<PublishPreview isUpdate={isUpdate} uri={uri} /> <PublishPreview isUpdate={isUpdate} uri={uri} />

View file

@ -1,5 +1,5 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { setUpdateTrue, updateMetadata, clearFile } from '../../actions/publish'; import { setUpdateTrue, setHasChanged, updateMetadata, clearFile } from '../../actions/publish';
import { onHandleShowPageUri } from '../../actions/show'; import { onHandleShowPageUri } from '../../actions/show';
import { selectAsset } from '../../selectors/show'; import { selectAsset } from '../../selectors/show';
import View from './view'; import View from './view';
@ -17,6 +17,7 @@ const mapDispatchToProps = {
updateMetadata, updateMetadata,
onHandleShowPageUri, onHandleShowPageUri,
setUpdateTrue, setUpdateTrue,
setHasChanged,
clearFile, clearFile,
}; };

View file

@ -5,12 +5,13 @@ import PublishTool from '@containers/PublishTool';
class EditPage extends React.Component { class EditPage extends React.Component {
componentDidMount () { componentDidMount () {
const {asset, match, onHandleShowPageUri, clearFile, setUpdateTrue, updateMetadata} = this.props; const {asset, match, onHandleShowPageUri, setUpdateTrue, setHasChanged, updateMetadata} = this.props;
onHandleShowPageUri(match.params); onHandleShowPageUri(match.params);
setUpdateTrue(); setUpdateTrue();
if (asset) { if (asset) {
['title', 'description', 'license', 'nsfw'].forEach(meta => updateMetadata(meta, asset.claimData[meta])); ['title', 'description', 'license', 'nsfw'].forEach(meta => updateMetadata(meta, asset.claimData[meta]));
} }
setHasChanged(false);
} }
componentWillUnmount () { componentWillUnmount () {
this.props.clearFile(); this.props.clearFile();

View file

@ -41,7 +41,8 @@ const initialState = {
license : '', license : '',
nsfw : false, nsfw : false,
}, },
isUpdate : false, isUpdate: false,
hasChanged: false,
thumbnail: null, thumbnail: null,
thumbnailChannel, thumbnailChannel,
thumbnailChannelId, thumbnailChannelId,
@ -52,6 +53,7 @@ export default function (state = initialState, action) {
case actions.FILE_SELECTED: case actions.FILE_SELECTED:
return Object.assign({}, state.isUpdate ? state : initialState, { // note: clears to initial state return Object.assign({}, state.isUpdate ? state : initialState, { // note: clears to initial state
file: action.data, file: action.data,
hasChanged: true,
}); });
case actions.FILE_CLEAR: case actions.FILE_CLEAR:
return initialState; return initialState;
@ -60,14 +62,17 @@ export default function (state = initialState, action) {
metadata: Object.assign({}, state.metadata, { metadata: Object.assign({}, state.metadata, {
[action.data.name]: action.data.value, [action.data.name]: action.data.value,
}), }),
hasChanged: true,
}); });
case actions.CLAIM_UPDATE: case actions.CLAIM_UPDATE:
return Object.assign({}, state, { return Object.assign({}, state, {
claim: action.data, claim: action.data,
hasChanged: true,
}); });
case actions.SET_PUBLISH_IN_CHANNEL: case actions.SET_PUBLISH_IN_CHANNEL:
return Object.assign({}, state, { return Object.assign({}, state, {
publishInChannel: action.channel, publishInChannel: action.channel,
hasChanged: true,
}); });
case actions.PUBLISH_STATUS_UPDATE: case actions.PUBLISH_STATUS_UPDATE:
return Object.assign({}, state, { return Object.assign({}, state, {
@ -84,17 +89,26 @@ export default function (state = initialState, action) {
selectedChannel: action.data, selectedChannel: action.data,
}); });
case actions.TOGGLE_METADATA_INPUTS: case actions.TOGGLE_METADATA_INPUTS:
return Object.assign({}, state, { return {
...state,
showMetadataInputs: action.data, showMetadataInputs: action.data,
}); };
case actions.THUMBNAIL_NEW: case actions.THUMBNAIL_NEW:
return Object.assign({}, state, { return {
...state,
thumbnail: action.data, thumbnail: action.data,
}); hasChanged: true,
};
case actions.SET_UPDATE_TRUE: case actions.SET_UPDATE_TRUE:
return Object.assign({}, state, { return {
...state,
isUpdate: true, isUpdate: true,
}); };
case actions.SET_HAS_CHANGED:
return {
...state,
hasChanged: action.data,
};
default: default:
return state; return state;
} }