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

feat: planter selfie rotation #142

Merged
merged 6 commits into from
Sep 15, 2021
Merged

Conversation

tranquanghuy0801
Copy link
Member

Resolves #137

Copy link
Collaborator

@nmcharlton nmcharlton left a comment

Choose a reason for hiding this comment

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

It's a good start, but a few things don't quite work as they should.
We also need to apply the rotation on Planter cards and in the Planter Detail dialog.

onSelectChange('imageRotation', 0);
} else {
setRotation(rotation + 90);
onSelectChange('imageRotation', rotation + 90);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could reduce duplication here by using a temporary variable:

let newRotation = rotation + 90;
if (newRotation === 360) {
  newRotation = 0;
}
setRotation(newRotation);
onSelectChange('imageRotation, newRotation);

@@ -129,7 +165,15 @@ export default function ImageScroller(props) {
height={192}
className={classes.image}
fixed
style={{ transform: `rotate(${rotation}deg)` }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Applying the style attribute here overrides the value inside the OptimizedImage component, causing the image to become squashed.
Instead, you could add a rotation or transform prop to OptimizedImage and add the transform to the style within that component.

position: 'absolute',
bottom: 0,
right: 0,
width: 25,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the button needs to be larger (try 32px).

/>
<IconButton
Copy link
Collaborator

Choose a reason for hiding this comment

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

A Fab is more appropriate here, and the default styling is better than an IconButton.

@@ -118,7 +154,7 @@ export default function ImageScroller(props) {
images.slice(0, maxImages).map((img, idx) => (
<Card
key={`${idx}_${img}`}
onClick={() => onSelectImage(img)}
onClick={() => handleDataChange(img)}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Clicking the image shouldn't trigger rotation, just select the image.
Only clicking the rotate button should trigger rotation.

/>
<IconButton
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should only show the rotation button on (and apply to the rotation to) the selected image.
Otherwise it looks a bit messy with multiple images:

Screen.Recording.2021-08-08.at.12.03.36.mov

@@ -103,6 +126,19 @@ export default function ImageScroller(props) {
});
}

function handleDataChange(img) {
if (img) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doing the selection and rotation at the same time doesn't work. Only the rotation is applied.
Selection and rotation should be two separate actions.

@@ -90,17 +90,13 @@ const EditPlanter = (props) => {
return newPlanter[key] !== planter[key];
});

if (changed) {
if (key === 'imageRotation' || changed) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This causes the Save button to be enabled if there is a non-zero rotation, even if nothing has changed.

width: 25,
height: 25,
color: 'white',
backgroundColor: 'black',
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you use a Fab component, there's no need to override the default colors.

<IconButton
className={classes.clickRotate}
onClick={() => handleDataChange(null)}
style={{ transform: `rotateY(180deg)` }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's just the icon we want to rotate, not the button.

@nmcharlton
Copy link
Collaborator

@tranquanghuy0801 For info, while having a look at the latest updates I noticed a bug in the api (fixed in Greenstand/treetracker-admin-api#587) whereby imageRotation was required in a planter PATCH.

The fix has now been deployed to dev and test.

@tranquanghuy0801
Copy link
Member Author

@nmcharlton Yeah thanks for fixing that issue. I will check my code again later today.

@tranquanghuy0801
Copy link
Member Author

tranquanghuy0801 commented Aug 10, 2021

Screenshot 2021-08-10 at 19 58 03
@nmcharlton Now I am getting stuck at this. As the Card image and the rotation icon both have the onClick props, therefore when I click the icon, it will also execute the onClick of the Card image, which will overwrite the planterUpdate variable

planterUpdate = {imageRotation: 90} --> planterUpdate = {imageUrl: ''}

This will make the "Save" button become inactive even you click rotate icon. Hope this make sense. Do you have any thought on this case?

@tranquanghuy0801
Copy link
Member Author

@nmcharlton that's alright. I knew how to fix that. Check my latest commits.

Copy link
Collaborator

@nmcharlton nmcharlton left a comment

Choose a reason for hiding this comment

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

Thanks @tranquanghuy0801. This looks a lot better, but I still see a couple of issues:

  1. The rotation should only be applied to the selected image in the Edit Planter dialog, but is applied to all of them. Search planter ID 323 in the Planters tool for an example.
  2. Rotated images have gaps at the top and bottom in the Edit Planter dialog, and overlap text in the Planter Detail drawer (see video below). The image should fill but not overflow the container.
  3. If you edit the planter from the Verify tool, the Planter Detail does not get updated straight away. This is a more general problem, and not specific to image rotation, so could be handled under a separate PR if you don't want to fix it here.
Screen.Recording.2021-08-14.at.14.02.18.mov

@tranquanghuy0801
Copy link
Member Author

@nmcharlton
Thanks for your feedback. I fixed the issue 1 & 2. About the issue 3 you mentioned, you can create another issue for this and assign that to me.

Copy link
Collaborator

@nmcharlton nmcharlton left a comment

Choose a reason for hiding this comment

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

Sorry to keep asking for changes – this is a more complex problem than I'd realised!

@@ -30,7 +30,7 @@ const useStyle = makeStyles((theme) => ({
textAlign: 'center',
},
imageCard: {
height: '100%',
height: '84%',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's keep it simple and make these images square.

objRotation[selectedIndex] = imageRotation;
} else {
images.map((_, idx) => {
objRotation[idx] = imageRotation;
Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't really make sense to apply image rotation to all images if none is selected.
Just leave all other images unrotated and apply the rotation to the selected image only.

objRotation[idx] = imageRotation;
});
}
let [rotations, setRotations] = useState(objRotation);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is introducing unnecessary complexity.
There's no point in persisting the image rotation for other images in the frontend like this; only one value gets stored for each planter so all other rotations will be lost when they refresh (which is annoying for a user).

@@ -147,14 +157,14 @@ export default function ImageScroller(props) {
height={192}
className={classes.image}
fixed
rotation={rotation}
rotation={rotations[idx] ? rotations[idx] : imageRotation}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest reverting back to the single value, but only applying it if this is the selected image.
When the user selects a new image, just reset the rotation to zero.

@@ -108,7 +105,14 @@ const PlanterDetail = (props) => {
return (
<React.Fragment>
<Drawer anchor="right" open={props.open} onClose={props.onClose}>
<Grid className={classes.root}>
<Grid
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's make the container and image square to avoid this awkwardness and inconsistency between rotated and unrotated images.

The planter card can also be updated to use a square image.

@tranquanghuy0801
Copy link
Member Author

tranquanghuy0801 commented Aug 17, 2021

@nmcharlton

I am a bit struggling to remove the white gaps when setting the rotation of image = 90 or 270 deg if I set the images square as you said in Edit Planter dialog. Do you have any suggestions at this?

@nmcharlton
Copy link
Collaborator

@nmcharlton

I am a bit struggling to remove the white gaps when setting the rotation of image = 90 or 270 deg if I set the images square as you said in Edit Planter dialog. Do you have any suggestions at this?

@tranquanghuy0801 It seems to be a quirk of the way rotated elements are rendered, presumably due to some rounding in the transform calculation.
As a workaround I'd suggest increasing the height and width of the image slightly (just make sure you add a comment explaining the css. This appears to work in the OptimizedImage style property:

          width: 'calc(100% + 1px)',
          height: 'calc(100% + 1px)',

@nmcharlton nmcharlton merged commit f307700 into Greenstand:master Sep 15, 2021
@tranquanghuy0801 tranquanghuy0801 deleted the fix-81 branch October 13, 2021 11:08
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.

Planter selfie rotation
2 participants