Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
WIP: saving trans
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Mar 11, 2016
1 parent 519221e commit 66db7a5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 46 deletions.
10 changes: 3 additions & 7 deletions frontend/src/main/web/src/actions/glossary.js
Expand Up @@ -137,9 +137,7 @@ export const createGlossaryTerm = (dispatch, term) => {
types: [
{
type: GLOSSARY_CREATE_REQUEST,
payload: (action, state) => {
return term
}
payload: (action, state) => ({ entry: term })
},
{
type: GLOSSARY_CREATE_SUCCESS,
Expand Down Expand Up @@ -176,9 +174,7 @@ export const updateGlossaryTerm = (dispatch, term) => {
types: [
{
type: GLOSSARY_UPDATE_REQUEST,
payload: (action, state) => {
return term
}
payload: (action, state) => ({ entry: term })
},
{
type: GLOSSARY_UPDATE_SUCCESS,
Expand Down Expand Up @@ -408,7 +404,7 @@ export const glossarySelectTerm = (termId) => {
if (selectedTerm && selectedTerm.id !== termId) {
const status = selectedTerm.status
if (status && (status.isSrcModified || status.isTransModified)) {
dispatch(glossaryUpdateTerm(cloneDeep(selectedTerm)))
dispatch(glossaryUpdateTerm(selectedTerm))
}
dispatch(updateSelectedTerm(termId))
}
Expand Down
35 changes: 17 additions & 18 deletions frontend/src/main/web/src/containers/Glossary/Entry.js
Expand Up @@ -40,16 +40,14 @@ class Entry extends Component {
transSelected,
selected,
permission,
savingTerm
isSaving
} = this.props

const isSaving = !isUndefined(savingTerm)
const entryToUse = isSaving ? savingTerm : entry
const transContent = entry && entry.transTerm
? entry.transTerm.content
: ''

if (!entryToUse) {
if (!entry) {
return (
<TableRow>
<TableCell>
Expand All @@ -64,9 +62,10 @@ class Entry extends Component {
}

const isTermModified = transSelected
? (entryToUse.status && entryToUse.status.isTransModified)
: (entryToUse.status && entryToUse.status.isSrcModified)
const displayUpdateButton = permission.canUpdateEntry && isTermModified
? (entry.status && entry.status.isTransModified)
: (entry.status && entry.status.isSrcModified)
const displayUpdateButton = permission.canUpdateEntry &&
isTermModified && selected
const editable = permission.canUpdateEntry && !isSaving

let updateButton
Expand All @@ -79,7 +78,7 @@ class Entry extends Component {
} else if (displayUpdateButton) {
updateButton = (
<ButtonRound theme={{base: {m: 'Mstart(rh)'}}} type='primary'
onClick={() => handleUpdateTerm(entryToUse)}>
onClick={() => handleUpdateTerm(entry)}>
Update
</ButtonRound>)
}
Expand All @@ -88,12 +87,12 @@ class Entry extends Component {
<TableRow highlight
className='editable'
selected={selected}
onClick={() => handleSelectTerm(entryToUse.id)}>
onClick={() => handleSelectTerm(entry.id)}>
<TableCell size='2' tight>
<EditableText
editable={false}
editing={selected}>
{entryToUse.srcTerm.content}
{entry.srcTerm.content}
</EditableText>
</TableCell>
<TableCell size={transSelected ? '2' : '1'} tight={transSelected}>
Expand All @@ -108,7 +107,7 @@ class Entry extends Component {
emptyReadOnlyText='No translation'>
{transContent}
</EditableText>)
: <div className='LineClamp(1,24px) Px(rq)'>{entryToUse.termsCount}</div>
: <div className='LineClamp(1,24px) Px(rq)'>{entry.termsCount}</div>
}
</TableCell>
<TableCell hideSmall>
Expand All @@ -118,7 +117,7 @@ class Entry extends Component {
onChange={(e) => handleTermFieldUpdate('pos', e)}
placeholder='Add part of speech…'
emptyReadOnlyText='No part of speech'>
{entryToUse.pos}
{entry.pos}
</EditableText>
</TableCell>
{!transSelected ? (
Expand All @@ -129,7 +128,7 @@ class Entry extends Component {
onChange={(e) => handleTermFieldUpdate('description', e)}
placeholder='Add a description…'
emptyReadOnlyText='No description'>
{entryToUse.description}
{entry.description}
</EditableText>
</TableCell>
) : ''
Expand All @@ -138,21 +137,21 @@ class Entry extends Component {
<ButtonLink theme={{base: { m: 'Mend(rh)' }}}>
<Icon name='info'/>
</ButtonLink>
<EntryModal entry={entryToUse}/>
<EntryModal entry={entry}/>

{updateButton}
<div className='Op(0) row--selected_Op(1) editable:h_Op(1) Trs(eo)'>
{displayUpdateButton && !isSaving ? (
<ButtonLink theme={{base: {m: 'Mstart(rh)'}}}
onClick={() => handleResetTerm(entryToUse.id)}>
onClick={() => handleResetTerm(entry.id)}>
Cancel
</ButtonLink>
) : ''
}

{!transSelected && permission.canDeleteEntry && !isSaving ? (
<DeleteEntryModal id={entryToUse.id}
entry={entryToUse}
<DeleteEntryModal id={entry.id}
entry={entry}
className='Mstart(rh)'
onDelete={handleDeleteTerm}/>
) : ''
Expand All @@ -169,7 +168,7 @@ Entry.propType = {
selected: PropTypes.bool,
index: PropTypes.number,
permission: PropTypes.object,
savingEntry: PropTypes.object,
isSaving: PropTypes.bool,
transSelected: PropTypes.bool,
termsLoading: PropTypes.bool,
handleSelectTerm: PropTypes.func,
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/main/web/src/containers/Glossary/index.js
@@ -1,7 +1,7 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import Helmet from 'react-helmet'
import { debounce, cloneDeep } from 'lodash'
import { debounce, cloneDeep, isUndefined } from 'lodash'
import { replaceRouteQuery } from '../../utils/RoutingHelpers'
import ReactList from 'react-list'
import {
Expand Down Expand Up @@ -60,16 +60,17 @@ class Glossary extends Component {

const entryId = termIds[index]
const selected = entryId === selectedTerm.id
const entry = selected ? selectedTerm : entryId
? cloneDeep(terms[entryId]) : false
const savingEntry = saving[entryId]
const isSaving = !isUndefined(saving[entryId])
const entry = isSaving ? saving[entryId]
: (selected ? selectedTerm : entryId
? cloneDeep(terms[entryId]) : null)

return (
<Entry key={key}
entry={entry}
index={index}
selected={selected}
savingEntry={savingEntry}
isSaving={isSaving}
permission={permission}
transSelected={!!selectedTransLocale}
termsLoading={termsLoading}
Expand Down
27 changes: 11 additions & 16 deletions frontend/src/main/web/src/reducers/glossary.js
Expand Up @@ -204,16 +204,8 @@ const glossary = handleActions({
}),
[GLOSSARY_UPDATE_REQUEST]: (state, action) => {
let saving = state.saving
const selectedTerm = state.selectedTerm
const entryId = action.payload.id

if (selectedTerm && selectedTerm.id === entryId) {
saving[entryId] = cloneDeep(selectedTerm)
console.info('1alex..', saving)
} else if (state.terms[entryId]) {
console.info('2alex..')
saving[entryId] = cloneDeep(state.terms[entryId])
}
const entryId = action.payload.entry.id
saving[entryId] = cloneDeep(action.payload.entry)
return {
...state,
saving: saving
Expand All @@ -222,26 +214,30 @@ const glossary = handleActions({
[GLOSSARY_UPDATE_SUCCESS]: (state, action) => {
let saving = state.saving
let selectedTerm = state.selectedTerm
let terms = state.terms
forEach(action.payload.glossaryEntries, (entry) => {
delete saving[entry.id]
let cacheEntry = state.terms[entry.id]
let cacheEntry = terms[entry.id]
if (cacheEntry) {
GlossaryHelper.mergeEntry(entry, cacheEntry)
}
delete saving[entry.id]
})

if (selectedTerm) {
GlossaryHelper.mergeEntry(state.terms[selectedTerm.id], selectedTerm)
GlossaryHelper.mergeEntry(terms[selectedTerm.id], selectedTerm)
selectedTerm.status.isSrcModified = false
selectedTerm.status.isTransModified = false
}

return {
...state,
saving: saving
saving: saving,
terms: terms,
selectedTerm: selectedTerm
}
},
[GLOSSARY_UPDATE_FAILURE]: (state, action) => {
let saving = state.saving
//TODO: remove from saving delete saving[action.payload.id]
return {
...state,
saving: saving,
Expand Down Expand Up @@ -299,7 +295,6 @@ const glossary = handleActions({
let entries = {}
forEach(action.payload.entities.glossaryTerms, (entry) => {
entries[entry.id] = GlossaryHelper.generateEntry(entry, state.locale)
console.info(entries)
})
const terms = isEmpty(state.terms)
? entries
Expand Down

0 comments on commit 66db7a5

Please sign in to comment.