Skip to content

Conversation

dleroux
Copy link
Contributor

@dleroux dleroux commented Dec 4, 2018

WHY are these changes introduced?

Resolves https://github.com/Shopify/shopify/issues/174824

The default option we want for our Avatar component is to show the logged in user initials if an avatar image is not found. Gravatar gives us the possibility of providing a default image when an image is not found or to return a 404.

In this pr we attempted to return a transparent image. This was done with the following option in the query:

avatar(fallback: TRANSPARENT) {
      src
    }

The idea being that if the fallback was a transparent image it would simply lay over the Initials and wouldn't be visible.

However the Initials are in circle coloured background. As a result when an image was present it would show through pr

WHAT is this pull request doing?

This PR is taking the is relying on gravatar returning a 404.

avatar(fallback: NOT_FOUND) {
      src
    }

If an image requested returns a 404 the onError attribute of the image tag will trigger and no image will be outputted.

In previous attempts server side rendering was causing the onLoad and onError of the image to not trigger. This PR adds the image only on the componentDidMount Lifecycle.

How to 🎩

🖥 Local development instructions
🗒 General tophatting guidelines
📄 Changelog guidelines

Copy-paste this code in playground/Playground.tsx:
import * as React from 'react';
import {Page, Avatar, Image, Stack} from '@shopify/polaris';

interface State {}

export default class Playground extends React.Component<never, State> {
  render() {
    return (
      <Page title="Playground">
        <Stack>
          <Avatar size="large" initials="DL" customer />
          <Avatar
            size="large"
            initials="DL"
            source="https://avatars2.githubusercontent.com/u/1229901?s=80&v=4"
          />
          <Avatar size="large" initials="DL" source="https://bla" />
        </Stack>
        <br />
        <Image
          alt="Just an image"
          source="https://avatars2.githubusercontent.com/u/1229901?s=80&v=4"
        />
      </Page>
    );
  }
}

🎩 checklist

@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 4, 2018 13:30 Inactive
@dleroux dleroux changed the title [WIP] Avatar loads on component mount [WIP] [Avatar] Makes Avatar Image load only if Image load without error Dec 4, 2018
@dleroux dleroux changed the title [WIP] [Avatar] Makes Avatar Image load only if Image load without error [Avatar] Makes Avatar Image load only if Image load without error Dec 4, 2018
@dleroux dleroux requested a review from tmlayton December 4, 2018 14:08
@dleroux dleroux changed the title [Avatar] Makes Avatar Image load only if Image load without error [WIP] [Avatar] Makes Avatar Image load only if Image load without error Dec 4, 2018
@dleroux
Copy link
Contributor Author

dleroux commented Dec 4, 2018

Leaving this as a WIP until we can verify that this approach will work in production.

Copy link

@danrosenthal danrosenthal left a comment

Choose a reason for hiding this comment

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

I have some concerns about calling setState in componentDidUpdate. Is there anything we can do here as an alternative? Can it be guarded so it's not called needlessly?

@dleroux dleroux changed the title [WIP] [Avatar] Makes Avatar Image load only if Image load without error [Avatar] Makes Avatar Image load only if Image load without error Dec 4, 2018
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 4, 2018 16:16 Inactive
@dleroux dleroux force-pushed the image-error-on-avatar-new branch from 0fa5af8 to 6601426 Compare December 4, 2018 18:55
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 4, 2018 18:55 Inactive
@dleroux dleroux force-pushed the image-error-on-avatar-new branch from 6601426 to 4b50181 Compare December 4, 2018 19:00
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 4, 2018 19:00 Inactive
@tmlayton
Copy link
Contributor

tmlayton commented Dec 8, 2018

A couple questions:

Copy link
Contributor

@tmlayton tmlayton left a comment

Choose a reason for hiding this comment

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

I think a better approach here is to use !isServer (easy to test) and getDerivedStateFromProps for handling changes to the source prop

@dleroux
Copy link
Contributor Author

dleroux commented Dec 8, 2018

I’m okay either way. I prefer taking advantage of the lifecycle hooks, it feels more like a React approach but I’ll give this a shot.

The other PR was reverted because it broke in prod because of an issue with how Avatar was being exported.

@tmlayton
Copy link
Contributor

tmlayton commented Dec 8, 2018

I’m okay either way. I prefer taking advantage of the lifecycle hooks, it feels more like a React approach but I’ll give this a shot.

I would disagree – just because we use a lifecycle hook doesn’t necessarily mean it is a better React approach. In this case, it adds complexity (extra state and lifecycle hook) and is harder to test. It also adds an extra render we can avoid:

constructor -> render -> componentDidMount -> setState -> render -> componentDidUpdate

image

@BPScott BPScott had a problem deploying to polaris-react-pr-712 December 10, 2018 12:41 Failure
@BPScott BPScott had a problem deploying to polaris-react-pr-712 December 10, 2018 12:42 Failure
@dleroux dleroux force-pushed the image-error-on-avatar-new branch from b1f709a to a2589dd Compare December 10, 2018 14:25
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 10, 2018 14:26 Inactive
@dleroux
Copy link
Contributor Author

dleroux commented Dec 10, 2018

@tmlayton all changes have been applied and added a test for the isServer. Once you approve we can retry a test on web.

Copy link

@danrosenthal danrosenthal left a comment

Choose a reason for hiding this comment

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

Code changes, test coverage, and the getDerivedStateFromProps approach in particular LGTM. I guess we give this another try in web at some point this week. What do you think @lemonmade and @tmlayton?

@BPScott BPScott had a problem deploying to polaris-react-pr-712 December 11, 2018 18:09 Failure
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 11, 2018 18:10 Inactive
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 11, 2018 18:10 Inactive
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 11, 2018 18:13 Inactive
@danrosenthal danrosenthal force-pushed the image-error-on-avatar-new branch from 8e267ea to 13b7ade Compare December 11, 2018 18:26
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 11, 2018 18:27 Inactive
@danrosenthal danrosenthal force-pushed the image-error-on-avatar-new branch from 13b7ade to 94406fe Compare December 11, 2018 18:27
@BPScott BPScott temporarily deployed to polaris-react-pr-712 December 11, 2018 18:28 Inactive
@@ -1,4 +1,4 @@
import Avatar from './Avatar';

export * from './Avatar';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@danrosenthal this should ensure we don't get the issue we experienced in web. There's no need to export * now that Avatar is a class component.

@danrosenthal danrosenthal merged commit 35aefa8 into master Dec 12, 2018
@danrosenthal danrosenthal deleted the image-error-on-avatar-new branch December 12, 2018 15:19
@danrosenthal danrosenthal temporarily deployed to production December 12, 2018 18:50 Inactive
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.

4 participants