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

Timeslots all returning today as date #58

Open
denisyilmaz opened this issue Aug 8, 2024 · 2 comments
Open

Timeslots all returning today as date #58

denisyilmaz opened this issue Aug 8, 2024 · 2 comments
Labels

Comments

@denisyilmaz
Copy link

Description

While working on getting the new getGroupedRanges() feature preparsed so i can use it via GraphQL I saw that all dates returned by the plugin are using today as the date.

For example given this template:

{% apply spaceless %}
{{element.openingHours.getGroupedRanges(1) | json_encode}}
{% endapply %}

i get the following result:

[
  [
    {
      "open": {
        "date": "2024-08-08 07:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      },
      "close": {
        "date": "2024-08-08 19:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      }
    },
    {
      "open": {
        "date": "2024-08-08 07:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      },
      "close": {
        "date": "2024-08-08 19:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      }
    },
    {
      "open": {
        "date": "2024-08-08 07:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      },
      "close": {
        "date": "2024-08-08 19:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      }
    },
    {
      "open": {
        "date": "2024-08-08 07:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      },
      "close": {
        "date": "2024-08-08 19:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      }
    }
  ],
  [
    {
      "open": {
        "date": "2024-08-08 07:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      },
      "close": {
        "date": "2024-08-08 21:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      }
    }
  ],
  [
    {
      "open": {
        "date": "2024-08-08 08:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      },
      "close": {
        "date": "2024-08-08 22:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      }
    },
    {
      "open": {
        "date": "2024-08-08 08:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      },
      "close": {
        "date": "2024-08-08 22:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
      }
    }
  ]
]

while the groups are all correct, there is no way to see which day of the weeks the timeslots are refering to.

I tested this via GraphQL as well:

{
  locationsEntries {
    ... on location_Entry {
      openingHours {
        open
        close
      }
    }
  }
}

results in:

{
  "data": {
    "locationsEntries": [
      {
        "openingHours": [
          {
            "open": "2024-08-08T08:00:00+02:00",
            "close": "2024-08-08T22:00:00+02:00"
          },
          {
            "open": "2024-08-08T07:00:00+02:00",
            "close": "2024-08-08T19:00:00+02:00"
          },
          {
            "open": "2024-08-08T07:00:00+02:00",
            "close": "2024-08-08T19:00:00+02:00"
          },
          {
            "open": "2024-08-08T07:00:00+02:00",
            "close": "2024-08-08T19:00:00+02:00"
          },
          {
            "open": "2024-08-08T07:00:00+02:00",
            "close": "2024-08-08T19:00:00+02:00"
          },
          {
            "open": "2024-08-08T07:00:00+02:00",
            "close": "2024-08-08T21:00:00+02:00"
          },
          {
            "open": "2024-08-08T08:00:00+02:00",
            "close": "2024-08-08T22:00:00+02:00"
          }
        ]
      }
    ]
  }
}

Is there no way to get the "day of the week" as an int in the same result without having to call the getMon()/getTue() functions?

Additional info

  • Craft version: 5.2.10
  • Plugins & versions: 4.2.0
@denisyilmaz denisyilmaz added the bug label Aug 8, 2024
@denisyilmaz
Copy link
Author

denisyilmaz commented Aug 8, 2024

the types returned by graphql only show the columns, but not the day name for example or any other field:
image

I guess whats missing is the dayIndex that is saved for each item that is used when calling getName() for each day in twig.

@denisyilmaz
Copy link
Author

For anyone trying to solve this with Javascript, this is what i came up with:

export interface GroupedOpeningHour {
  days: Array<number>;
  firstDay: number;
  lastDay: number;
  open: string;
  close: string;
}

function openingHoursGrouped(openingHours: OpeningHours_Day[]) {

  const result: Array<GroupedOpeningHour> = [];

  // move sunday at the end of array
  const sunday = openingHours.shift();
  if (sunday) {
    openingHours.push(sunday);
  }

  openingHours.forEach((day, index) => {
    const dayOfWeek = index + 1;

    const lastEntry = result[result.length - 1];

    if (lastEntry && lastEntry.open === day?.open && lastEntry.close === day?.close && lastEntry.days[lastEntry.days.length - 1] === index) {
      lastEntry.days.push(dayOfWeek);
      lastEntry.lastDay = dayOfWeek;
    } else {
      result.push({
        days: [dayOfWeek],
        firstDay: dayOfWeek,
        lastDay: dayOfWeek,
        open: day?.open,
        close: day?.close
      });
    }
  });

  return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant