@devtron-labs/devtron-fe-common-lib 0.0.60 → 0.0.61-beta-2

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.
Files changed (84) hide show
  1. package/dist/Common/Api.d.ts +9 -0
  2. package/dist/Common/Constants.d.ts +1 -0
  3. package/dist/Common/Types.d.ts +3 -2
  4. package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraConfigForm.d.ts +4 -0
  5. package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraDescriptionField.d.ts +4 -0
  6. package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormAction.d.ts +7 -0
  7. package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormItem.d.ts +4 -0
  8. package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraProfileNameField.d.ts +4 -0
  9. package/dist/Pages/GlobalConfigurations/BuildInfra/Descriptor.d.ts +4 -0
  10. package/dist/Pages/GlobalConfigurations/BuildInfra/Footer.d.ts +4 -0
  11. package/dist/Pages/GlobalConfigurations/BuildInfra/constants.d.ts +50 -0
  12. package/dist/Pages/GlobalConfigurations/BuildInfra/index.d.ts +7 -0
  13. package/dist/Pages/GlobalConfigurations/BuildInfra/services.d.ts +6 -0
  14. package/dist/Pages/GlobalConfigurations/BuildInfra/types.d.ts +286 -0
  15. package/dist/Pages/GlobalConfigurations/BuildInfra/utils.d.ts +12 -0
  16. package/dist/Pages/GlobalConfigurations/index.d.ts +1 -0
  17. package/dist/Pages/index.d.ts +1 -0
  18. package/dist/Shared/Components/APIResponseHandler/APIResponseHandler.d.ts +4 -0
  19. package/dist/Shared/Components/APIResponseHandler/constants.d.ts +4 -0
  20. package/dist/Shared/Components/APIResponseHandler/index.d.ts +2 -0
  21. package/dist/Shared/Components/APIResponseHandler/types.d.ts +37 -0
  22. package/dist/Shared/Components/BulkSelection/BulkSelection.d.ts +4 -0
  23. package/dist/Shared/Components/BulkSelection/BulkSelectionDropdownItems.d.ts +4 -0
  24. package/dist/Shared/Components/BulkSelection/BulkSelectionProvider.d.ts +4 -0
  25. package/dist/Shared/Components/BulkSelection/constants.d.ts +9 -0
  26. package/dist/Shared/Components/BulkSelection/index.d.ts +4 -0
  27. package/dist/Shared/Components/BulkSelection/types.d.ts +54 -0
  28. package/dist/Shared/Components/ReactSelect/index.d.ts +1 -0
  29. package/dist/Shared/Components/ReactSelect/utils.d.ts +12 -0
  30. package/dist/Shared/Components/index.d.ts +3 -0
  31. package/dist/Shared/Helpers.d.ts +16 -0
  32. package/dist/Shared/index.d.ts +3 -0
  33. package/dist/Shared/validations.d.ts +8 -0
  34. package/dist/index.d.ts +3 -1
  35. package/dist/index.js +1457 -158
  36. package/package.json +1 -1
  37. package/src/Assets/Icon/ic-check-all.svg +5 -0
  38. package/src/Assets/Icon/ic-check-square.svg +4 -0
  39. package/src/Assets/Icon/ic-close.svg +9 -0
  40. package/src/Assets/Icon/ic-cpu.svg +4 -0
  41. package/src/Assets/Icon/ic-memory.svg +3 -0
  42. package/src/Assets/Icon/ic-timer.svg +3 -0
  43. package/src/Common/Api.ts +21 -0
  44. package/src/Common/BreadCrumb/Types.ts +0 -1
  45. package/src/Common/Checkbox.tsx +1 -0
  46. package/src/Common/Constants.ts +2 -1
  47. package/src/Common/CustomInput/CustomInput.tsx +2 -1
  48. package/src/Common/DraggableWrapper/DraggableWrapper.tsx +3 -1
  49. package/src/Common/RJSF/utils.ts +2 -0
  50. package/src/Common/SearchBar/SearchBar.component.tsx +23 -11
  51. package/src/Common/SearchBar/searchBar.scss +1 -1
  52. package/src/Common/Types.ts +3 -1
  53. package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraConfigForm.tsx +128 -0
  54. package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraDescriptionField.tsx +44 -0
  55. package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormAction.tsx +106 -0
  56. package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormItem.tsx +55 -0
  57. package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraProfileNameField.tsx +32 -0
  58. package/src/Pages/GlobalConfigurations/BuildInfra/Descriptor.tsx +45 -0
  59. package/src/Pages/GlobalConfigurations/BuildInfra/Footer.tsx +56 -0
  60. package/src/Pages/GlobalConfigurations/BuildInfra/Readme.md +4 -0
  61. package/src/Pages/GlobalConfigurations/BuildInfra/constants.tsx +139 -0
  62. package/src/Pages/GlobalConfigurations/BuildInfra/index.ts +7 -0
  63. package/src/Pages/GlobalConfigurations/BuildInfra/services.tsx +167 -0
  64. package/src/Pages/GlobalConfigurations/BuildInfra/types.tsx +339 -0
  65. package/src/Pages/GlobalConfigurations/BuildInfra/utils.tsx +509 -0
  66. package/src/Pages/GlobalConfigurations/index.ts +1 -0
  67. package/src/Pages/index.ts +1 -0
  68. package/src/Shared/Components/APIResponseHandler/APIResponseHandler.tsx +41 -0
  69. package/src/Shared/Components/APIResponseHandler/constants.tsx +5 -0
  70. package/src/Shared/Components/APIResponseHandler/index.ts +2 -0
  71. package/src/Shared/Components/APIResponseHandler/types.tsx +39 -0
  72. package/src/Shared/Components/BulkSelection/BulkSelection.tsx +77 -0
  73. package/src/Shared/Components/BulkSelection/BulkSelectionDropdownItems.tsx +31 -0
  74. package/src/Shared/Components/BulkSelection/BulkSelectionProvider.tsx +160 -0
  75. package/src/Shared/Components/BulkSelection/constants.tsx +13 -0
  76. package/src/Shared/Components/BulkSelection/index.ts +4 -0
  77. package/src/Shared/Components/BulkSelection/types.tsx +65 -0
  78. package/src/Shared/Components/ReactSelect/index.ts +1 -0
  79. package/src/Shared/Components/ReactSelect/utils.tsx +97 -0
  80. package/src/Shared/Components/index.ts +3 -0
  81. package/src/Shared/Helpers.tsx +29 -0
  82. package/src/Shared/index.ts +3 -0
  83. package/src/Shared/validations.tsx +130 -0
  84. package/src/index.ts +3 -1
