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

Improve CSV download for Fit cohort view #27608

Merged
merged 3 commits into from Mar 23, 2019
Merged

Improve CSV download for Fit cohort view #27608

merged 3 commits into from Mar 23, 2019

Conversation

hacodeorg
Copy link
Contributor

@hacodeorg hacodeorg commented Mar 20, 2019

PLC-161

  • Add more fields to Fit cohort CSV file.
  • Sanitize strings so CSV file can be viewed correctly in spreadsheet tools.
  • Injecting downloadCsv into AdminCohortView so the function can be tested properly.
  • Add 3 tests for AdminCohortView component
    1. It can be rendered
    2. It can sanitize string
    3. It can compile data to export to CSV file.

How tested

  • Unit test

    • In apps folder, run yarn test:entry --entry test/unit/code-studio/pd/application_dashboard/admin_cohort_viewTest.js
    • In dashboard folder, run bundle exec rails test test/controllers/api/v1/pd/applications_controller_test.rb
  • Manual test

    • In local dev environment, created FiT 1920 application and a summer Workshop.
    • Registered the application for Fit Weekend workshop
    • In Application dashboard (log in as a workshop admin) -> Fit cohort view -> Download CSV. Got a CSV file like this:
    Screen Shot 2019-03-20 at 1 28 15 PM Screen Shot 2019-03-20 at 1 28 29 PM Screen Shot 2019-03-20 at 1 28 44 PM Screen Shot 2019-03-20 at 1 29 02 PM

Copy link

@clareconstantine clareconstantine left a comment

Choose a reason for hiding this comment

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

It would be great to get test coverage on this. Also, did you test the fields that are blank in your screenshots?

let res = str;
if (str) {
// Convert line breaker to dot, multiple spaces to single space
res = res.replace(/\n|\r\n|\r+/gm, '.');

Choose a reason for hiding this comment

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

Let's put a space after the period to make it easier to read

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -47,12 +48,16 @@ def registered_fit
object.try(:registered_fit_workshop?)
end

def registered_fit_submission_time
object.try(FIT_WEEKEND_REGISTRATION_SYMBOL).try(:created_at)
end

Choose a reason for hiding this comment

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

it would be great if we could make this a friendlier format to read, like we do in the timestamp log.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think we should do it at the server side or client side? Should it be in UTC or local time zone?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

}

return res;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Since both replace and trim return a new string instead of modifying the original string, you could chain these calls.

sanitizeStringForCsv = str => (str || '')
  .replace(/\n|\r\n|\r+/gm, '.')
  .replace(/\s+/gm, ' ')
  .trim();

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 like this, it is much cleaner!

let res = str;
if (str) {
// Convert line breaker to dot, multiple spaces to single space
res = res.replace(/\n|\r\n|\r+/gm, '.');
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a bug here - did you mean (\n|\r\n|\r)+? As-is, you'll match multiple consecutive carriage returns \r but not multiple consecutive line breaks in other formats.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's a bug, thanks for catching it

// Sanitize string content before exporting it to CSV
filteredCohortWithFormData.forEach(row => {
Object.keys(row).forEach(key => {
if (_.isString(row[key])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not typeof row[key] === 'string'?

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 learn that idea from here https://stackoverflow.com/a/9436948; it seems like the safe way is to do typeof myVar === 'string' || myVar instanceof String. I'm not fully confident with Javascript yet to know if there is any other corner case, so I opt to use a library instead. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it - using the helper is fine. In practice I think it's so rare for us to use new String() that the instanceof call isn't needed, but no complaints from me about covering all possibilities!

@hacodeorg
Copy link
Contributor Author

Add test coverage for sanitizeString and handleDownloadCsv functions

It would be great to get test coverage on this. Also, did you test the fields that are blank in your screenshots?

Copy link
Contributor

@islemaster islemaster left a comment

Choose a reason for hiding this comment

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

🎉

@hacodeorg hacodeorg merged commit 99a37bf into staging Mar 23, 2019
@hacodeorg hacodeorg deleted the ha/fit-csv branch March 23, 2019 06:23
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.

None yet

3 participants