Skip to content

Commit

Permalink
feat: Address book (#38)
Browse files Browse the repository at this point in the history
* Add address book view

* Hook up search

* Add migration mutation for contacts

* Fix package.json

* Add contacts store name
  • Loading branch information
dgca authored Nov 28, 2023
1 parent 26f3cdd commit 086973e
Show file tree
Hide file tree
Showing 17 changed files with 557 additions and 293 deletions.
96 changes: 0 additions & 96 deletions main/api/address-book/v1/AbstractStorage.ts

This file was deleted.

101 changes: 0 additions & 101 deletions main/api/address-book/v1/AddressBookStorage.ts

This file was deleted.

16 changes: 0 additions & 16 deletions main/api/address-book/v1/Contact.ts

This file was deleted.

5 changes: 0 additions & 5 deletions main/api/address-book/v1/Entity.ts

This file was deleted.

16 changes: 0 additions & 16 deletions main/api/address-book/v1/IStorage.ts

This file was deleted.

6 changes: 0 additions & 6 deletions main/api/address-book/v1/SortType.ts

This file was deleted.

18 changes: 0 additions & 18 deletions main/api/address-book/v1/index.ts

This file was deleted.

96 changes: 96 additions & 0 deletions main/api/contacts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Store, { Schema } from "electron-store";
import { v4 as uuidv4 } from "uuid";
import { z } from "zod";

import { getNodeAppBetaContacts } from "./utils/getNodeAppBetaContacts";
import { t } from "../trpc";

const contactDefinition = z.object({
id: z.string(),
name: z.string(),
address: z.string(),
note: z.string().optional(),
});

type Contact = z.infer<typeof contactDefinition>;

const schema: Schema<{
contacts: Contact[];
}> = {
contacts: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
name: { type: "string" },
address: { type: "string" },
note: { type: "string" },
},
required: ["id", "name", "address"],
additionalProperties: false,
},
},
};

const store = new Store({ schema, name: "contacts" });

export const contactsRouter = t.router({
getContacts: t.procedure.query(async () => {
return store.get("contacts", []);
}),
migrateNodeAppBetaContacts: t.procedure.mutation(async () => {
const betaContacts = await getNodeAppBetaContacts();
const contacts = await store.get("contacts", []);
const addressLookup = new Set(contacts.map((contact) => contact.address));

for (const contact of betaContacts) {
if (!addressLookup.has(contact.address)) {
contacts.push({
id: uuidv4(),
name: contact.name,
address: contact.address,
});
}
}

store.set("contacts", contacts);
return contacts;
}),
addContact: t.procedure
.input(
z.object({
name: z.string(),
address: z.string(),
}),
)
.mutation(async (opts) => {
const contacts = store.get("contacts", []);
contacts.push({
id: uuidv4(),
name: opts.input.name,
address: opts.input.address,
});
store.set("contacts", contacts);
return contacts;
}),
deleteContact: t.procedure
.input(
z.object({
id: z.string(),
}),
)
.mutation(async (opts) => {
const contacts = store.get("contacts", []);
const index = contacts.findIndex(
(contact) => contact.id === opts.input.id,
);

if (index > -1) {
contacts.splice(index, 1);
store.set("contacts", contacts);
}

return contacts;
}),
});
Loading

0 comments on commit 086973e

Please sign in to comment.