willba-component-library 0.1.41 → 0.1.42

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.41",
3
+ "version": "0.1.42",
4
4
  "description": "A stroybook 6 with TypeScript demo",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.esm.js",
@@ -32,7 +32,7 @@ export const Primary: Story = {
32
32
  {
33
33
  id: '1',
34
34
  name: 'Aikuiset',
35
- minVal: 2,
35
+ minVal: 1,
36
36
  sortOrder: 1,
37
37
  },
38
38
  ],
@@ -61,7 +61,7 @@ export default function FilterBar({
61
61
  handleSubmit,
62
62
  updateGuestsCount,
63
63
  setSelectedPath,
64
- } = useFilterBar({ redirectUrl, currentViewApply })
64
+ } = useFilterBar({ redirectUrl, currentViewApply, ageCategories })
65
65
 
66
66
  // Default selected tab
67
67
  useEffect(() => {
@@ -37,7 +37,7 @@ export const Calendar = forwardRef<HTMLDivElement, Props>(
37
37
  <DayPicker
38
38
  id="will-calendar"
39
39
  mode="range"
40
- showOutsideDays
40
+ //showOutsideDays
41
41
  numberOfMonths={!isTablet ? 2 : 1}
42
42
  weekStartsOn={1}
43
43
  selected={calendarRange}
@@ -12,10 +12,6 @@ export default function GuestCount({
12
12
  count,
13
13
  minVal,
14
14
  }: GuestsCountPropsType) {
15
- useEffect(() => {
16
- if (minVal) updateGuestsCount(`guests-${id}`, minVal)
17
- }, [])
18
-
19
15
  const handleDecrement = () => {
20
16
  if (count > minVal) {
21
17
  updateGuestsCount(`guests-${id}`, count - 1)
@@ -34,10 +30,10 @@ export default function GuestCount({
34
30
  <button
35
31
  className="will-guests-filter-counter-button"
36
32
  onClick={handleDecrement}
37
- disabled={minVal && count < 2 ? true : false}
33
+ disabled={minVal && count <= minVal ? true : false}
38
34
  style={{
39
35
  cursor:
40
- minVal && count < 2
36
+ minVal && count <= minVal
41
37
  ? 'initial'
42
38
  : !minVal && count < 1
43
39
  ? 'initial'
@@ -31,7 +31,7 @@ export const Guests = forwardRef<HTMLDivElement, Props>(
31
31
  count={
32
32
  (ageCategoryCounts as AgeCategoryCount)[
33
33
  `guests-${category.id}`
34
- ] || category.minVal
34
+ ] || 0
35
35
  }
36
36
  />
37
37
  ))}
@@ -2,14 +2,19 @@ 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 } from '../FilterBarTypes'
5
+ import { AgeCategoryCount, AgeCategoryType } from '../FilterBarTypes'
6
6
 
7
7
  type Props = {
8
8
  redirectUrl?: string
9
9
  currentViewApply?: string
10
+ ageCategories?: AgeCategoryType[]
10
11
  }
11
12
 
12
- export const useFilterBar = ({ redirectUrl, currentViewApply }: Props) => {
13
+ export const useFilterBar = ({
14
+ redirectUrl,
15
+ currentViewApply,
16
+ ageCategories,
17
+ }: Props) => {
13
18
  const [selectedPath, setSelectedPath] = useState('/events')
14
19
 
15
20
  const [selectedFilter, setSelectedFilter] = useState<number | boolean>(false)
@@ -44,6 +49,7 @@ export const useFilterBar = ({ redirectUrl, currentViewApply }: Props) => {
44
49
  }, [])
45
50
 
46
51
  useEffect(() => {
52
+ // Handle hide scroll bar on mobile
47
53
  if (typeof window === 'undefined') return
48
54
 
49
55
  document.body.style.overflow =
@@ -55,16 +61,16 @@ export const useFilterBar = ({ redirectUrl, currentViewApply }: Props) => {
55
61
  }, [selectedFilter])
56
62
 
57
63
  useEffect(() => {
58
- if (typeof window !== 'undefined') {
59
- // Handle default selected tab
60
- const currentPath = window.location.pathname.includes('/events')
61
- ? '/events'
62
- : window.location.pathname.includes('/rooms')
63
- ? '/rooms'
64
- : '/events'
65
-
66
- setSelectedPath(currentPath)
67
- }
64
+ // Handle default selected tab
65
+ if (typeof window === 'undefined') return
66
+
67
+ const currentPath = window.location.pathname.includes('/events')
68
+ ? '/events'
69
+ : window.location.pathname.includes('/rooms')
70
+ ? '/rooms'
71
+ : '/events'
72
+
73
+ setSelectedPath(currentPath)
68
74
  }, [])
69
75
 
70
76
  const updateGuestsCount = (id: string, newCount: number) => {
@@ -85,9 +91,10 @@ export const useFilterBar = ({ redirectUrl, currentViewApply }: Props) => {
85
91
  : '',
86
92
  endDate: calendarRange?.to ? format(calendarRange.to, 'yyyy-MM-dd') : '',
87
93
  categories,
88
- ageCategoryCounts: Object.entries(ageCategoryCounts).length
89
- ? JSON.stringify(ageCategoryCounts)
90
- : '',
94
+ ageCategoryCounts: handleAgeCategoryRules({
95
+ ageCategoryCounts,
96
+ ageCategories,
97
+ }),
91
98
  }
92
99
 
93
100
  if (currentViewApply) {
@@ -151,3 +158,41 @@ export const useFilterBar = ({ redirectUrl, currentViewApply }: Props) => {
151
158
  setSelectedPath,
152
159
  }
153
160
  }
161
+
162
+ ////////////
163
+
164
+ const handleAgeCategoryRules = ({
165
+ ageCategoryCounts,
166
+ ageCategories,
167
+ }: {
168
+ ageCategoryCounts: AgeCategoryCount
169
+ ageCategories?: AgeCategoryType[]
170
+ }) => {
171
+ if (ageCategories?.length) {
172
+ ageCategories?.map((a) => {
173
+ if (a.minVal) {
174
+ // Age categories rules
175
+ const totalAgeCategories = Object.entries(ageCategoryCounts).reduce(
176
+ (acc, c) => {
177
+ return acc + c[1]
178
+ },
179
+ 0
180
+ )
181
+ const ageCategory = ageCategoryCounts[`guests-${a.id}`]
182
+
183
+ if (
184
+ !totalAgeCategories ||
185
+ (totalAgeCategories === 1 && ageCategory === 1)
186
+ ) {
187
+ ageCategoryCounts[`guests-${a.id}`] = 2
188
+ } else if (totalAgeCategories > 1 && !ageCategory) {
189
+ ageCategoryCounts[`guests-${a.id}`] = a.minVal
190
+ }
191
+ }
192
+ })
193
+ }
194
+
195
+ return Object.entries(ageCategoryCounts).length
196
+ ? JSON.stringify(ageCategoryCounts)
197
+ : ''
198
+ }