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

Lf 4671 migrate weather board saga to rtk query #3699

Open
wants to merge 2 commits into
base: integration
Choose a base branch
from

Conversation

gursimran-singh
Copy link
Collaborator

Description

This PR migrates the functionality to RTK query by extending the main API and removes the dependency on saga and slice.

Jira link:https://lite-farm.atlassian.net/browse/LF-4671

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Login with the account and on the home page correct weather is shown.

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have added "MISSING" for all new language tags to languages I don't speak
  • I have added the GNU General Public License to all new files

@gursimran-singh gursimran-singh requested review from a team as code owners February 26, 2025 17:54
@gursimran-singh gursimran-singh requested review from Duncan-Brain and kathyavini and removed request for a team February 26, 2025 17:54
Copy link
Collaborator

@Duncan-Brain Duncan-Brain left a comment

Choose a reason for hiding this comment

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

Thanks so much @gursimran-singh I think it looks great to me!

If you are interested in continuing on WeatherBoard we would love to obscure the api call a little more and move it to the backend https://lite-farm.atlassian.net/browse/LF-4281

Copy link
Collaborator

@kathyavini kathyavini left a comment

Choose a reason for hiding this comment

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

This is so cool Gursimran!!

Thank you for delving into so much uncharted territory and getting us started with the separate files for new endpoints (actually I think we should have started doing this earlier 😅), migrating off of slice + saga (🎉!!) and using RTK Query to interface with an external API. It's great! ☺️

Everything functions well, and my only comments are about how to keep the weatherApi.ts a little bit more parallel to the existing RTK Query endpoints in terms of types and structure. I think a consistent structure will help us a lot with keeping things readable at a glance as you pick up more and more endpoints.

Awesome job -- so exciting to see the great migration underway 😁

@@ -0,0 +1,67 @@
/*
Copy link
Collaborator

@kathyavini kathyavini Feb 28, 2025

Choose a reason for hiding this comment

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

I'm pretty sure we'd like to keep all the endpoint definitions within store/api/ instead of keeping them near their calling component. But I'll double-check with @antsgar when she returns!

export const weatherApi = api.injectEndpoints({
endpoints: (build) => ({
getWeather: build.query({
queryFn: async (arg) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know it's only called only in a JS file, but since all our API endpoints will be written in TS, it would be great to keep type safety. In this case, the return type can be inferred, but the parameters cannot so at least those could be typed explicitly:

interface WeatherQueryArgs {
  lat: number;
  lon: number;
  measurement: string;
}

BUT to keep consistency with all our other RTK query endpoints and for the clearest documentation, it would actually be ideal if we could keep this format and type both return and params:

getWeather: build.query<WeatherData, WeatherQueryArgs>({

}

try {
const response = await axios.get(openWeatherUrl.toString());
Copy link
Collaborator

@kathyavini kathyavini Feb 28, 2025

Choose a reason for hiding this comment

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

RTK Query manages its own fetching so it would be cleaner and more like the other endpoints to use query instead of queryFn and you wouldn't need axios or the error handling (try/catch) as that is all built-in (unless there is a particularly reason it's necessary to use axios for this endpoint)

query: ({ lat, lon, measurement }) => {
  const apikey = import.meta.env.VITE_WEATHER_API_KEY;
  const params = new URLSearchParams({
    lat: String(lat),
   // ...
  });

  return `https://api.openweathermap.org/data/2.5/weather?${params.toString()}`;
},

We haven't used it yet in app, but I think the best RTK Query pattern for the returned data would then be transformResponse:

transformResponse: (response: any /* optional to type their API response */) => {
 const weatherPayload = {
   humidity: `${response.main?.humidity}%`,
  // ...
 };

 return weatherPayload;

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.

3 participants