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

Update video states #159

Merged
merged 9 commits into from
Dec 2, 2023
Merged

Conversation

IrynaLypnyk
Copy link
Collaborator

@IrynaLypnyk IrynaLypnyk commented Nov 29, 2023

Ticket https://github.com/JoeWeate/thebigmouth/issues/139

  1. added apiUpdateVideo axios request function
  2. created constants for all video actions, states
  3. added function to validate video state update
  4. created VideoActionButton component with all buttons view and logic
  5. add Snackbar component and added for video state updates

To use VideoActionButton you need to add <VideoActionButton key={key} videoData={someVideoData} action={actionName}/> - options for possible actionNames are in constant ACTION_NAME in src/utils/constants.js

All types of buttons temporary placed at the bottom of our DummyHomePage. Pay attention that you will see all the buttons - for admin and for user as userRole is fake(userRole === USER_ROLE.USER, userRole !== USER_ROLE.ADMIN) but their working logic as for real user
Screenshot 2023-11-30 at 01 23 48

@IrynaLypnyk IrynaLypnyk self-assigned this Nov 29, 2023
@IrynaLypnyk IrynaLypnyk changed the base branch from main to li-add-context-provider November 29, 2023 23:33
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please merge the latest main? I think we treat this component as the actual home page now

Copy link
Collaborator Author

@IrynaLypnyk IrynaLypnyk Dec 1, 2023

Choose a reason for hiding this comment

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

You are right. Tank you. Removed all buttons examples from that page.
Added to comment above "To use VideoActionButton you need to add <VideoActionButton key={key} videoData={someVideoData} action={actionName}/> - options for possible actionNames are in constant ACTION_NAME in src/utils/constants.js"

};

export const VIDEO_STATE = {
DRAFT: 'draft', //[USER: prevState === null (just uploaded)],
Copy link
Collaborator

Choose a reason for hiding this comment

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

The comment here is not entirely correct, right? It could be that the video was moved to draft

{//DELETE
action: ACTION_NAME.DELETE,
currentState: VIDEO_STATE.DRAFT,
nextSate: null,//do we keep it in DB?
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 delete is a delete, we should remove the record from the database. Let's leave this for now and implement the delete functionality later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I remember that we discussed this and agreed to not to keep them in DB, I just forgot to delete the comment.

nextSate: VIDEO_STATE.DRAFT
},
{//PUT
action: ACTION_NAME.MOVE_TO_PENDING,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Should we rename this action to SEND_FOR_REVIEW? What do you think? We might then rename the pending state to IN_REVIEW as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Super!. SEND_FOR_REVIEW is much better.

@@ -0,0 +1,40 @@
import { VIDEO_DATA_KEYS, USER_ROLE, VIDEO_STATE, ACTION_NAME } from './constants';

export const validateChangeState = ({videoData, action, userRole}) => {
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 we can simplify this function by reusing the logic you've implemented in constants.js.

Imagine that we have an object that looks like the following:

videoStates = 
{
  "user": {
      "MOVE_TO_PENDING": {
        "validate": (currentState) => currentState == DRAFT,
       "nextState": "PENDING"
      },
      "MOVE_TO_DRAFT": {....},
      // other actions
  },
  "admin": {
     "APPROVE": {
       "validate": (currentState) => currentState == PENDING
       "nextState": "APPROVED"
     },
     // other actions
  }

Then we can use this object in the validateChangeState function as the following:

val state = videoStates[user.role][action]
val isValid = state["validate"](videoData[VIDE_DATA_KEYS.STATE])
if (isValid) state["nextState"]

I think this will allow us to have the logic defined in the videoStates object and the validateChangeState function will be simple without lots of nested if conditions. Let me know what you think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@sherif98 it sounds great. It really should be much simpler and more readable. I'll do it now. Thank you so much for your review 🫶

Comment on lines 10 to 11
if (userRole === USER_ROLE.USER && currentVideoState === VIDEO_STATE.PENDING) {
nextState = VIDEO_STATE.APPROVED;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this correct? Can a user approve a video?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course it is not correct 😱. The right code should be

if (action === ACTION_NAME.APPROVE) {
            if (userRole === USER_ROLE.ADMIN && (currentVideoState === VIDEO_STATE.IN_REVIEW)) {
                nextState = VIDEO_STATE.APPROVED;
            }

Thank you!

@IrynaLypnyk
Copy link
Collaborator Author

@sherif98 thank you very much for your review 🫶 I've updated my code according to your comments. Hope very much you will have time to look through it once again with my updates.

Copy link
Collaborator

@sherif98 sherif98 left a comment

Choose a reason for hiding this comment

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

LGTM! Looks great now Iryna!

@@ -31,3 +31,18 @@ export const getAllVideoByUserID = (UserID) => {
console.log(error);
});
};

export const apiUpdateVideo = (updatedVideo, handleSnackbar) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: rename handleSnackbar to a more generic name, for example handleStatus or reportStatus. The reason is that in API level (in videos.js) it doesn't need to know that you're using a snackbar or an error message or whatever to display something to user, it should be agnostic on the API level

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please merge the latest main and update the PR. I don't think this file is newly added here, it was added in the SortBy PR

if(isValid) {
return VIDEO_STATE_UPDATE_ACTIONS[userRole][action].nextSate;
} else {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the if branch we return a string (the state) while here we return a boolean, can we unify the return type of the function? Maybe make it return undefined here or so?

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

well-written and structured code makes it easily readable, good use of the use context from react for accessing of the user role

//navigate to somewhere
});
} else if(action === ACTION_NAME.DELETE){
console.log('add delete confirmation and axios request')
Copy link
Collaborator

@Bedi06 Bedi06 Dec 2, 2023

Choose a reason for hiding this comment

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

It seems like you've handled everything well. Great job! Perhaps, in the future, we could consider incorporating a pop-up message or a text that says, "Are you sure you want to delete this video?" This will provide a double confirmation with a Yes or No option

Copy link
Collaborator

@AndriusIsin AndriusIsin left a comment

Choose a reason for hiding this comment

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

👏 Great job! You've done an excellent job with this pull request! 👏

@IrynaLypnyk IrynaLypnyk merged commit e5bcfe4 into li-add-context-provider Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants