0

I have the following code that's intended to filter an array based on id:

combineReminders: builder.mutation<Reminder, Partial<Reminder>[]>({
  query: ([...reminders]) => ({
    url: "/",
    method: "PUT",
    body: reminders,
  }),
  async onQueryStarted([...reminders], { dispatch, queryFulfilled }) {
    const combineResult = dispatch(
      reminderApi.util.updateQueryData(
        "getReminders",
        undefined,
        (draft: Partial<Reminder>[]) => {
          const ids = reminders.map((item) => item.id);
          console.log("ids:", ids);
          const result = reminders.reduce((acc, reminder) => {
            const { priority, date, time } = reminder;
            const compareDates = (
              date1: Date | undefined,
              date2: Date | undefined
            ) => {
              if (!date1) return date2;
              if (!date2) return date1;
              return date1 < date2 ? date2 : date1;
            };
            const compareTimes = (
              time1: string | undefined,
              time2: string | undefined
            ) => {
              if (!time1) return time2;
              if (!time2) return time1;
              return new Date(time1) < new Date(time2) ? time2 : time1;
            };
            return {
              ...acc,
              date: compareDates(date, acc.date) || date,
              time: compareTimes(time, acc.time) || time,
              priority: priority,
            };
          });
          // draft.push(result);
          draft = draft.filter((item) => ids.includes(item.id)); <------- not working
          console.log(draft);
        }
      )
    );
    try {
      await queryFulfilled;
    } catch (error) {
      combineResult.undo();
    }
  },
}),

But it's doing nothing. I've considered the fact that maybe because I've mentioned the PUT method inside the mutation and not DELETE? But that logic is completely separate from this.

Maybe I'm not filtering correctly. I've commented out the reduce logic, and my event handlers and frontend logic works because it does add the result on click if I were to uncomment it.

Not sure why it's not filtering and returning the result though.

Mathew
  • 318
  • 11

0 Answers0