Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Click views #31

Merged
merged 7 commits into from
Aug 18, 2019
Merged

Click views #31

merged 7 commits into from
Aug 18, 2019

Conversation

pedanticantic
Copy link
Contributor

No description provided.

@pedanticantic pedanticantic mentioned this pull request Aug 6, 2019
@pedanticantic
Copy link
Contributor Author

pedanticantic commented Aug 6, 2019

See also the issue that this PR is for.

I have refactored the code to save a single record instead of the entire database when anything is changed.

This involved rationalising the various ways the application saves data, so now it uses $add(), $save() and $remove(). As a result, the record collection keys are now strings instead of integers - I believe is the correct way of doing it.

It no longer uses the syncObject (well almost never). An added bonus is that there are essentially no errors in the console now (there are one or two, but they're not related to saving/loading data).

There are a lot of controllers where 1 line ("$scope") has been removed. app/scripts/services/DataRepositoryFactory.js has had a lot of redundant code removed. The main changes are in app/scripts/services/DataRepository.js.

I made a couple of small improvements/changes whilst doing this:

  • after deleting a record, it now refreshes the screen without reloading the app. These are the "$state" changes in app/scripts/controllers/editBrands.js, app/scripts/controllers/editCategories.js and app/scripts/controllers/editProducts.js.
  • similar for clicking "add another" after adding a record. These are the "$state" changes in app/scripts/controllers/formAdd.js.
  • after the app loads, it no longer saves the entire database. I'm not sure why it was doing this, but it didn't seem right and caused problems, so I've removed it. This is the block I removed.

Copy link
Member

@toriejw toriejw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!!! I think I just had one comment, and I'm also wondering about the console.logs - I do very little front end work so I'm not sure if those are left over or if we want those there for error warnings.

Really exciting improvements! 🎉

@@ -289,6 +315,42 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
return id;
};

this.determineIndexFromBrandModel = function determineIndexFromBrandModel(brandModel) {
// Determine the index of the firebase array, using the brand model's id.
var indexToDelete = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other places we may want to use this function? If there are we may want to rename this variable so it isn't related to deletion. Maybe something like targetIndex or indexMatch or something else completely :) . (same in the other 2 functions below)

Copy link
Contributor Author

@pedanticantic pedanticantic Aug 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, you're right. Well spotted! I re-used the code that was explicitly for deleting a record, but didn't notice the variable name.
I'll change it (and the other methods) now - this method is also used when updating a record.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I went with a different format for the name :-)
I went with brandIndex, categoryIndex, etc.
The commit is here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect - love it!

// Overwrite the record with the values from the model.
this.populateRecordFromBrandModel(brandSource, brand);
// Save the record back to the store.
this.update('brands', brandIndex, successMsg, callback);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments are exceptionally helpful in keeping how firebase functions straight 👌. Thank you for them!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you. Yeah, I put the comments in because when I looked at the code it looked really complicated and it wasn't clear at all what was going on, so I essentially commented every line!

productRecord.parentCategoryId = product.getParentCategoryId();
productRecord.purchaseURlClicks = product.getPurchaseUrlClicks();
productRecord.purchaseUrl = product.getPurchaseUrl();
// productRecord.$$hashKey = product.$$hashKey(); @TODO: Do we save this? When does it change?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was that this was some internal key to firebase that we don't need to worry about (still very worth looking into, because I'm not sure I'm remembering that correctly or if it's just wishful thinking 😄).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha! Wishful thinking it a dangerous thing!!
I'll leave it for now, but make a note to look into it. Is this a feature of firebase (ie does it generate this hash itself?)

@pedanticantic
Copy link
Contributor Author

@toriejw , in reply to your question about the console.logs, I have a vague memory that someone (possibly Aashni?) said that they were essentially placeholders because the error handling hadn't really been implemented.
Obviously the end users aren't going to see them (unless they have the developer console open, which is fairly unlikely), so at the moment only developers will see them.
Perhaps we need to suggest error handling as another task to do within the project?

@pedanticantic
Copy link
Contributor Author

pedanticantic commented Aug 9, 2019

Oh, @toriejw , I was also going to say that I am worried/annoyed that there is still quite a lot of duplicated (well very similar) code after my big refactor. You'll learn that one of my pet hates is duplicated code!!
For example, there are 3 methods, determineIndexFrom<record-type>Model() that have identical logic in them, but just reference a different array/model.
In theory they could be combined into a single method, but the code would be horribly dynamic, hard to understand and prone to subtle bugs.
Do you have any ideas how we could reduce the duplication?
I think it's okay for now, but might be a problem in the long term.

@toriejw
Copy link
Member

toriejw commented Aug 10, 2019

Makes senes on the duplication. I don't have any other ideas currently, but I'll keep it in the back of my head and see if anything comes up :) .

@pedanticantic pedanticantic merged commit d93621e into master Aug 18, 2019
@pedanticantic pedanticantic deleted the click-views branch August 18, 2019 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants