Skip to content

Commit fafb24f

Browse files
committed
feat(extension): add note delete logic
1 parent c8d8ade commit fafb24f

File tree

6 files changed

+94
-8
lines changed

6 files changed

+94
-8
lines changed

clients/extension/actions.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ export enum EXT_ACTIONS {
3030
USER_LOG_IN = 'USER_LOG_IN',
3131
ADD_NOTE_REQUEST = 'ADD_NOTE_REQUEST',
3232
ADD_NOTE_SUCCESS = 'ADD_NOTE_SUCCESS',
33-
ADD_NOTE_FAILURE = 'ADD_NOTE_FAILURE'
33+
ADD_NOTE_FAILURE = 'ADD_NOTE_FAILURE',
34+
DELETE_NOTE_REQUEST = 'DELETE_NOTE_REQUEST',
35+
DELETE_NOTE_SUCCESS = 'DELETE_NOTE_SUCCESS',
36+
DELETE_NOTE_FAILURE = 'DELETE_NOTE_FAILURE'
3437
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { extensionRequest } from '../../utilities/request'
2+
import { verifySession } from '../auth'
3+
4+
const gql = String.raw
5+
const query = gql`
6+
mutation DeleteNote($input: DeleteNoteInput!) {
7+
deleteNote(input: $input)
8+
}
9+
`
10+
11+
export async function removeNote(noteId: string) {
12+
// We need a token to save. This will create a session if need be
13+
// be requesting a new bearer if the current one is expired
14+
const token = await verifySession()
15+
16+
// Set up the variables we need for this request
17+
const data = { variables: { input: { id: noteId } }, query }
18+
19+
// Make the request and return it
20+
const response = await extensionRequest<{ deleteNote: boolean }>(data, token)
21+
22+
return response
23+
}

clients/extension/background/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isSystemPage } from '../utilities/is-system'
55
import { sendMessage } from '../utilities/send-message'
66
import { getSetting } from '../utilities/storage'
77
import { addNote } from './api/add-note'
8+
import { removeNote } from './api/remove-note'
89
import { upsertItem } from './api/upsert'
910
import { setAuth, logIn } from './auth'
1011
import { setDefaultIcon, setToolbarIcon } from './icon-updates'
@@ -50,6 +51,8 @@ async function messageHandler(message: ExtMessage, sender: chrome.runtime.Messag
5051
const tab = senderTab ?? activeTab[0]
5152
const tabId = tab?.id
5253

54+
console.log({ message })
55+
5356
switch (action) {
5457
case EXT_ACTIONS.BROWSER_ACTION: {
5558
// No tab was sent
@@ -83,6 +86,12 @@ async function messageHandler(message: ExtMessage, sender: chrome.runtime.Messag
8386
return true
8487
}
8588

89+
case EXT_ACTIONS.DELETE_NOTE_REQUEST: {
90+
const { noteId } = message
91+
if (noteId) await deleteNote(noteId)
92+
return true
93+
}
94+
8695
case EXT_ACTIONS.AUTH_CODE_RECEIVED: {
8796
try {
8897
const { auth } = message
@@ -171,6 +180,19 @@ async function saveNote(noteData: ExtNote, source?: string) {
171180
}
172181
}
173182

183+
async function deleteNote(noteId: string) {
184+
try {
185+
await removeNote(noteId)
186+
// send a message so the popup can display the preview
187+
sendMessage({ action: EXT_ACTIONS.DELETE_NOTE_SUCCESS, noteId })
188+
} catch (error) {
189+
// Things have gone awry. Let's send the error along
190+
const message = getErrorMessage(error)
191+
sendMessage({ action: EXT_ACTIONS.DELETE_NOTE_FAILURE, error: message })
192+
return false
193+
}
194+
}
195+
174196
export function openPocket() {
175197
void chrome.tabs.create({ url: POCKET_SAVES })
176198
}

clients/extension/components/notes-list/index.tsx

+38-6
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,39 @@ import ReactMarkdown from 'react-markdown'
66
// Types
77
import type { Note, NoteEdge } from '@common/types/pocket'
88

9-
export function NotesList({ notes }: { notes?: NoteEdge[] }) {
9+
export function NotesList({
10+
notes,
11+
handleNoteDelete
12+
}: {
13+
notes?: NoteEdge[]
14+
handleNoteDelete: (id: string) => void
15+
}) {
16+
const validNotes = notes?.filter((note) => !note?.node?.deleted) ?? []
1017
return (
1118
<div className={style.notes}>
1219
{Array.isArray(notes) && notes.length ? (
13-
notes.map((note) => <Note key={note?.cursor} note={note} />)
20+
validNotes.map((note) => (
21+
<Note key={note?.cursor} note={note} handleNoteDelete={handleNoteDelete} />
22+
))
1423
) : (
1524
<NoNotes />
1625
)}
1726
</div>
1827
)
1928
}
2029

21-
function Note({ note }: { note?: NoteEdge }) {
30+
function Note({
31+
note,
32+
handleNoteDelete
33+
}: {
34+
note?: NoteEdge
35+
handleNoteDelete: (id: string) => void
36+
}) {
2237
const [confirmDelete, setConfirmDelete] = useState<boolean>(false)
2338
const noteRef = useRef<HTMLDivElement>(null)
2439

2540
const node = note?.node
41+
const noteId = node?.id
2642
const title = node?.title
2743
const contentPreview = node?.contentPreview as string
2844

@@ -32,6 +48,11 @@ function Note({ note }: { note?: NoteEdge }) {
3248
}
3349
const handleCancelClick = () => setConfirmDelete(false)
3450

51+
const handleConfirmClick = () => {
52+
setConfirmDelete(false)
53+
if (noteId) handleNoteDelete(noteId)
54+
}
55+
3556
return contentPreview ? (
3657
<div className={`${style.container} ${confirmDelete && style.active}`} ref={noteRef}>
3758
<div className={style.note}>
@@ -47,15 +68,26 @@ function Note({ note }: { note?: NoteEdge }) {
4768
</button>
4869
) : null}
4970
</div>
50-
{confirmDelete ? <NoteDeleteConfirm handleCancelClick={handleCancelClick} /> : null}
71+
{confirmDelete ? (
72+
<NoteDeleteConfirm
73+
handleCancelClick={handleCancelClick}
74+
handleConfirmClick={handleConfirmClick}
75+
/>
76+
) : null}
5177
</div>
5278
) : null
5379
}
5480

55-
function NoteDeleteConfirm({ handleCancelClick }: { handleCancelClick: () => void }) {
81+
function NoteDeleteConfirm({
82+
handleCancelClick,
83+
handleConfirmClick
84+
}: {
85+
handleCancelClick: () => void
86+
handleConfirmClick: () => void
87+
}) {
5688
return (
5789
<div className={style.confirm}>
58-
<button>Delete this note</button>
90+
<button onClick={handleConfirmClick}>Delete this note</button>
5991
<button onClick={handleCancelClick}>Cancel</button>
6092
</div>
6193
)

clients/extension/components/notes/index.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ export function Notes({
4141
void sendMessage({ action: EXT_ACTIONS.ADD_NOTE_REQUEST, noteData: { docMarkdown } })
4242
}
4343

44+
const handleNoteDelete = (id: string) => {
45+
setNoteStatus('deleting note')
46+
void sendMessage({ action: EXT_ACTIONS.DELETE_NOTE_REQUEST, noteId: id })
47+
}
48+
4449
return (
4550
<div>
4651
<div className={style.container}>
47-
<NotesList notes={notes} />
52+
<NotesList notes={notes} handleNoteDelete={handleNoteDelete} />
4853
<NotesAdd noteStatus={noteStatus} textRef={textRef} setErrorText={setErrorText} />
4954
</div>
5055
<NotesFooter

clients/extension/types/message.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export interface ExtMessage {
1212
error?: string
1313
saveData?: ExtSave
1414
noteData?: ExtNote
15+
noteId?: string
1516
}

0 commit comments

Comments
 (0)