@star-insure/sdk 1.1.42 → 1.2.0

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.
@@ -1,84 +1,124 @@
1
- import React from "react";
2
- import { padStart } from "lodash-es";
1
+ import React from 'react';
2
+ import { calculateAge } from '../../lib';
3
+ import { padStart } from 'lodash';
3
4
 
4
5
  interface Props {
5
- value?: string;
6
- onChange: (newValue: string) => void;
6
+ name?: string;
7
+ id?: string;
8
+ onChange: (dateString: string) => void;
9
+ value?: string;
10
+ maxYear?: number;
11
+ showAge?: boolean;
7
12
  }
8
13
 
9
- export default function DateOfBirthField({ value, onChange }: Props) {
10
- const [dateOfBirth, setDateOfBirth] = React.useState({
11
- day: value?.split("-")[2] || '',
12
- month: value?.split("-")[1] || '',
13
- year: value?.split("-")[0] || '',
14
- });
14
+ export default function DateOfBirthField({
15
+ name = 'dob',
16
+ id = 'dob',
17
+ onChange,
18
+ value,
19
+ maxYear = 0,
20
+ showAge = false,
21
+ }: Props) {
22
+ const dayOptions = [...Array.from(Array(31).keys())].map(value => {
23
+ return padStart(`${value + 1}`, 2, '0');
24
+ });
15
25
 
16
- const dayOptions = [...Array.from(Array(31).keys())].map(value => {
17
- return padStart(`${value + 1}`, 2, '0');
18
- });
26
+ const months = [
27
+ 'January',
28
+ 'February',
29
+ 'March',
30
+ 'April',
31
+ 'May',
32
+ 'June',
33
+ 'July',
34
+ 'August',
35
+ 'September',
36
+ 'October',
37
+ 'November',
38
+ 'December',
39
+ ];
40
+ const monthOptions = [...Array.from(Array(12).keys())].map(value => {
41
+ return {
42
+ title: months[value],
43
+ value: padStart(`${value + 1}`, 2, '0'),
44
+ };
45
+ });
19
46
 
20
- const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
21
- const monthOptions = [...Array.from(Array(12).keys())].map(value => {
22
- return {
23
- title: months[value],
24
- value: padStart(`${value + 1}`, 2, '0')
25
- }
26
- })
47
+ // Create 100 years of options, subtracting the maximum year if provided
48
+ const yearOptions = [...Array.from(Array(100).keys())].map(value => {
49
+ return maxYear ? maxYear - value : new Date().getFullYear() - value;
50
+ });
27
51
 
28
- const thisYear = new Date().getFullYear();
29
- const yearOptions = [...Array.from(Array(100).keys())].map(value => {
30
- return (thisYear - value - 16).toString();
31
- });
52
+ function handleChange(
53
+ e: React.ChangeEvent<
54
+ HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement
55
+ >
56
+ ) {
57
+ const { name, value } = e.currentTarget;
32
58
 
33
- function handleSelectChange(e: React.FormEvent<HTMLSelectElement>) {
34
- const { name, value } = e.currentTarget;
59
+ let y = name.includes('year') ? value : year;
60
+ let m = name.includes('month') ? value : month;
61
+ let d = name.includes('day') ? value : day;
35
62
 
36
- const newDob = {
37
- ...dateOfBirth,
38
- [name]: value,
39
- }
63
+ onChange(`${y}-${m}-${d}`);
64
+ }
40
65
 
41
- // Set local state
42
- setDateOfBirth(newDob);
66
+ const [year, month, day] = value ? value.split('-') : ['', '', ''];
67
+ const isValidDate =
68
+ year && month && day && !isNaN(Date.parse(`${year}-${month}-${day}`));
43
69
 
44
- // Format the date for the API
45
- const formattedDate = `${newDob.year}-${newDob.month}-${newDob.day}`;
46
-
47
- // Call the upstream prop if we have all of the date parts
48
- if (Object.values(newDob).every(value => value !== '')) {
49
- onChange(formattedDate);
50
- } else {
51
- // Otherwise, clear the value
52
- onChange('');
53
- }
54
- }
55
-
56
- return (
57
- <label className="w-full">
58
- <span className="relative mr-auto">
59
- <span>Date of birth</span>
60
- </span>
61
-
62
- <span className="flex space-x-4 border border-gray-300 rounded-lg mt-1 bg-white">
63
- <select name="day" value={dateOfBirth.day} className="flex-grow focus:outline-none border-0" onChange={handleSelectChange} required>
64
- <option value="">Day</option>
65
- {dayOptions.map(option => (
66
- <option key={option} value={option}>{option}</option>
67
- ))}
68
- </select>
69
- <select name="month" value={dateOfBirth.month} className="flex-grow focus:outline-none border-0" onChange={handleSelectChange} required>
70
- <option value="">Month</option>
71
- {monthOptions.map(option => (
72
- <option key={option.value} value={option.value}>{option.title}</option>
73
- ))}
74
- </select>
75
- <select name="year" value={dateOfBirth.year} className="flex-grow focus:outline-none border-0" onChange={handleSelectChange} required>
76
- <option value="">Year</option>
77
- {yearOptions.map(option => (
78
- <option key={option} value={option}>{option}</option>
79
- ))}
80
- </select>
81
- </span>
82
- </label>
83
- )
70
+ return (
71
+ <span className="flex flex-col gap-2">
72
+ <span className="flex space-x-4 border border-gray-300 rounded-lg bg-white">
73
+ <select
74
+ name={`${name}_day`}
75
+ id={`${id}_day`}
76
+ value={day}
77
+ className="flex-grow focus:outline-none border-0"
78
+ onChange={handleChange}
79
+ required
80
+ >
81
+ <option value="">Day</option>
82
+ {dayOptions.map(option => (
83
+ <option key={option} value={option}>
84
+ {option}
85
+ </option>
86
+ ))}
87
+ </select>
88
+ <select
89
+ name={`${name}_month`}
90
+ id={`${id}_month`}
91
+ value={month}
92
+ className="flex-grow focus:outline-none border-0"
93
+ onChange={handleChange}
94
+ required
95
+ >
96
+ <option value="">Month</option>
97
+ {monthOptions.map(option => (
98
+ <option key={option.value} value={option.value}>
99
+ {option.title}
100
+ </option>
101
+ ))}
102
+ </select>
103
+ <select
104
+ name={`${name}_year`}
105
+ id={`${id}_year`}
106
+ value={year}
107
+ className="flex-grow focus:outline-none border-0"
108
+ onChange={handleChange}
109
+ required
110
+ >
111
+ <option value="">Year</option>
112
+ {yearOptions.map(option => (
113
+ <option key={option} value={option}>
114
+ {option}
115
+ </option>
116
+ ))}
117
+ </select>
118
+ </span>
119
+ {showAge && value && isValidDate && (
120
+ <span className="font-bold">Age: {calculateAge(value)} years</span>
121
+ )}
122
+ </span>
123
+ );
84
124
  }
@@ -2,4 +2,5 @@ export interface QuoteRequestIncident {
2
2
  year?: string;
3
3
  month?: string;
4
4
  description?: string;
5
+ key?: string;
5
6
  }
@@ -8,4 +8,5 @@ export interface QuoteRequestLog {
8
8
  user?: User;
9
9
  activity_type: string;
10
10
  message: string;
11
+ key?: string;
11
12
  }
@@ -5,4 +5,5 @@ export interface QuoteRequestReferrer {
5
5
  name: string;
6
6
  category_id: QuoteRequestReferrerCategory['id'];
7
7
  category?: QuoteRequestReferrerCategory;
8
+ key?: string;
8
9
  }
@@ -1,4 +1,5 @@
1
1
  export interface QuoteRequestReferrerCategory {
2
2
  id: number;
3
3
  name: string;
4
+ key?: string;
4
5
  }