Skip to content

Commit

Permalink
Merge branch 'create-subtask'
Browse files Browse the repository at this point in the history
  • Loading branch information
MangoCubes committed Jul 23, 2023
2 parents 7e8684f + d1d380d commit b018e6c
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 55 deletions.
9 changes: 7 additions & 2 deletions src/Calendars/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ export default function CalendarsMain() {

async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise<void> {
const collection = collections!.find((x) => x.uid === collectionUid)!;
await itemSave(etebase, collection, items!, item, collectionUid, originalItem);
await itemSave(etebase, collection, items!, collectionUid,
[{
original: originalItem,
new: item,
}]
);
}

async function onItemDelete(item: PimType, collectionUid: string) {
const collection = collections!.find((x) => x.uid === collectionUid)!;
await itemDelete(etebase, collection, items!, item, collectionUid);
await itemDelete(etebase, collection, items!, [item], collectionUid);

history.push(routeResolver.getRoute("pim.events"));
}
Expand Down
9 changes: 7 additions & 2 deletions src/Contacts/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ export default function ContactsMain() {

async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise<void> {
const collection = collections!.find((x) => x.uid === collectionUid)!;
await itemSave(etebase, collection, items!, item, collectionUid, originalItem);
await itemSave(etebase, collection, items!, collectionUid,
[{
original: originalItem,
new: item,
}]
);
}

async function onItemDelete(item: PimType, collectionUid: string) {
const collection = collections!.find((x) => x.uid === collectionUid)!;
await itemDelete(etebase, collection, items!, item, collectionUid);
await itemDelete(etebase, collection, items!, [item], collectionUid);

history.push(routeResolver.getRoute("pim.contacts"));
}
Expand Down
71 changes: 38 additions & 33 deletions src/Pim/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import memoize from "memoizee";

import * as Etebase from "etebase";

import { PimType } from "../pim-types";
import { PimChanges, PimType } from "../pim-types";
import { getCollectionManager } from "../etebase-helpers";
import { asyncDispatch, store } from "../store";
import { itemBatch, appendError } from "../store/actions";
Expand Down Expand Up @@ -85,47 +85,52 @@ export function getDecryptItemsFunction<T extends PimType>(_colType: string, par
);
}

export async function itemSave(etebase: Etebase.Account, collection: Etebase.Collection, items: Map<string, Map<string, Etebase.Item>>, item: PimType, collectionUid: string, originalItem?: PimType): Promise<void> {
const itemUid = originalItem?.itemUid;
export async function itemSave(etebase: Etebase.Account, collection: Etebase.Collection, items: Map<string, Map<string, Etebase.Item>>, collectionUid: string, changes: PimChanges[]): Promise<void> {
const colMgr = getCollectionManager(etebase);
const itemMgr = colMgr.getItemManager(collection);

const mtime = (new Date()).getTime();
const content = item.toIcal();

let eteItem;
if (itemUid) {
// Existing item
eteItem = items!.get(collectionUid)?.get(itemUid)!;
await eteItem.setContent(content);
const meta = eteItem.getMeta();
meta.mtime = mtime;
eteItem.setMeta(meta);
} else {
// New
const meta: Etebase.ItemMetadata = {
mtime,
name: item.uid,
};
eteItem = await itemMgr.create(meta, content);
const itemList = [];
for (const item of changes) {
const itemUid = item.original?.itemUid;
const content = item.new.toIcal();
let eteItem;
if (itemUid) {
// Existing item
eteItem = items!.get(collectionUid)?.get(itemUid)!;
await eteItem.setContent(content);
const meta = eteItem.getMeta();
meta.mtime = mtime;
eteItem.setMeta(meta);
} else {
// New
const meta: Etebase.ItemMetadata = {
mtime,
name: item.new.uid,
};
eteItem = await itemMgr.create(meta, content);
}
itemList.push(eteItem);
}

await asyncDispatch(itemBatch(collection, itemMgr, [eteItem]));
await asyncDispatch(itemBatch(collection, itemMgr, itemList));
}

export async function itemDelete(etebase: Etebase.Account, collection: Etebase.Collection, items: Map<string, Map<string, Etebase.Item>>, item: PimType, collectionUid: string) {
const itemUid = item.itemUid!;
export async function itemDelete(etebase: Etebase.Account, collection: Etebase.Collection, items: Map<string, Map<string, Etebase.Item>>, itemsToDelete: PimType[], collectionUid: string) {
const colMgr = getCollectionManager(etebase);
const itemMgr = colMgr.getItemManager(collection);
const itemList = [];
for (const item of itemsToDelete) {
const itemUid = item.itemUid!;
const eteItem = items!.get(collectionUid)?.get(itemUid)!;
const mtime = (new Date()).getTime();
const meta = eteItem.getMeta();
meta.mtime = mtime;
eteItem.setMeta(meta);
eteItem.delete(true);
itemList.push(eteItem);
}


const eteItem = items!.get(collectionUid)?.get(itemUid)!;
const mtime = (new Date()).getTime();
const meta = eteItem.getMeta();
meta.mtime = mtime;
eteItem.setMeta(meta);
eteItem.delete(true);

await asyncDispatch(itemBatch(collection, itemMgr, [eteItem]));
await asyncDispatch(itemBatch(collection, itemMgr, itemList));
}

interface PimFabPropsType {
Expand Down
39 changes: 30 additions & 9 deletions src/Tasks/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Button, useTheme } from "@material-ui/core";
import IconEdit from "@material-ui/icons/Edit";
import IconChangeHistory from "@material-ui/icons/ChangeHistory";

import { TaskType, PimType } from "../pim-types";
import { TaskType, PimType, PimChanges } from "../pim-types";
import { useCredentials } from "../credentials";
import { useItems, useCollections } from "../etebase-helpers";
import { routeResolver } from "../App";
Expand Down Expand Up @@ -57,15 +57,15 @@ export default function TasksMain() {
}

async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise<void> {
const collection = collections!.find((x) => x.uid === collectionUid)!;
await itemSave(etebase, collection, items!, item, collectionUid, originalItem);
await onMultipleItemsSave([{
original: originalItem,
new: item,
}], collectionUid);
}

async function onItemDelete(item: PimType, collectionUid: string) {
async function onMultipleItemsSave(changes: PimChanges[], collectionUid: string): Promise<void> {
const collection = collections!.find((x) => x.uid === collectionUid)!;
await itemDelete(etebase, collection, items!, item, collectionUid);

history.push(routeResolver.getRoute("pim.tasks"));
await itemSave(etebase, collection, items!, collectionUid, changes);
}

function onCancel() {
Expand All @@ -79,6 +79,25 @@ export default function TasksMain() {
}
}

async function onItemDelete(item: PimType, collectionUid: string, redirect = true, recursive = false) {
const collection = collections!.find((x) => x.uid === collectionUid)!;
if (recursive) {
let index = 0;
const deleteTarget = [item];
while (index < deleteTarget.length) {
const current = deleteTarget[index++];
const children = flatEntries.filter((i) => i.relatedTo === current.uid);
deleteTarget.push(...children);
}
await itemDelete(etebase, collection, items!, deleteTarget, collectionUid);
} else {
await itemDelete(etebase, collection, items!, [item], collectionUid);
}
if (redirect) {
history.push(routeResolver.getRoute("pim.tasks"));
}
}

const styles = {
button: {
marginLeft: theme.spacing(1),
Expand Down Expand Up @@ -113,9 +132,10 @@ export default function TasksMain() {
exact
>
<TaskEdit
directChildren={[]}
entries={flatEntries}
collections={cachedCollections}
onSave={onItemSave}
onSave={onMultipleItemsSave}
onDelete={onItemDelete}
onCancel={onCancel}
history={history}
Expand Down Expand Up @@ -155,12 +175,13 @@ export default function TasksMain() {
exact
>
<TaskEdit
directChildren={flatEntries.filter((t) => t.relatedTo === item.uid)}
entries={flatEntries}
key={itemUid}
initialCollection={item.collectionUid}
item={item}
collections={cachedCollections}
onSave={onItemSave}
onSave={onMultipleItemsSave}
onDelete={onItemDelete}
onCancel={onCancel}
history={history}
Expand Down
Loading

0 comments on commit b018e6c

Please sign in to comment.