@@ -0,0 +1,31 @@
1
+ import { BulkSelectionDropdownItemsProps } from './types'
2
+
3
+ const BulkSelectionDropdownItems = <T,>({
4
+ locator,
5
+ label,
6
+ isSelected,
7
+ handleBulkSelection,
8
+ icon: Icon,
9
+ }: BulkSelectionDropdownItemsProps<T>) => {
10
+ const handleSelect = () => {
11
+ handleBulkSelection({
12
+ action: locator,
13
+ })
14
+ }
15
+
16
+ return (
17
+ <button
18
+ data-testid={`bulk-action-${locator}`}
19
+ onClick={handleSelect}
20
+ className={`dc__no-border dc__outline-none-imp w-100 flexbox h-32 dc__align-items-center pt-6 pr-8 pb-6 pl-8 dc__gap-8 ${
21
+ isSelected ? 'cb-5 bcb-2 fs-13 fw-6 lh-20' : 'dc__no-background cn-9 fs-13 fw-4 lh-20 dc__hover-n50'
22
+ }`}
23
+ type="button"
24
+ >
25
+ <Icon className="icon-dim-16" />
26
+ {label}
27
+ </button>
28
+ )
29
+ }
30
+
31
+ export default BulkSelectionDropdownItems
@@ -0,0 +1,160 @@
1
+ import { createContext, useContext, useMemo, useState } from 'react'
2
+ import { BULK_SELECTION_CONTEXT_ERROR, SELECT_ALL_ACROSS_PAGES_LOCATOR, getInvalidActionMessage } from './constants'
3
+ import {
4
+ BulkSelectionEvents,
5
+ GetBulkSelectionCheckboxValuesType,
6
+ HandleBulkSelectionType,
7
+ SelectAllDialogStatus,
8
+ UseBulkSelectionProps,
9
+ UseBulkSelectionReturnType,
10
+ } from './types'
11
+ import { CHECKBOX_VALUE, noop } from '../../../Common'
12
+
13
+ // giving type any here since not exporting this context, rather using it through useBulkSelection hook which is typed
14
+ const BulkSelectionContext = createContext<UseBulkSelectionReturnType<any>>({
15
+ selectedIdentifiers: {},
16
+ handleBulkSelection: noop,
17
+ isChecked: false,
18
+ checkboxValue: CHECKBOX_VALUE.CHECKED,
19
+ })
20
+
21
+ export const useBulkSelection = <T,>() => {
22
+ const context = useContext<UseBulkSelectionReturnType<T>>(BulkSelectionContext)
23
+ if (!context) {
24
+ throw new Error(BULK_SELECTION_CONTEXT_ERROR)
25
+ }
26
+ return context
27
+ }
28
+
29
+ export const BulkSelectionProvider = <T,>({
30
+ children,
31
+ identifiers,
32
+ getSelectAllDialogStatus,
33
+ }: UseBulkSelectionProps<T>) => {
34
+ const [selectedIdentifiers, setSelectedIdentifiers] = useState<T>({} as T)
35
+
36
+ const setIdentifiersAfterClear = (newIdentifiers: T, selectedIds: (number | string)[]) => {
37
+ const _newIdentifiers = JSON.parse(JSON.stringify(newIdentifiers))
38
+ selectedIds.forEach((id) => {
39
+ delete _newIdentifiers[id]
40
+ })
41
+ setSelectedIdentifiers(_newIdentifiers)
42
+ }
43
+
44
+ const setIdentifiersAfterPageSelection = (baseObject: T) => {
45
+ const _selectedIdentifiers = JSON.parse(JSON.stringify(selectedIdentifiers))
46
+ // removing bulk selection across pages if present
47
+ if (_selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
48
+ delete _selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]
49
+ }
50
+
51
+ setSelectedIdentifiers({
52
+ ..._selectedIdentifiers,
53
+ ...baseObject,
54
+ })
55
+ }
56
+
57
+ const handleBulkSelection = ({ action, data }: HandleBulkSelectionType<T>) => {
58
+ const selectedIds = data?.identifierIds ?? []
59
+ const identifierObject = data?.identifierObject ?? {}
60
+
61
+ switch (action) {
62
+ case BulkSelectionEvents.CLEAR_ALL_SELECTIONS:
63
+ setSelectedIdentifiers({} as T)
64
+ break
65
+
66
+ case BulkSelectionEvents.CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION: {
67
+ setIdentifiersAfterClear(identifiers, selectedIds)
68
+ break
69
+ }
70
+
71
+ case BulkSelectionEvents.CLEAR_IDENTIFIERS: {
72
+ setIdentifiersAfterClear(selectedIdentifiers, selectedIds)
73
+ break
74
+ }
75
+
76
+ case BulkSelectionEvents.SELECT_ALL_ACROSS_PAGES: {
77
+ if (selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
78
+ return
79
+ }
80
+ const selectedIdentifiersLength = Object.keys(selectedIdentifiers).length
81
+ if (selectedIdentifiersLength > 0 && getSelectAllDialogStatus() === SelectAllDialogStatus.OPEN) {
82
+ return
83
+ }
84
+
85
+ setSelectedIdentifiers({
86
+ [SELECT_ALL_ACROSS_PAGES_LOCATOR]: true,
87
+ } as unknown as T)
88
+ break
89
+ }
90
+
91
+ case BulkSelectionEvents.CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES:
92
+ setSelectedIdentifiers({
93
+ [SELECT_ALL_ACROSS_PAGES_LOCATOR]: true,
94
+ } as unknown as T)
95
+ break
96
+
97
+ case BulkSelectionEvents.SELECT_ALL_ON_PAGE: {
98
+ setIdentifiersAfterPageSelection(identifiers)
99
+ break
100
+ }
101
+
102
+ case BulkSelectionEvents.SELECT_IDENTIFIER: {
103
+ setIdentifiersAfterPageSelection(identifierObject as T)
104
+ break
105
+ }
106
+
107
+ default:
108
+ throw new Error(getInvalidActionMessage(action))
109
+ }
110
+ }
111
+
112
+ const getBulkSelectionCheckboxValues = (): GetBulkSelectionCheckboxValuesType => {
113
+ const selectedIdentifiersArray = Object.keys(selectedIdentifiers)
114
+ if (selectedIdentifiersArray.length === 0) {
115
+ return {
116
+ isChecked: false,
117
+ checkboxValue: CHECKBOX_VALUE.CHECKED,
118
+ }
119
+ }
120
+
121
+ // if selectedIdentifiers contains select all across pages locator then it means all are selected
122
+ if (selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
123
+ return {
124
+ isChecked: true,
125
+ checkboxValue: CHECKBOX_VALUE.BULK_CHECKED,
126
+ }
127
+ }
128
+
129
+ // if all the identifiers are selected then CHECKED else intermediate
130
+ const areAllPresentIdentifiersSelected = Object.keys(identifiers).every(
131
+ (identifierId) => selectedIdentifiers[identifierId],
132
+ )
133
+
134
+ if (areAllPresentIdentifiersSelected) {
135
+ return {
136
+ isChecked: true,
137
+ checkboxValue: CHECKBOX_VALUE.CHECKED,
138
+ }
139
+ }
140
+
141
+ return {
142
+ isChecked: true,
143
+ checkboxValue: CHECKBOX_VALUE.INTERMEDIATE,
144
+ }
145
+ }
146
+
147
+ const { isChecked, checkboxValue } = getBulkSelectionCheckboxValues()
148
+
149
+ const value = useMemo(
150
+ () => ({
151
+ selectedIdentifiers,
152
+ handleBulkSelection,
153
+ isChecked,
154
+ checkboxValue,
155
+ }),
156
+ [selectedIdentifiers, handleBulkSelection, isChecked, checkboxValue],
157
+ )
158
+
159
+ return <BulkSelectionContext.Provider value={value}>{children}</BulkSelectionContext.Provider>
160
+ }
@@ -0,0 +1,13 @@
1
+ import { BulkSelectionEvents } from './types'
2
+
3
+ export const BULK_DROPDOWN_TEST_ID = 'bulk-selection-pop-up-menu' as const
4
+ export const BulkSelectionOptionsLabels = {
5
+ [BulkSelectionEvents.SELECT_ALL_ON_PAGE]: 'All on this page',
6
+ [BulkSelectionEvents.SELECT_ALL_ACROSS_PAGES]: 'All across pages',
7
+ [BulkSelectionEvents.CLEAR_ALL_SELECTIONS]: 'Clear selection',
8
+ } as const
9
+
10
+ // Considering application can not be named as *
11
+ export const SELECT_ALL_ACROSS_PAGES_LOCATOR = '*' as const
12
+ export const getInvalidActionMessage = (action: string) => `Invalid action ${action} passed to useBulkSelection`
13
+ export const BULK_SELECTION_CONTEXT_ERROR = 'useBulkSelection must be used within BulkSelectionContext'
@@ -0,0 +1,4 @@
1
+ export { default as BulkSelection } from './BulkSelection'
2
+ export * from './BulkSelectionProvider'
3
+ export * from './types'
4
+ export * from './constants'
@@ -0,0 +1,65 @@
1
+ import { CHECKBOX_VALUE } from '../../../Common'
2
+
3
+ export enum BulkSelectionEvents {
4
+ CLEAR_ALL_SELECTIONS = 'CLEAR_ALL_SELECTIONS',
5
+ CLEAR_IDENTIFIERS = 'CLEAR_IDENTIFIERS',
6
+ SELECT_ALL_ACROSS_PAGES = 'SELECT_ALL_ACROSS_PAGES',
7
+ SELECT_ALL_ON_PAGE = 'SELECT_ALL_ON_PAGE',
8
+ SELECT_IDENTIFIER = 'SELECT_IDENTIFIER',
9
+ CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION = 'CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION',
10
+ CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES = 'CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES',
11
+ }
12
+
13
+ export interface HandleBulkSelectionType<T> {
14
+ action: BulkSelectionEvents
15
+ data?: {
16
+ identifierIds?: (number | string)[]
17
+ identifierObject?: T
18
+ }
19
+ }
20
+
21
+ export interface GetBulkSelectionCheckboxValuesType {
22
+ isChecked: boolean
23
+ checkboxValue: CHECKBOX_VALUE
24
+ }
25
+
26
+ export interface UseBulkSelectionReturnType<T> extends GetBulkSelectionCheckboxValuesType {
27
+ selectedIdentifiers: T
28
+ handleBulkSelection: ({ action, data }: HandleBulkSelectionType<T>) => void
29
+ }
30
+
31
+ export interface BulkSelectionProps {
32
+ showPagination: boolean
33
+ }
34
+
35
+ export interface BulkSelectionDropdownItemsType {
36
+ locator: BulkSelectionEvents
37
+ label: string
38
+ isSelected: boolean
39
+ icon: React.FunctionComponent<React.SVGProps<SVGSVGElement>>
40
+ }
41
+
42
+ export interface BulkSelectionDropdownItemsProps<T>
43
+ extends BulkSelectionDropdownItemsType,
44
+ Pick<UseBulkSelectionReturnType<T>, 'handleBulkSelection'> {}
45
+
46
+ export enum SelectAllDialogStatus {
47
+ OPEN = 'OPEN',
48
+ CLOSED = 'CLOSED',
49
+ }
50
+
51
+ // This type is intended to be used with UseBulkSelectionProps, e.g, UseBulkSelectionProps<BulkSelectionIdentifiersType<boolean>>
52
+ export type BulkSelectionIdentifiersType<T> = Record<string | number, T>
53
+
54
+ export interface UseBulkSelectionProps<T> {
55
+ /**
56
+ * Response from API, assuming structure to be array of objects with key and values
57
+ * This will the given ids on current page
58
+ */
59
+ identifiers: T
60
+ /**
61
+ * Act as buffer between select all across pages and select all on page state
62
+ */
63
+ getSelectAllDialogStatus: () => SelectAllDialogStatus
64
+ children?: React.ReactNode
65
+ }
@@ -0,0 +1 @@
1
+ export * from './utils'
@@ -0,0 +1,97 @@
1
+ import Tippy from '@tippyjs/react'
2
+ import { components } from 'react-select'
3
+ import { stopPropagation } from '../../../Common'
4
+
5
+ export const getCommonSelectStyle = (styleOverrides = {}) => ({
6
+ menuList: (base) => ({
7
+ ...base,
8
+ paddingTop: 0,
9
+ paddingBottom: 0,
10
+ }),
11
+ control: (base, state) => ({
12
+ ...base,
13
+ minHeight: '32px',
14
+ boxShadow: 'none',
15
+ backgroundColor: 'var(--N50)',
16
+ border: state.isFocused ? '1px solid var(--B500)' : '1px solid var(--N200)',
17
+ cursor: 'pointer',
18
+ }),
19
+ option: (base, state) => ({
20
+ ...base,
21
+ color: 'var(--N900)',
22
+ backgroundColor: state.isFocused ? 'var(--N100)' : 'white',
23
+ padding: '10px 12px',
24
+ }),
25
+ dropdownIndicator: (base, state) => ({
26
+ ...base,
27
+ color: 'var(--N400)',
28
+ padding: '0 8px',
29
+ transition: 'all .2s ease',
30
+ transform: state.selectProps.menuIsOpen ? 'rotate(180deg)' : 'rotate(0deg)',
31
+ }),
32
+ valueContainer: (base) => ({
33
+ ...base,
34
+ padding: '0 8px',
35
+ fontWeight: '400',
36
+ }),
37
+ loadingMessage: (base) => ({
38
+ ...base,
39
+ color: 'var(--N600)',
40
+ }),
41
+ noOptionsMessage: (base) => ({
42
+ ...base,
43
+ color: 'var(--N600)',
44
+ }),
45
+ ...styleOverrides,
46
+ })
47
+
48
+ const getCustomOptionBackgroundColor = (isSelected: boolean, isFocused: boolean) => {
49
+ if (isSelected) {
50
+ return 'var(--B100)'
51
+ }
52
+
53
+ if (isFocused) {
54
+ return 'var(--N50)'
55
+ }
56
+
57
+ return 'var(--white)'
58
+ }
59
+
60
+ export const getCustomOptionSelectionStyle =
61
+ (styleOverrides = {}) =>
62
+ (base, state) => ({
63
+ ...base,
64
+ backgroundColor: getCustomOptionBackgroundColor(state.isSelected, state.isFocused),
65
+ color: state.isSelected ? 'var(--B500)' : 'var(--N900)',
66
+ textOverflow: 'ellipsis',
67
+ fontWeight: '500',
68
+ overflow: 'hidden',
69
+ textAlign: 'left',
70
+ whiteSpace: 'nowrap',
71
+ cursor: 'pointer',
72
+ fontSize: '13px',
73
+ ...styleOverrides,
74
+ })
75
+
76
+ export const SelectOption = (props: any) => {
77
+ const { selectProps, data, showTippy, style, placement, tippyContent, tippyClass } = props
78
+ selectProps.styles.option = getCustomOptionSelectionStyle(style)
79
+ const getOption = () => (
80
+ <div onClick={stopPropagation}>
81
+ <components.Option {...props} />
82
+ </div>
83
+ )
84
+
85
+ return showTippy ? (
86
+ <Tippy
87
+ className={tippyClass || 'default-white'}
88
+ arrow={false}
89
+ placement={placement || 'right'}
90
+ content={tippyContent || data.label}
91
+ >
92
+ {getOption()}
93
+ </Tippy>
94
+ ) : (
95
+ getOption()
96
+ )
97
+ }
@@ -0,0 +1,3 @@
1
+ export * from './APIResponseHandler'
2
+ export * from './ReactSelect'
3
+ export * from './BulkSelection'
@@ -0,0 +1,29 @@
1
+ interface HighlightSearchTextProps {
2
+ /**
3
+ * The text to be highlighted
4
+ */
5
+ searchText: string
6
+ /**
7
+ * The whole text string
8
+ */
9
+ text: string
10
+ /**
11
+ * The classes to be applied to the highlighted text
12
+ */
13
+ highlightClasses?: string
14
+ }
15
+
16
+ // Disabling default export since this is a helper function and we would have to export a lot of functions in future.
17
+ // eslint-disable-next-line import/prefer-default-export
18
+ export const highlightSearchText = ({ searchText, text, highlightClasses }: HighlightSearchTextProps): string => {
19
+ if (!searchText) {
20
+ return text
21
+ }
22
+
23
+ try {
24
+ const regex = new RegExp(searchText, 'gi')
25
+ return text.replace(regex, (match) => `<span class="${highlightClasses}">${match}</span>`)
26
+ } catch (error) {
27
+ return text
28
+ }
29
+ }
@@ -0,0 +1,3 @@
1
+ export * from './Components'
2
+ export * from './validations'
3
+ export * from './Helpers'
@@ -0,0 +1,130 @@
1
+ export interface ValidationResponseType {
2
+ isValid: boolean
3
+ message?: string
4
+ }
5
+
6
+ const MESSAGES = {
7
+ PROVIDE_A_NUMBER: 'Please provide a number',
8
+ LOWERCASE_ALPHANUMERIC: 'Only lowercase alphanumeric characters, -, _ or . allowed',
9
+ CAN_NOT_START_END_WITH_SEPARATORS: 'Cannot start/end with -, _ or .',
10
+ getMinMaxCharMessage: (min: number, max: number) => `Minimum ${min} and maximum ${max} characters allowed`,
11
+ getMaxCharMessage: (max: number) => `Maximum ${max} characters are allowed`,
12
+ VALID_POSITIVE_NUMBER: 'This field should be a valid positive number',
13
+ VALID_POSITIVE_INTEGER: 'This field should be a valid positive integer',
14
+ MAX_SAFE_INTEGER: `Maximum allowed value is ${Number.MAX_SAFE_INTEGER}`,
15
+ }
16
+
17
+ const MAX_DESCRIPTION_LENGTH = 300
18
+
19
+ export const validateName = (name: string): ValidationResponseType => {
20
+ if (!/^.{3,50}$/.test(name)) {
21
+ return {
22
+ isValid: false,
23
+ message: MESSAGES.getMinMaxCharMessage(3, 50),
24
+ }
25
+ }
26
+
27
+ if (!/^[a-z0-9-._]+$/.test(name)) {
28
+ return {
29
+ isValid: false,
30
+ message: MESSAGES.LOWERCASE_ALPHANUMERIC,
31
+ }
32
+ }
33
+
34
+ if (!/^(?![-._]).*[^-._]$/.test(name)) {
35
+ return {
36
+ isValid: false,
37
+ message: MESSAGES.CAN_NOT_START_END_WITH_SEPARATORS,
38
+ }
39
+ }
40
+
41
+ return {
42
+ isValid: true,
43
+ }
44
+ }
45
+
46
+ export const validateDescription = (description: string): ValidationResponseType => {
47
+ if (description?.length > MAX_DESCRIPTION_LENGTH) {
48
+ return {
49
+ isValid: false,
50
+ message: MESSAGES.getMaxCharMessage(MAX_DESCRIPTION_LENGTH),
51
+ }
52
+ }
53
+
54
+ return {
55
+ isValid: true,
56
+ }
57
+ }
58
+
59
+ export const validateRequiredPositiveNumber = (value: string | number): ValidationResponseType => {
60
+ if (!value) {
61
+ return {
62
+ isValid: false,
63
+ message: MESSAGES.PROVIDE_A_NUMBER,
64
+ }
65
+ }
66
+
67
+ // TODO: Look if regex is available for this, since 0002 is not a valid number
68
+ if (!/^\d+(\.\d+)?$/.test(value.toString())) {
69
+ return {
70
+ isValid: false,
71
+ message: MESSAGES.VALID_POSITIVE_NUMBER,
72
+ }
73
+ }
74
+
75
+ const numericValue = Number(value)
76
+
77
+ if (numericValue > Number.MAX_SAFE_INTEGER) {
78
+ return {
79
+ isValid: false,
80
+ message: MESSAGES.MAX_SAFE_INTEGER,
81
+ }
82
+ }
83
+
84
+ if (numericValue <= 0) {
85
+ return {
86
+ isValid: false,
87
+ message: MESSAGES.VALID_POSITIVE_NUMBER,
88
+ }
89
+ }
90
+
91
+ return {
92
+ isValid: true,
93
+ }
94
+ }
95
+
96
+ export const validateRequiredPositiveInteger = (value: string | number): ValidationResponseType => {
97
+ if (!value) {
98
+ return {
99
+ isValid: false,
100
+ message: MESSAGES.PROVIDE_A_NUMBER,
101
+ }
102
+ }
103
+
104
+ if (!/^\d+$/.test(value.toString())) {
105
+ return {
106
+ isValid: false,
107
+ message: MESSAGES.VALID_POSITIVE_INTEGER,
108
+ }
109
+ }
110
+
111
+ const numericValue = Number(value)
112
+
113
+ if (numericValue > Number.MAX_SAFE_INTEGER) {
114
+ return {
115
+ isValid: false,
116
+ message: MESSAGES.MAX_SAFE_INTEGER,
117
+ }
118
+ }
119
+
120
+ if (numericValue <= 0) {
121
+ return {
122
+ isValid: false,
123
+ message: MESSAGES.VALID_POSITIVE_INTEGER,
124
+ }
125
+ }
126
+
127
+ return {
128
+ isValid: true,
129
+ }
130
+ }
package/src/index.ts CHANGED
@@ -1 +1,3 @@
1
- export * from "./Common"
1
+ export * from './Common'
2
+ export * from './Pages'
3
+ export * from './Shared'