Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
list changed to dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanflorentin committed Aug 24, 2016
1 parent 6e41af0 commit 3d41a67
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 57 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
},
"author": "Ivan Florentin <[email protected]>",
"license": "ISC",
"repository" :
{ "type" : "git"
, "url" : "https://github.com/ivanflorentin/ProvideAppCRUD"
},
"dependencies": {
"babel-core": "^6.7.4",
"babel-eslint": "^4.1.5",
Expand Down
48 changes: 19 additions & 29 deletions src/edit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {PropTypes} from 'react'
import {List, ListItem,ListSubHeader, Button, Input} from 'react-toolbox'
import {List, ListItem, ListSubHeader, Button, Input} from 'react-toolbox'

export default function (compDef) {
const {componentName,
Expand All @@ -12,23 +12,24 @@ export default function (compDef) {
fieldNames.map((fieldName)=>{
if (fields[fieldName].relation) {
const relation = fields[fieldName].relation
const relationProperName = relation.toUpperCase() + relation.substring(1)
const relationProperName = relation[0].toUpperCase() + relation.substring(1)
propTypes[`${relation}Routes`] = PropTypes.object
propTypes[`${relation}Templates`] = PropTypes.object
propTypes[`relationDisplay${relationProperName}`] = PropTypes.func
propTypes[`save${componentProperName}${relationProperName}`] = PropTypes.func
propTypes[`delete${componentProperName}${relationProperName}`] = PropTypes.func
propTypes[`${relation}ComponentDef`] = PropTypes.object
propTypes[`${relation}List`] = PropTypes.array
propTypes[`${relation}List`] = PropTypes.object
propTypes[relation] = PropTypes.object
}
})
const ComponentEdit = (props) =>{
const next = Object.assign({}, props[componentName])
console.log('next', next, props)
const save = () =>{props[`save${componentProperName}`](next)}
const store = () =>{props[`store${componentProperName}`](next)}
const {goBack} = props
const listCaption = `Ingrese datos de ${componentProperName}`


const listFields = fieldNames.map((fieldName)=>{
let label = fields[fieldName].label
if (!label || label === '') {
Expand All @@ -42,51 +43,40 @@ export default function (compDef) {
}
if (fields[fieldName].relation) {
const relation = fields[fieldName].relation
console.log('relation',props[relation])
const relationList = props[`${relation}List`]
const relationComponentDef = props[`${relation}ComponentDef`]
const relationProperName = relation.toUpperCase() + relation.substring(1)
const relationProperName = relation[0].toUpperCase() + relation.substring(1)
const saveRelation = props[`save${componentProperName}${relationProperName}`]
const deleteRelation = props[`delete${componentProperName}${relationProperName}`]
const routes = `${relation}Routes`
const listRoute = props[routes][`${relation}List`]

if (next[relation]===undefined) {next[relation]=[]}
if (next[`${relation}Selecting`]) {
if (next[relation].indexOf(props[relation].uuid)===-1) {
console.log('adding', props[relation].uuid)
next[relation].push(props[relation].uuid)
}
saveRelation(props[relation].uuid)
delete next[`${relation}Selecting`]
}
const relListFields= relationList.filter(comp =>{
if (next[relation].indexOf(comp.uuid)) {
return true
}
return false
}).map(comp=>{
const ids = Object.keys(relationList)
const relListFields = ids.map(id => {
const comp = relationList[id]
let thisComp = []
for (let key in comp) {
thisComp.push(<div key={key}>{key}: {comp[key]}</div>)
}
return <div key={comp.uuid}>{thisComp}
<Button icon='close' floating accent mini onClick={
() =>{
const idx = next[relation].indexOf(comp.uuid)
console.log(idx, next[relation][idx])
next[relation] = [
...next[relation].slice(0, idx),
...next[relation].slice(idx+1)]
save()
}
}/>
<Button icon='close' floating accent mini onClick={
() =>{
deleteRelation(comp.uuid)
}
}/>
</div>
})

return <div key={fieldName}>
<label>{relation}</label>
<List>{relListFields}</List>
<Button icon='add' onClick={() => {
next[`${relation}Selecting`] = true
save()
//save()
props.pushRoute(listRoute)
}}
/></div>
Expand Down
16 changes: 9 additions & 7 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export default function (compDef) {
const displayURL = `${url.slice(0, url.lastIndexOf('/')+1)}display`
const editURL = `${url.slice(0, url.lastIndexOf('/')+1)}edit`
const list = props[`${componentName}List`]
// console.log('list', list)
const Items = list.map((item, index) =>{
console.log('list', list)
const ids = Object.keys(list)
const Items = ids.map((id, index) =>{
const item = list[id]
let itemLegend = ''
for (let field in fields) {
let label = fields[field].label
Expand All @@ -28,23 +30,23 @@ export default function (compDef) {
rightActions={[
<Button key='edit' icon='edit' floating accent mini
onClick={() =>{
props[`select${componentProperName}`](props[`${componentName}List`][index])
props[`select${componentProperName}`](item)
props.pushRoute(editURL)
}}
/>,
<Button key='display' icon='description' floating accent mini
onClick={() =>{
props[`select${componentProperName}`](props[`${componentName}List`][index])
props[`select${componentProperName}`](item)
props.pushRoute(displayURL)
}}/>,
<Button key='select' icon='done' floating accent mini
onClick={() =>{
props[`select${componentProperName}`](props[`${componentName}List`][index])
props[`select${componentProperName}`](item)
props.goBack()
}}/>,
<Button key='delete' icon='close' floating accent mini
onClick={() =>{
props[`delete${componentProperName}`](props[`${componentName}List`][index])
props[`delete${componentProperName}`](item)
}}/>
]}
/>
Expand All @@ -71,7 +73,7 @@ export default function (compDef) {
pushRoute: PropTypes.func,
goBack: PropTypes.func
}
ComponentList.propTypes[`${compDef.componentName}List`] = PropTypes.array
ComponentList.propTypes[`${compDef.componentName}List`] = PropTypes.object
ComponentList.propTypes[`deselect${componentProperName}`] = PropTypes.func
ComponentList.propTypes[`select${componentProperName}`] = PropTypes.func
ComponentList.propTypes[`delete${componentProperName}`] = PropTypes.func
Expand Down
76 changes: 55 additions & 21 deletions src/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export default function (compDef) {
const templatesName = componentName + 'Templates'
const componentDefinitionName = componentName + 'ComponentDef'

let fieldNames = Object.keys(fields)

/*
const crudInit = {}
const crudErrorInit = {}
let fieldNames = Object.keys(fields)
if (fieldNames.length > 0) {
for (let fieldName of fieldNames) {
crudInit[fieldName] = fields[fieldName].defaultValue
crudErrorInit[`${fieldName}Error`] = ''
}}

*/
const SELECT = `SELECT_${componentCapitalName}`
const DESELECT = `DESELECT_${componentCapitalName}`
const SAVE = `SAVE_${componentCapitalName}`
Expand All @@ -36,6 +38,8 @@ export default function (compDef) {
const SET_TEMPLATE = `SET_TEMPLATE_${componentCapitalName}`
const DELETE_TEMPLATE = `DELETE_TEMPLATE_${componentCapitalName}`
const SET_COMPONENT_DEF = `SET_COMPOENET_DEF_${componentCapitalName}`
const SAVE_RELATION = `SAVE_RELATION`
const DELETE_RELATION = `DELETE_RELATION`

const actions = {}
const reducers = {}
Expand Down Expand Up @@ -74,6 +78,22 @@ export default function (compDef) {
return {type: SET_COMPONENT_DEF, componentDef}
}

if (fieldNames.length > 0) {
for (let fieldName of fieldNames) {
const fieldDef = fields[fieldName]
const relationName = fieldDef.relation
if (relationName !== undefined) {
const relationProperName = relationName[0].toUpperCase() + relationName.substring(1)
actions[`save${componentProperName}${relationProperName}`] = (id) =>{
return {type: SAVE_RELATION, fieldName, id}
}
actions[`delete${componentProperName}${relationProperName}`] = (id) =>{
return {type: DELETE_RELATION, fieldName, id}
}
}
}
}

reducers[componentDefinitionName] = (state = {}, action) =>{
switch (action.type) {
case SET_COMPONENT_DEF: {
Expand Down Expand Up @@ -122,6 +142,7 @@ export default function (compDef) {
reducers[componentName] = (state = {isValid: false}, action) =>{
switch (action.type) {
case SELECT: {
console.log('select', state, action)
return action.component
}
case DESELECT: {
Expand All @@ -147,37 +168,50 @@ export default function (compDef) {
}
return next
}
case SAVE_RELATION: {
const {fieldName, id} = action
if (id === undefined) {
return state
}
const next = Object.assign({}, state)
next[fieldName] = Object.assign({}, state[fieldName])
console.log ('save', fieldName, id, next)
if (next[fieldName] === undefined) {
next[fieldName] = {}
}
next[fieldName][id] = id
return next
}
case DELETE_RELATION: {
const next = Object.assign({}, state)
const {fieldName, id} = action
console.log ('delete', fieldName, id, state)
next[fieldName] = Object.assign({}, state[fieldName])
delete next[fieldName][id]
return next
}
default: return state
}
}

reducers[componentListName] = (state = [], action) =>{
reducers[componentListName] = (state = {}, action) =>{
switch (action.type) {
case LOAD: {
return action.list
}
case STORE: {
if (action.component.uuid === undefined) {
return [
...state,
Object.assign({}, action.component, {uuid: uuid.v4()})
]
}
const next = state.map((component)=>{
if (component.id === action.component.uuid) {
return action.component
}
return component
})
let id = ''
const next = {...state}
if (action.component.uuid) {
id = action.component.uuid
next[id] = Object.assign({}, action.component, {uuid: id})
} else { id = uuid.v4() }
next[id] = Object.assign({}, action.component, {uuid: id})
return next
}
case DELETE: {
let next = []
state.map((component) =>{
if (component.uuid !== action.component.uuid) {
next.push(component)
}
})
const next = Object.assign({}, state)
delete next[action.component.uuid]
return next
}
default: {
Expand Down

0 comments on commit 3d41a67

Please sign in to comment.