You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
7.1 KiB
TypeScript
223 lines
7.1 KiB
TypeScript
7 years ago
|
import * as React from 'react';
|
||
7 years ago
|
import { Route, Switch } from 'react-router';
|
||
7 years ago
|
import RaisedButton from 'material-ui/RaisedButton';
|
||
|
import IconEdit from 'material-ui/svg-icons/editor/mode-edit';
|
||
7 years ago
|
|
||
7 years ago
|
import * as EteSync from '../api/EteSync';
|
||
7 years ago
|
|
||
7 years ago
|
import { createSelector } from 'reselect';
|
||
|
|
||
7 years ago
|
import { ContactType, EventType } from '../pim-types';
|
||
7 years ago
|
|
||
7 years ago
|
import Container from '../widgets/Container';
|
||
7 years ago
|
|
||
7 years ago
|
import ContactEdit from '../components/ContactEdit';
|
||
|
import Contact from '../components/Contact';
|
||
|
import EventEdit from '../components/EventEdit';
|
||
|
import Event from '../components/Event';
|
||
7 years ago
|
import PimMain from './PimMain';
|
||
|
|
||
7 years ago
|
import { routeResolver } from '../App';
|
||
7 years ago
|
|
||
7 years ago
|
import { store, CredentialsData } from '../store';
|
||
7 years ago
|
|
||
7 years ago
|
import { SyncInfo } from '../SyncGate';
|
||
7 years ago
|
|
||
7 years ago
|
import { createJournalEntry } from '../etesync-helpers';
|
||
7 years ago
|
|
||
7 years ago
|
import { syncEntriesToItemMap, syncEntriesToCalendarItemMap } from '../journal-processors';
|
||
7 years ago
|
|
||
7 years ago
|
function objValues(obj: any) {
|
||
|
return Object.keys(obj).map((x) => obj[x]);
|
||
|
}
|
||
|
|
||
7 years ago
|
const itemsSelector = createSelector(
|
||
|
(props: {syncInfo: SyncInfo}) => props.syncInfo,
|
||
|
(syncInfo) => {
|
||
|
let collectionsAddressBook: Array<EteSync.CollectionInfo> = [];
|
||
|
let collectionsCalendar: Array<EteSync.CollectionInfo> = [];
|
||
|
let addressBookItems: {[key: string]: ContactType} = {};
|
||
|
let calendarItems: {[key: string]: EventType} = {};
|
||
|
syncInfo.forEach(
|
||
|
(syncJournal) => {
|
||
|
const syncEntries = syncJournal.entries;
|
||
|
const journal = syncJournal.journal;
|
||
|
|
||
|
// FIXME: Skip shared journals for now
|
||
|
if (journal.key) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const collectionInfo = syncJournal.collection;
|
||
|
|
||
|
if (collectionInfo.type === 'ADDRESS_BOOK') {
|
||
7 years ago
|
addressBookItems = syncEntriesToItemMap(collectionInfo, syncEntries, addressBookItems);
|
||
7 years ago
|
collectionsAddressBook.push(collectionInfo);
|
||
|
} else if (collectionInfo.type === 'CALENDAR') {
|
||
7 years ago
|
calendarItems = syncEntriesToCalendarItemMap(collectionInfo, syncEntries, calendarItems);
|
||
7 years ago
|
collectionsCalendar.push(collectionInfo);
|
||
|
}
|
||
|
}
|
||
|
);
|
||
|
|
||
|
return { collectionsAddressBook, collectionsCalendar, addressBookItems, calendarItems };
|
||
|
},
|
||
|
);
|
||
7 years ago
|
class Pim extends React.PureComponent {
|
||
7 years ago
|
props: {
|
||
|
etesync: CredentialsData;
|
||
7 years ago
|
syncInfo: SyncInfo;
|
||
7 years ago
|
history: any;
|
||
7 years ago
|
};
|
||
|
|
||
|
constructor(props: any) {
|
||
|
super(props);
|
||
7 years ago
|
this.onEventSave = this.onEventSave.bind(this);
|
||
7 years ago
|
this.onContactSave = this.onContactSave.bind(this);
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
onEventSave(event: EventType, journalUid: string, originalEvent?: EventType) {
|
||
7 years ago
|
const syncJournal = this.props.syncInfo.get(journalUid);
|
||
7 years ago
|
|
||
7 years ago
|
if (syncJournal === undefined) {
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
const journal = syncJournal.journal;
|
||
7 years ago
|
|
||
7 years ago
|
let action = (originalEvent === undefined) ? EteSync.SyncEntryAction.Add : EteSync.SyncEntryAction.Change;
|
||
|
let saveEvent = store.dispatch(
|
||
7 years ago
|
createJournalEntry(this.props.etesync, journal, syncJournal.journalEntries, action, event.toIcal()));
|
||
7 years ago
|
(saveEvent as any).then(() => {
|
||
|
this.props.history.goBack();
|
||
|
});
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
onContactSave(contact: ContactType, journalUid: string, originalContact?: ContactType) {
|
||
7 years ago
|
const syncJournal = this.props.syncInfo.get(journalUid);
|
||
7 years ago
|
|
||
7 years ago
|
if (syncJournal === undefined) {
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
const journal = syncJournal.journal;
|
||
7 years ago
|
|
||
|
let action = (originalContact === undefined) ? EteSync.SyncEntryAction.Add : EteSync.SyncEntryAction.Change;
|
||
|
let saveContact = store.dispatch(
|
||
7 years ago
|
createJournalEntry(this.props.etesync, journal, syncJournal.journalEntries, action, contact.toIcal()));
|
||
7 years ago
|
(saveContact as any).then(() => {
|
||
|
this.props.history.goBack();
|
||
|
});
|
||
|
}
|
||
|
|
||
7 years ago
|
render() {
|
||
7 years ago
|
const { collectionsAddressBook, collectionsCalendar, addressBookItems, calendarItems } = itemsSelector(this.props);
|
||
7 years ago
|
|
||
|
return (
|
||
7 years ago
|
<Switch>
|
||
|
<Route
|
||
|
path={routeResolver.getRoute('pim')}
|
||
|
exact={true}
|
||
|
render={({history}) => (
|
||
|
<PimMain
|
||
7 years ago
|
contacts={objValues(addressBookItems)}
|
||
|
events={objValues(calendarItems)}
|
||
7 years ago
|
history={history}
|
||
|
/>
|
||
|
)}
|
||
|
/>
|
||
|
<Route
|
||
7 years ago
|
path={routeResolver.getRoute('pim.contacts.new')}
|
||
|
exact={true}
|
||
|
render={({match}) => (
|
||
|
<Container style={{maxWidth: 400}}>
|
||
|
<ContactEdit collections={collectionsAddressBook} onSave={this.onContactSave} />
|
||
|
</Container>
|
||
|
)}
|
||
|
/>
|
||
|
<Route
|
||
|
path={routeResolver.getRoute('pim.contacts._id.edit')}
|
||
7 years ago
|
exact={true}
|
||
|
render={({match}) => (
|
||
7 years ago
|
<Container style={{maxWidth: 400}}>
|
||
|
<ContactEdit
|
||
7 years ago
|
initialCollection={(addressBookItems[match.params.contactUid] as any).journalUid}
|
||
7 years ago
|
contact={addressBookItems[match.params.contactUid]}
|
||
|
collections={collectionsAddressBook}
|
||
|
onSave={this.onContactSave}
|
||
|
/>
|
||
|
</Container>
|
||
|
)}
|
||
|
/>
|
||
|
<Route
|
||
|
path={routeResolver.getRoute('pim.contacts._id')}
|
||
|
exact={true}
|
||
|
render={({match, history}) => (
|
||
7 years ago
|
<Container>
|
||
7 years ago
|
<div style={{textAlign: 'right'}}>
|
||
|
<RaisedButton
|
||
|
label="Edit"
|
||
|
secondary={true}
|
||
|
icon={<IconEdit />}
|
||
|
onClick={() =>
|
||
|
history.push(routeResolver.getRoute(
|
||
|
'pim.contacts._id.edit',
|
||
|
{contactUid: match.params.contactUid}))
|
||
|
}
|
||
|
/>
|
||
|
</div>
|
||
7 years ago
|
<Contact contact={addressBookItems[match.params.contactUid]} />
|
||
7 years ago
|
</Container>
|
||
7 years ago
|
)}
|
||
|
/>
|
||
7 years ago
|
<Route
|
||
|
path={routeResolver.getRoute('pim.events.new')}
|
||
|
exact={true}
|
||
|
render={({match}) => (
|
||
|
<Container style={{maxWidth: 400}}>
|
||
|
<EventEdit collections={collectionsCalendar} onSave={this.onEventSave} />
|
||
|
</Container>
|
||
|
)}
|
||
|
/>
|
||
7 years ago
|
<Route
|
||
7 years ago
|
path={routeResolver.getRoute('pim.events._id.edit')}
|
||
7 years ago
|
exact={true}
|
||
|
render={({match}) => (
|
||
7 years ago
|
<Container style={{maxWidth: 400}}>
|
||
|
<EventEdit
|
||
7 years ago
|
initialCollection={(calendarItems[match.params.eventUid] as any).journalUid}
|
||
7 years ago
|
event={calendarItems[match.params.eventUid]}
|
||
|
collections={collectionsCalendar}
|
||
|
onSave={this.onEventSave}
|
||
|
/>
|
||
|
</Container>
|
||
|
)}
|
||
|
/>
|
||
|
<Route
|
||
|
path={routeResolver.getRoute('pim.events._id')}
|
||
|
exact={true}
|
||
|
render={({match, history}) => (
|
||
7 years ago
|
<Container>
|
||
7 years ago
|
<div style={{textAlign: 'right'}}>
|
||
|
<RaisedButton
|
||
|
label="Edit"
|
||
|
secondary={true}
|
||
|
icon={<IconEdit />}
|
||
|
onClick={() =>
|
||
|
history.push(routeResolver.getRoute(
|
||
|
'pim.events._id.edit',
|
||
|
{eventUid: match.params.eventUid}))
|
||
|
}
|
||
|
/>
|
||
|
</div>
|
||
7 years ago
|
<Event event={calendarItems[match.params.eventUid]} />
|
||
7 years ago
|
</Container>
|
||
7 years ago
|
)}
|
||
|
/>
|
||
7 years ago
|
</Switch>
|
||
7 years ago
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
export default Pim;
|