Skip to content

Commit

Permalink
Modify saving of a temporary model after deleting a saved model
Browse files Browse the repository at this point in the history
  • Loading branch information
levindoneto committed Dec 26, 2017
1 parent af5f0df commit 852422e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const refSavedModels = firebase.database().ref('savedModels/');
const refTmp = firebase.database().ref('tmp/');

refTrig.on('child_changed', (snapshot) => {
console.log('Triggering ...');
//console.log('Triggering ...');
refSavedModels.on('value', (savedM) => {
accessedModel = JSON.parse(savedM.val()[snapshot.key]);
for (let i = 0; i < (Object.keys(accessedModel[['@graph']])).length; i++) {
Expand Down Expand Up @@ -178,8 +178,6 @@ export function fireAjaxImport(type, content) {
}

export function fireAjaxSave(name, content, isBinding, alertSave, tmpSaving) {
console.log('IM SAVING');
console.log('tmpSaving: ', tmpSaving);
localStorage.setItem(IS_SYNC, FALSE); // Do not set a model before the synchronization
const notShowAlert = alertSave | false;
const thisIsBinding = isBinding | false; // If the user hasn't clicked <Bind> isBinding is undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const DIGITAL_TWIN_WAS_EMPTY = 'digitalTwinWasEmpty';
const LOAD_LAST_MODEL = 'loadLastModel';
const LOAD_TEMP_MODEL = 'loadTempModel';
const IS_TEMPORARY_MODEL = 'isTemporaryModel';
const DELETED_MODEL = '__del_model__'; /* Flag put in the infoSavedModels.lastLoadedModels in order
* to not try to loaded a non-existant model from the database */
let savedModels = {};

const style = {
Expand Down Expand Up @@ -339,14 +341,15 @@ export default class NavigationBar extends React.Component {
};

handleOpenSaveModel = () => {
const auxInfoSaved = {};
if (localStorage.getItem(IS_TEMPORARY_MODEL) === TRUE && !backend.isDigitalTwinEmpty()) {
this.setState({ openSaveModelAs: true });
}
else {
localStorage.setItem(IS_TEMPORARY_MODEL, FALSE);
let auxSavedModels = {};
refInfoSaved.once('value', (snapshot) => {
if (localStorage.getItem(DIGITAL_TWIN_WAS_EMPTY) === FALSE && backend.isDigitalTwinEmpty()) { // The user has loaded a model in the current section
if (localStorage.getItem(DIGITAL_TWIN_WAS_EMPTY) === FALSE && backend.isDigitalTwinEmpty() && snapshot.val().lastLoadedModel !== DELETED_MODEL && snapshot.val().lastLoadedModel !== DELETED_MODEL) { // The user has loaded a model in the current section
swal({
title: ('Do you want to delete the model '.concat(snapshot.val().lastLoadedModel)).concat(' ?'),
text: 'Once deleted, the model will not be available for modifications anymore!',
Expand All @@ -355,6 +358,9 @@ export default class NavigationBar extends React.Component {
dangerMode: true
}).then((value) => {
if (value) { // [Yes]
auxInfoSaved.lastLoadedModel = DELETED_MODEL;
auxInfoSaved.lastSavedModel = DELETED_MODEL;
refInfoSaved.update(auxInfoSaved);
refSavedModels.child(snapshot.val().lastLoadedModel).remove();
refDevicesWithSubsystems.child(snapshot.val().lastLoadedModel).remove();
swal({
Expand Down Expand Up @@ -398,7 +404,7 @@ export default class NavigationBar extends React.Component {
}, LEVEL.THERE);
}
else {
if (snapshot.val().lastLoadedModel !== TEMP_MODEL) { // The temp model is not supposed to be overwritten this way
if (snapshot.val().lastLoadedModel !== TEMP_MODEL && snapshot.val().lastLoadedModel !== DELETED_MODEL && snapshot.val().lastLoadedModel !== DELETED_MODEL) { // The temp model is not supposed to be overwritten this way
swal({
title: 'Are you sure you want save the new model over the current one?',
text: ('The model '.concat(snapshot.val().lastLoadedModel)).concat(' will be overwritten if you confirm this action.'),
Expand All @@ -409,7 +415,7 @@ export default class NavigationBar extends React.Component {
auxSavedModels[snapshot.val().lastLoadedModel] = JSON.stringify(DeviceStore.getModel()); /* key:last_loaded_model,
* value: current model on the digital twin */
refSavedModels.update(auxSavedModels); // Update the current model on the database
backend.fireAjaxSave(snapshot.val().lastLoadedModel, DeviceStore.getModel(), false, false, falase); /* The DevicesWithSubsystems.lastLoadedModel is overwritten
backend.fireAjaxSave(snapshot.val().lastLoadedModel, DeviceStore.getModel(), false, false, false); /* The DevicesWithSubsystems.lastLoadedModel is overwritten
* with the current information on the digital twin */
swal({
title: 'The current model has been saved successfully',
Expand Down
Loading

0 comments on commit 852422e

Please sign in to comment.