UoaWDCC / uasc-web

Project with the University of Auckland Snowsports Club Website & Booking System. Established in 2023.
https://uasc.co.nz
6 stars 2 forks source link

[FRONTEND] Add optimistic mutations to promote/demote user mutations #404

Closed bcho892 closed 4 months ago

bcho892 commented 4 months ago

Is your feature request related to a problem? Please describe.

https://tanstack.com/query/v4/docs/framework/react/guides/optimistic-updates

We currently do not perform any sort of optimistic update on the user data, which means the only way to reflect the changes by an admin is to refetch all the user data, leading to bad UX more inefficiency.

To solve this in the AdminMutations we should mutate the data object, likely using the onMutate property

like in the snippet below (provided by the docs)

onMutate: async (newTodo) => {
    // Cancel any outgoing refetches
    // (so they don't overwrite our optimistic update)
    await queryClient.cancelQueries({ queryKey: ['todos', newTodo.id] })

    // Snapshot the previous value
    const previousTodo = queryClient.getQueryData(['todos', newTodo.id])

    // Optimistically update to the new value
    queryClient.setQueryData(['todos', newTodo.id], newTodo)

    // Return a context with the previous and new todo
    return { previousTodo, newTodo }
  }

BEFORE MERGING