@kaspernj/api-maker 1.0.236 → 1.0.237

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/.eslintrc.cjs CHANGED
@@ -100,6 +100,7 @@ module.exports = {
100
100
  "line-comment-position": "off",
101
101
  "lines-around-comment": "error",
102
102
  "lines-between-class-members": "off",
103
+ "logical-assignment-operators": "error",
103
104
  "max-classes-per-file": "off",
104
105
  "max-depth": "error",
105
106
  "max-nested-callbacks": "error",
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.236",
19
+ "version": "1.0.237",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -16,7 +16,7 @@ export default class Serializer {
16
16
  }
17
17
 
18
18
  serializeArgument (arg) {
19
- if (typeof arg == "object" && arg.constructor.apiMakerType == "BaseModel") {
19
+ if (typeof arg == "object" && arg && arg.constructor.apiMakerType == "BaseModel") {
20
20
  return {
21
21
  api_maker_type: "model",
22
22
  model_class_name: digg(arg.modelClassData(), "name"),
@@ -44,7 +44,7 @@ export default class Serializer {
44
44
  }
45
45
  } else if (Array.isArray(arg)) {
46
46
  return this.serializeArray(arg)
47
- } else if (typeof arg == "object" && arg.constructor && arg.constructor.apiMakerType == "Collection") {
47
+ } else if (typeof arg == "object" && arg && arg.constructor && arg.constructor.apiMakerType == "Collection") {
48
48
  return {
49
49
  api_maker_type: "collection",
50
50
  value: this.serializeObject(arg)
@@ -4,6 +4,7 @@ import {digg} from "diggerize"
4
4
  import inflection from "inflection"
5
5
  import {serialize as objectToFormData} from "object-to-formdata"
6
6
  import {TableSetting} from "../models.mjs.erb"
7
+ import {v4 as uuidv4} from "uuid"
7
8
 
8
9
  export default class ApiMakerTableSettings {
9
10
  constructor({table}) {
@@ -62,13 +63,13 @@ export default class ApiMakerTableSettings {
62
63
  }
63
64
 
64
65
  loadTableSetting = async () => {
65
- if (!TableSetting) throw new Error("TableSetting model couldn't be imported")
66
+ if (!TableSetting) throw new Error("TableSetting model isn't globally available")
66
67
 
67
68
  const tableSetting = await TableSetting
68
69
  .ransack({
69
70
  identifier_eq: this.identifier(),
70
- user_id_eq: this.currentUser().id(),
71
- user_type_eq: digg(this.currentUser().modelClassData(), "name")
71
+ user_id_eq: this.currentUserIdOrFallback(),
72
+ user_type_eq: this.currentUserTypeOrFallback()
72
73
  })
73
74
  .preload("columns")
74
75
  .first()
@@ -76,11 +77,39 @@ export default class ApiMakerTableSettings {
76
77
  return tableSetting
77
78
  }
78
79
 
80
+ currentUserIdOrFallback() {
81
+ const currentUser = this.currentUser()
82
+
83
+ if (currentUser) return currentUser.id()
84
+
85
+ return this.anonymouseUserId()
86
+ }
87
+
88
+ currentUserTypeOrFallback() {
89
+ const currentUser = this.currentUser()
90
+
91
+ if (currentUser) return digg(currentUser.modelClassData(), "name")
92
+
93
+ return null
94
+ }
95
+
96
+ anonymouseUserId() {
97
+ const variableName = `ApiMakerTableAnonymousUserId-${this.identifier()}`
98
+
99
+ if (!(variableName in localStorage)) {
100
+ const generatedId = uuidv4()
101
+
102
+ localStorage[variableName] = generatedId
103
+ }
104
+
105
+ return digg(localStorage, variableName)
106
+ }
107
+
79
108
  createInitialTableSetting = async () => {
80
109
  const tableSettingData = {
81
110
  identifier: this.identifier(),
82
- user_id: this.currentUser().id(),
83
- user_type: digg(this.currentUser().modelClassData(), "name"),
111
+ user_id: this.currentUserIdOrFallback(),
112
+ user_type: this.currentUserTypeOrFallback(),
84
113
  columns_attributes: {}
85
114
  }
86
115
 
@@ -116,9 +145,6 @@ export default class ApiMakerTableSettings {
116
145
  }
117
146
 
118
147
  updateTableSetting = async (tableSetting) => {
119
- // This should remove columns no longer found
120
- // This should update columns that have changed
121
-
122
148
  const columns = this.columns()
123
149
  const columnsData = {}
124
150
  const tableSettingData = {columns_attributes: columnsData}
@@ -149,7 +175,7 @@ export default class ApiMakerTableSettings {
149
175
  const column = columns.find((column) => columnIdentifier(column) == tableSettingColumn.identifier())
150
176
 
151
177
  if (column) {
152
- // Update column if changed
178
+ // TODO: Update column if changed
153
179
  } else {
154
180
  // Removed saved columns no longer found
155
181
  const columnKey = ++columnsKeyCount
@@ -158,6 +184,7 @@ export default class ApiMakerTableSettings {
158
184
  id: tableSettingColumn.id(),
159
185
  _destroy: true
160
186
  }
187
+ changed = true
161
188
  }
162
189
  }
163
190
 
@@ -165,9 +192,6 @@ export default class ApiMakerTableSettings {
165
192
  const tableSettingFormData = objectToFormData({table_setting: tableSettingData})
166
193
 
167
194
  await tableSetting.saveRaw(tableSettingFormData)
168
-
169
- // Maybe not necessary?
170
- // tableSetting = this.loadTableSetting()
171
195
  }
172
196
 
173
197
  return tableSetting