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

autorefresh tasks for a courier #1840

Merged
merged 41 commits into from
Aug 12, 2024
Merged

Conversation

vladimir-8
Copy link
Contributor

@vladimir-8 vladimir-8 commented Jul 31, 2024

Fixes: #1807

@vladimir-8 vladimir-8 marked this pull request as ready for review August 2, 2024 00:26
@vladimir-8 vladimir-8 changed the title Feature/1807 autorefresh tasks autorefresh tasks for a courier Aug 2, 2024

// checking whether the mutex is locked
if (mutex.isLocked()) {
// wait until the mutex is available without locking it
Copy link
Member

@Atala Atala Aug 7, 2024

Choose a reason for hiding this comment

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

if i understand well we could add the following comment :
// wait for the mutex release, i.e. wait that another request that needed a fresh token gets the new token for us and update the state accordingly
?

Comment on lines +11 to +35
subscriptionGenerateOrders: builder.mutation({
query: date => ({
url: 'api/recurrence_rules/generate_orders',
params: {
date: date.format('YYYY-MM-DD'),
},
method: 'POST',
body: {},
}),
}),
getMyTasks: builder.query({
query: date => `api/me/tasks/${date.format('YYYY-MM-DD')}`,
}),
getOrderTiming: builder.query({
query: nodeId => `${nodeId}/timing`,
}),
getOrderValidate: builder.query({
query: nodeId => `${nodeId}/validate`,
}),
updateOrder: builder.mutation({
query: ({ nodeId, ...patch }) => ({
url: nodeId,
method: 'PUT',
body: patch,
}),
Copy link
Member

Choose a reason for hiding this comment

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

I am surprised by the functions that are not getMyTasks

  1. they are not linked to the PR
  2. they are not explictly called? or maybe they will be called on onFocus/onOnline... aso, but it seems weird to me for example for updateOrder?
  3. we should build apiSlice differently according to the user roles? (getMytasks is for courier, generateOrders for dispatchers, updateorder for admin/restaurant owner ?

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 actually a copy-paste of https://github.com/coopcycle/coopcycle-web/blob/master/js/app/api/slice.js with getMyTasks added. I mentioned elsewhere that, ideally, I see it in a library, but for now, I think we should try to make them compatible so it's easy to copy-paste already implemented endpoints between the web and the app

  1. not directly
  2. no, they are not used at the moment, but will be soon. In a timing modal, for example
  3. I thought about it, but rkt query actually advises against it (see Tip): https://redux-toolkit.js.org/rtk-query/api/createApi, so I decided to stick to the standard approach

Copy link
Member

Choose a reason for hiding this comment

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

  1. i am not sure we understand the same, i was thinking to generate dynamically the slice upon login so you don't load the automatic refetch for tasks for a customer for example

What would happen with the current code if a simple customer logs in for example? will it try to refetch tasks (hence 401) on focus? I can test that later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, at least as I understand. Refetch happens only in TasksPage component

Until you reach it no refetch is setup.

setupListenersReactNative https://github.com/coopcycle/coopcycle-app/blob/f7667c26a9c822427f6ef30aea845baf59f7436a/src/redux/setupListenersReactNative.js only tell rtk about the state changes, but they don't trigger any requests until they are configured elsewhere

Copy link
Member

@Atala Atala left a comment

Choose a reason for hiding this comment

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

hello,

i tested the behavior locally and everything seems alright 👍 it is a huge improvment 👍 and now we have that we can add easily same behavior for restaurant part and solve that much problem

my main interrogation is the apiSlice.endpoints. to me you are mixing endpoints that belongs to different categories of users, and instead we should have one slice for restaurant owner, one for riders, one for dispatcher, one for admin... and combine them according to the user's role?

also can we have fine-grained control on what will be refetched onFocus when we use the global setupListeners ? (i can check myslef if you dont know)

@vladimir-8
Copy link
Contributor Author

can we have fine-grained control on what will be refetched onFocus when we use the global setupListeners ? (i can check myslef if you dont know)

Each component can define the requests that they want to get refetched. It's defined here, if that's what you are asking:

@Atala
Copy link
Member

Atala commented Aug 12, 2024

ah ok i had a misunderstanding of the RTK doc I think, because when I read it I thought the behavior will be enabled by default for all endpoints when calling setupListeners https://redux-toolkit.js.org/rtk-query/api/setupListeners

maybe change your comment enable refetchOnFocus and refetchOnReconnect behaviors to something like bind handlers for refetchOnfocus (need to be enabled in the component ?

@Atala Atala self-requested a review August 12, 2024 07:16
@vladimir-8 vladimir-8 merged commit 89c1f31 into master Aug 12, 2024
2 of 3 checks passed
@vladimir-8 vladimir-8 deleted the feature/1807-autorefresh-tasks branch August 12, 2024 18:28
render() {
const { tasks, tasksWithColor, selectedDate } = this.props;
const { isFetching, refetch } = useGetMyTasksQuery(selectedDate, {
refetchOnFocus: true,
Copy link
Member

Choose a reason for hiding this comment

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

@vladimir-8 we should enable refetchOnReconnect here as well no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can do it, why not

const { tasks, tasksWithColor, selectedDate } = this.props;
const { isFetching, refetch } = useGetMyTasksQuery(selectedDate, {
refetchOnFocus: true,
});
Copy link
Member

Choose a reason for hiding this comment

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

@vladimir-8 also more out of curiosity
with apiSlice i get the impression there is only one centralized thing that handles the fetching, here for the rider tasks
but what if i call the hook from several screens with different configurations (like not the same refetchOnfocus)? each screen/hook usage has its own config or they override each other (I guess that would be bad :rire:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand, all screens with refetchOnfocus == true will try to fetch when they get focused, so we should be careful with using this option, otherwise we end up with a lot of requests. If refetchOnfocus == false then there will be only one request and all other screens should get the data from the cache: https://redux-toolkit.js.org/rtk-query/usage/cache-behavior#default-cache-behavior

I think, ideally, it would be cool to set a lifetime on each item in the cache, like 60 seconds, in that case we don't need refetchOnfocus we will just drop the old cached data and trigger a new request. I don't see any build-in ways to do it, so it seems we need to modify https://redux-toolkit.js.org/rtk-query/api/createApi#onquerystarted method ourselves

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is keepUnusedDataFor https://redux-toolkit.js.org/rtk-query/usage/cache-behavior#reducing-subscription-time-with-keepunuseddatafor option, but I guess a component does not unsubscribe when it loses focus. If it's the case, then it will not work for us

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.

Rider's TaskList not automatically refreshing correctly when coming in foreground
2 participants