willba-component-library 0.1.57 → 0.1.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "willba-component-library",
3
- "version": "0.1.57",
3
+ "version": "0.1.59",
4
4
  "description": "A stroybook 6 with TypeScript demo",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.esm.js",
@@ -15,6 +15,10 @@ type Story = StoryObj<typeof FilterBar>
15
15
 
16
16
  export const Primary: Story = {
17
17
  args: {
18
+ calendarOffset: {
19
+ rooms: 7,
20
+ events: -2,
21
+ },
18
22
  fullWidth: true,
19
23
  language: '',
20
24
  redirectUrl: '',
@@ -7,8 +7,10 @@ import '../../themes/Default.css'
7
7
  import { parseDates, parseGuests } from './utils'
8
8
  import {
9
9
  AgeCategoryType,
10
+ CalendarOffset,
10
11
  FilterSections,
11
12
  Filters,
13
+ Pages,
12
14
  ViewApply,
13
15
  } from './FilterBarTypes'
14
16
  import {
@@ -39,6 +41,7 @@ export type FilterBarProps = {
39
41
  currentViewApply?: string
40
42
  onSubmit?: ((val: Filters) => void) | null
41
43
  fullWidth?: boolean
44
+ calendarOffset: CalendarOffset
42
45
  }
43
46
 
44
47
  export default function FilterBar({
@@ -49,6 +52,7 @@ export default function FilterBar({
49
52
  currentViewApply,
50
53
  onSubmit,
51
54
  fullWidth,
55
+ calendarOffset,
52
56
  }: FilterBarProps) {
53
57
  const themePalette = useTheme({ palette })
54
58
 
@@ -69,6 +73,7 @@ export default function FilterBar({
69
73
  handleSubmit,
70
74
  updateGuestsCount,
71
75
  setSelectedPath,
76
+ handleResetFilters,
72
77
  } = useFilterBar({
73
78
  redirectUrl,
74
79
  currentViewApply,
@@ -79,7 +84,7 @@ export default function FilterBar({
79
84
  // Default selected tab when tabs are hidden
80
85
  useEffect(() => {
81
86
  if (currentViewApply === ViewApply.ROOMS) {
82
- setSelectedPath('/rooms')
87
+ setSelectedPath(Pages.ROOMS)
83
88
  }
84
89
  }, [])
85
90
 
@@ -116,13 +121,17 @@ export default function FilterBar({
116
121
  >
117
122
  <TabButton
118
123
  label={t('tabs.events')}
119
- onClick={() => setSelectedPath('/events')}
120
- active={selectedPath === '/events'}
124
+ onClick={() => {
125
+ setSelectedPath(Pages.EVENTS), handleResetFilters()
126
+ }}
127
+ active={selectedPath === Pages.EVENTS}
121
128
  />
122
129
  <TabButton
123
130
  label={t('tabs.rooms')}
124
- onClick={() => setSelectedPath('/rooms')}
125
- active={selectedPath === '/rooms'}
131
+ onClick={() => {
132
+ setSelectedPath(Pages.ROOMS), handleResetFilters()
133
+ }}
134
+ active={selectedPath === Pages.ROOMS}
126
135
  />
127
136
  </div>
128
137
  )}
@@ -136,7 +145,7 @@ export default function FilterBar({
136
145
  description={
137
146
  parsedDates
138
147
  ? parsedDates
139
- : selectedPath === '/rooms'
148
+ : selectedPath === Pages.ROOMS
140
149
  ? t('calendar.roomsLabelPlaceholder')
141
150
  : t('calendar.eventsLabelPlaceholder')
142
151
  }
@@ -144,14 +153,16 @@ export default function FilterBar({
144
153
  active={selectedFilter === FilterSections.CALENDAR}
145
154
  />
146
155
 
147
- {selectedPath === '/rooms' && (
156
+ {selectedPath === Pages.ROOMS && (
148
157
  <>
149
158
  <Divider />
150
159
 
151
160
  <SelectButton
152
161
  label={t('guests.label')}
153
162
  description={
154
- parsedGuests ? parsedGuests : t('guests.labelPlaceholder')
163
+ parsedGuests
164
+ ? t('guests.guestsLabel') + parsedGuests
165
+ : t('guests.labelPlaceholder')
155
166
  }
156
167
  onClick={() => handleSelectedFilter(FilterSections.GUESTS)}
157
168
  active={selectedFilter === FilterSections.GUESTS}
@@ -174,6 +185,8 @@ export default function FilterBar({
174
185
  calendarRange={calendarRange}
175
186
  setCalendarRange={setCalendarRange}
176
187
  ref={filtersRef}
188
+ calendarOffset={calendarOffset}
189
+ selectedPath={selectedPath}
177
190
  />
178
191
  )}
179
192
  {selectedFilter === FilterSections.GUESTS && (
@@ -29,3 +29,12 @@ export type Filters = { [key: string]: string }
29
29
  export enum ViewApply {
30
30
  ROOMS = 'roomFilters',
31
31
  }
32
+
33
+ export enum Pages {
34
+ ROOMS = '/rooms',
35
+ EVENTS = '/events',
36
+ }
37
+
38
+ export type CalendarOffset = {
39
+ [key: string]: number
40
+ }
@@ -3,16 +3,23 @@ import { DateRange, DayPicker } from 'react-day-picker'
3
3
  import { addDays, startOfDay, addMonths } from 'date-fns'
4
4
  import { useMediaQuery } from 'react-responsive'
5
5
 
6
+ import { CalendarOffset } from '../../FilterBarTypes'
7
+
6
8
  import 'react-day-picker/dist/style.css'
7
9
  import './Calendar.css'
8
10
 
9
11
  type Props = {
10
12
  calendarRange: DateRange | undefined
11
13
  setCalendarRange: (range: DateRange | undefined) => void
14
+ calendarOffset: CalendarOffset
15
+ selectedPath: string
12
16
  }
13
17
 
14
18
  export const Calendar = forwardRef<HTMLDivElement, Props>(
15
- ({ calendarRange, setCalendarRange }: Props, ref) => {
19
+ (
20
+ { calendarRange, setCalendarRange, calendarOffset, selectedPath }: Props,
21
+ ref
22
+ ) => {
16
23
  const isTablet = useMediaQuery({ maxWidth: 960 })
17
24
 
18
25
  useEffect(() => {
@@ -21,10 +28,17 @@ export const Calendar = forwardRef<HTMLDivElement, Props>(
21
28
 
22
29
  const today = startOfDay(new Date())
23
30
 
31
+ const daysToOffsetCalendar = Object.entries(calendarOffset)?.find(
32
+ (page) => selectedPath.substring(1) === page[0]
33
+ )
34
+
24
35
  const disabledDays = [
25
36
  {
26
- from: addDays(today, -2),
27
- to: addDays(today, -50),
37
+ from: addDays(
38
+ today,
39
+ !!daysToOffsetCalendar?.length ? daysToOffsetCalendar[1] : -2
40
+ ),
41
+ to: addDays(today, -100),
28
42
  },
29
43
  ]
30
44
  const selectedStartDate = calendarRange?.from
@@ -2,7 +2,12 @@ import { useEffect, useState } from 'react'
2
2
  import { DateRange } from 'react-day-picker'
3
3
  import { format } from 'date-fns'
4
4
 
5
- import { AgeCategoryCount, AgeCategoryType, Filters } from '../FilterBarTypes'
5
+ import {
6
+ AgeCategoryCount,
7
+ AgeCategoryType,
8
+ Filters,
9
+ Pages,
10
+ } from '../FilterBarTypes'
6
11
 
7
12
  type Props = {
8
13
  redirectUrl?: string
@@ -17,7 +22,7 @@ export const useFilterBar = ({
17
22
  ageCategories,
18
23
  onSubmit,
19
24
  }: Props) => {
20
- const [selectedPath, setSelectedPath] = useState('/events')
25
+ const [selectedPath, setSelectedPath] = useState(Pages.EVENTS)
21
26
  const [selectedFilter, setSelectedFilter] = useState<string | boolean>(false)
22
27
  const [calendarRange, setCalendarRange] = useState<DateRange | undefined>()
23
28
 
@@ -65,11 +70,11 @@ export const useFilterBar = ({
65
70
  // Handle default selected tab
66
71
  if (typeof window === 'undefined') return
67
72
 
68
- const currentPath = window.location.pathname.includes('/events')
69
- ? '/events'
70
- : window.location.pathname.includes('/rooms')
71
- ? '/rooms'
72
- : '/events'
73
+ const currentPath = window.location.pathname.includes(Pages.EVENTS)
74
+ ? Pages.EVENTS
75
+ : window.location.pathname.includes(Pages.ROOMS)
76
+ ? Pages.ROOMS
77
+ : Pages.EVENTS
73
78
 
74
79
  setSelectedPath(currentPath)
75
80
  }, [])
@@ -120,7 +125,7 @@ export const useFilterBar = ({
120
125
 
121
126
  handleSelectedFilter(false)
122
127
 
123
- if (onSubmit && selectedPath === '/rooms') {
128
+ if (onSubmit && selectedPath === Pages.ROOMS) {
124
129
  const updatedParamsObject: { [key: string]: string } = {}
125
130
 
126
131
  updatedParams.forEach((value, key) => {
@@ -141,7 +146,7 @@ export const useFilterBar = ({
141
146
  }
142
147
 
143
148
  handleSelectedFilter(false)
144
- return onSubmit && selectedPath === '/rooms'
149
+ return onSubmit && selectedPath === Pages.ROOMS
145
150
  ? onSubmit(newParams)
146
151
  : (window.location.href = `${redirectUrl}/${selectedPath}${
147
152
  querySearchParams ? `?${querySearchParams.toString()}` : ''
@@ -151,8 +156,7 @@ export const useFilterBar = ({
151
156
 
152
157
  const handleResetFilters = () => {
153
158
  setAgeCategoryCounts({})
154
- handleSubmit()
155
- setSelectedFilter(false)
159
+ setCalendarRange(undefined)
156
160
  }
157
161
 
158
162
  return {
@@ -5,15 +5,37 @@ type Props = {
5
5
  ageCategories: AgeCategoryType[]
6
6
  }
7
7
 
8
- export const parseGuests = ({ ageCategoryCounts, ageCategories }: Props) =>
9
- Object.entries(ageCategoryCounts).reduce((acc, [key, value]) => {
10
- const ageCategory = ageCategories.find((c) => c.id === key[key.length - 1])
8
+ type AccType = {
9
+ total: number
10
+ html: string[]
11
+ }
12
+
13
+ export const parseGuests = ({ ageCategoryCounts, ageCategories }: Props) => {
14
+ const parsedData = Object.entries(ageCategoryCounts).reduce(
15
+ (acc: AccType, [key, value]) => {
16
+ const ageCategoryId = key[key.length - 1]
17
+ const ageCategory = ageCategories.find((c) => c.id === ageCategoryId)
18
+
19
+ if (ageCategory && value) {
20
+ return {
21
+ total: acc.total + value,
22
+ html: [
23
+ ...acc.html,
24
+ `<span style="display: inline-block; width: 21px, text-align: center">${value}</span>`,
25
+ ],
26
+ }
27
+ }
11
28
 
12
- if (ageCategory && value) {
13
- acc += `${acc ? ' |' : ''} ${
14
- ageCategory.name
15
- }: <span style="display: inline-block; width: 21px">${value}</span>`
16
- }
29
+ return acc
30
+ },
31
+ { total: 0, html: [] }
32
+ )
17
33
 
18
- return acc
19
- }, '')
34
+ const htmlString =
35
+ parsedData.html.length > 1 ? parsedData.html.join(' + ') : ''
36
+
37
+ return (
38
+ (parsedData.total || htmlString) &&
39
+ `: ${parsedData.total} ${htmlString ? `- ( ${htmlString} )` : ''}`
40
+ )
41
+ }
@@ -12,7 +12,8 @@
12
12
  "labelPlaceholder": "Add guests",
13
13
  "title": "Who's coming?",
14
14
  "adultsLabel": "Adults",
15
- "kidsLabel": "kids"
15
+ "kidsLabel": "kids",
16
+ "guestsLabel": "Guests"
16
17
  },
17
18
  "categories": {
18
19
  "label": "Categories",
@@ -12,7 +12,8 @@
12
12
  "labelPlaceholder": "Lisää vieraat",
13
13
  "title": "Ketkä ovat tulossa?",
14
14
  "adultsLabel": "Aikuiset",
15
- "kidsLabel": "lapset"
15
+ "kidsLabel": "lapset",
16
+ "guestsLabel": "Gieraita"
16
17
  },
17
18
  "categories": {
18
19
  "label": "Kategoriat",