Browse Source

+invalidate +fix

Ivan Asmer 4 years ago
parent
commit
1d078de019
3 changed files with 39 additions and 23 deletions
  1. 2 1
      src/App.css
  2. 16 15
      src/App.js
  3. 21 7
      src/front-models.js

+ 2 - 1
src/App.css

@@ -49,6 +49,7 @@
 .Row {
     display: flex;
     justify-content: space-between;
+    font-size: 14px;
 }
 
 .GridContent {
@@ -68,7 +69,7 @@
     border: 1px solid #BBB;
     padding: 10px;
     text-align: center;
-    font-size: 0.7em;
+    font-size: 0.9em;
     
 }
 

+ 16 - 15
src/App.js

@@ -287,11 +287,9 @@ const ForeignAutocomplete = ({models={}, children:value, onChange, model, exclud
                 }}
                 value={value && {value: value._id, label: shortName(value), record: value}}
                 onChange={(obj) => {
-                    debugger;
                     onChange(obj.record._id ? obj.record : null)
                 }}
                 />
-
     )
 }
 
@@ -344,20 +342,23 @@ const defaultAdminOptions =
                     return model.fields[0].name === '_id' ? <ObjectShortEdit children={children}  model={model} {...props}/> : <EditForm models={models} record={children} options={options} />
                 },
                 Array: ({children, onChange, ...props}) => {
-                    return (<><SortableContainer onSortEnd={({newIndex, oldIndex}) => {
-                                    onChange(arrayMove(children, newIndex, oldIndex))
+                    return (<><SortableContainer 
+                                    pressDelay={200}
+                                    onSortEnd={({newIndex, oldIndex}) => {
+                                        onChange(arrayMove(children, newIndex, oldIndex))
                                 }}>{children.map((child, i) => 
-                        <SortableCell onDelete={() => onChange(children.filter((item, j) => j !== i))} 
-                                      onAdd={() => onChange([...children.slice(0, i),null,...children.slice(i)])} 
-                                    index={i} key={(child && (child._id || child.key)) || i} {...props} children={child} selected 
-                                onCh={data => {
-                                                        const copy = [...children]
-                                                        debugger;
-                                                        copy[i] = (data && data.target && data.target.value) || data
-                                                        onChange(copy)
-                                }}
-                            />)}
-                        </SortableContainer><button onClick={() => onChange([...children, null])}>+</button></>)
+                                    <SortableCell onDelete={() => onChange(children.filter((item, j) => j !== i))} 
+                                                  onAdd={() => onChange([...children.slice(0, i),null,...children.slice(i)])} 
+                                                index={i} key={(child && (child._id || child.key)) || i} {...props} children={child} selected 
+                                            onCh={data => {
+                                                                    const copy = [...children]
+                                                                    debugger;
+                                                                    copy[i] = (data && data.target && data.target.value) || data
+                                                                    onChange(copy)
+                                            }}
+                                        />)}
+                        </SortableContainer>
+                        <button onClick={() => onChange([...children, null])}>+</button></>)
                 }
             },
             fields:{

+ 21 - 7
src/front-models.js

@@ -104,6 +104,11 @@ export default async function createModels2(gql, config={create: 'Upsert', updat
     }
 
     const identityMap = {}
+    const identityMapInvalidate = (exclude) =>  {
+        for (let [id, entity] of Object.entries(identityMap)){
+            entity.empty = true;
+        }
+    }
     let   identityMapHits = 0
     let   totalObjects = 0
 
@@ -193,36 +198,45 @@ export default async function createModels2(gql, config={create: 'Upsert', updat
                     return this.input.inputFields
                 }
 
-                async save(){
-                    if (this.empty) throw new ReferenceError('Cannot save empty object')
+                get mutationData(){
                     const data = {}
                     input.inputFields.forEach(({name, type:{ ofType, kind, name:otherName}}) => {
                         ({SCALAR(){
+                            //if (this.constructor.name.startsWith('GoodOptionValue')) debugger;
                             if (this[name]) data[name] = this[name] 
                         },
                         LIST(){
                             const otherType = inInputs(ofType.name)
                             if (otherType  && this[name] && this[name] instanceof Array)
-                                data[name] = this[name].map(otherEntity => (otherEntity._id ? {_id: otherEntity._id} : otherEntity))
+                                data[name] = this[name].map(otherEntity => (otherEntity._id ? {_id: otherEntity._id} : (otherEntity.mutationData || otherEntity)))
                         },
                         INPUT_OBJECT(){
                             const otherType = inInputs(otherName)
                             if (otherType && this[name] && typeof this[name] === 'object')
-                                data[name] = (this[name]._id ? {_id: this[name]._id} : this[name])
+                                data[name] = (this[name]._id ? {_id: this[name]._id} : (this[name].mutationData || this[name]))
                             else data[name] = this[name]
                         }})[kind].call(this)
                     })
+                    return data
+                }
+
+
+
+                async save(){
+                    if (this.empty) throw new ReferenceError('Cannot save empty object')
                     const gqlQuery = `
                                 mutation ${name}Upsert($data: ${input.name}){
                                     ${name}Upsert(${name.toLowerCase()}: $data)
                                         ${projectionBuilder(type)}
-                                }
-                    `
-                    let result = await gql.request(gqlQuery, {data})
+                                }`
+
+                    let result = await gql.request(gqlQuery, {data: this.mutationData})
                     if (result._id && !(result._id in identityMap)){
                         identityMap[result._id] = this
                     }
+                    identityMapInvalidate()
                     this.populate(result)
+                    this.empty = false
                 }
 
                 static invalidate(except=[]){