Skip to content

Commit

Permalink
Merge pull request #33 from amoscardino/dev
Browse files Browse the repository at this point in the history
v1.3
  • Loading branch information
amoscardino authored Jul 17, 2023
2 parents 4adac43 + 747eac0 commit b0e0720
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"scripts": {
"serve": "ionic serve",
"local-cors-proxy": "lcp --proxyUrl 'http://linkding.me' --proxyPartial ''",
"local-cors-proxy": "lcp --proxyUrl 'https://linkding.moscardino.synology.me' --proxyPartial ''",
"build": "ionic build",
"ios:build": "ionic cap sync ios",
"ios:run": "ionic cap run ios -l",
Expand Down
18 changes: 18 additions & 0 deletions src/api/linkdigApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ const updateBookmarkRead = async (id: number): Promise<void> => {
});
};

const deleteBookmark = async (id: number): Promise<void> => {
const settings = await getSettings();

if (settings.instanceUrl === undefined || settings.token === undefined)
throw new Error('Missing Linkdig settings. Please provide them from the Settings page.');

const url = new URL(`api/bookmarks/${id}/`, settings.instanceUrl);

await CapacitorHttp.delete({
url: url.toString(),
headers: {
'Authorization': `Token ${settings.token}`,
'Content-Type': 'application/json'
}
});
};

const getTags = async (): Promise<string[]> => {
const settings = await getSettings();

Expand Down Expand Up @@ -184,5 +201,6 @@ export {
createBookmark,
updateBookmark,
updateBookmarkRead,
deleteBookmark,
getTags
};
11 changes: 9 additions & 2 deletions src/hooks/useBookmark.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { SetStateAction, useEffect, useState } from "react";
import Bookmark from "api/types/bookmark";
import { getBookmark, updateBookmark } from "api/linkdigApi";
import { deleteBookmark, getBookmark, updateBookmark } from "api/linkdigApi";
import { useQueryClient } from "@tanstack/react-query";

interface UseBookmarkResult {
bookmark: Bookmark;
setBookmark: (value: SetStateAction<Bookmark>) => void;
saveBookmark: () => Promise<void>;
removeBookmark: () => Promise<void>;
}

const useBookmark = (id: number): UseBookmarkResult => {
Expand All @@ -27,10 +28,16 @@ const useBookmark = (id: number): UseBookmarkResult => {
await queryClient.invalidateQueries();
};

const removeBookmark = async (): Promise<void> => {
await deleteBookmark(bookmark.id);
await queryClient.invalidateQueries();
};

return {
bookmark,
setBookmark,
saveBookmark
saveBookmark,
removeBookmark
};
};

Expand Down
21 changes: 13 additions & 8 deletions src/hooks/useGroupedTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ interface UseGroupedTagsResult extends QueryResult {
const useGroupedTags = (): UseGroupedTagsResult => {
const { tags, isSuccess, isLoading, isError } = useTags();

const groups = tags.reduce((groups: TagGroup[], tag: string) => {
const groupName = /[A-Z]/i.test(tag[0]) ? tag[0].toUpperCase() : '#';
const existingGroups = groups.filter(g => g.name === groupName);
const groups = tags
.reduce((groups: TagGroup[], tag: string) => {
const groupName = /[A-Z]/.test(tag[0].toUpperCase()) ? tag[0].toUpperCase() : '#';
const existingGroups = groups.filter(g => g.name === groupName);

if (!existingGroups.length)
return [...groups, { name: groupName, tags: [tag] }];
if (!existingGroups.length)
return [...groups, { name: groupName, tags: [tag] }];

existingGroups[0].tags.push(tag);
return [...groups];
}, [] as TagGroup[]);
existingGroups[0].tags.push(tag);
return [...groups];
}, [] as TagGroup[])
.sort((a, b) => a.name === "#" ? -1 : a.name.localeCompare(b.name));

for (let group of groups)
group.tags.sort((a, b) => a.localeCompare(b));

return {
groups,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/AddPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ const AddPage = ({ dismiss }: AddPageProps) => {
secondaryButton={closeButton}
>
<IonList className="ion-padding-vertical" style={{ background: '' }}>
<IonItem className="ion-margin-bottom">
<IonItem className="ion-margin-bottom" lines={isExistingBookmark ? "none" : undefined}>
<IonInput
label="URL"
labelPlacement="stacked"
type="url"
value={bookmark.url}
onIonInput={e => setBookmark(prev => ({ ...prev, url: e.target.value || '' } as Bookmark))}
errorText={isExistingBookmark ? "It looks like this URL matches an existing bookmark. If you save this URL again, it will update the older bookmark." : undefined}
helperText={isExistingBookmark ? "It looks like this URL matches an existing bookmark. If you save this URL again, it will update the older bookmark." : undefined}
/>
</IonItem>

Expand Down
43 changes: 39 additions & 4 deletions src/pages/EditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
IonList,
IonTextarea,
IonToggle,
useIonAlert,
useIonModal
} from "@ionic/react";
import { checkmarkOutline, closeOutline, pricetagOutline } from "ionicons/icons";
import { checkmarkOutline, closeOutline, pricetagOutline, trashOutline } from "ionicons/icons";
import StandardPage from "components/StandardPage";
import { tapMedium } from "utils/haptics";
import { tapHeavy, tapMedium } from "utils/haptics";
import useBookmark from "hooks/useBookmark";
import Bookmark from "api/types/bookmark";
import Footer from "components/Footer";
Expand All @@ -24,8 +25,9 @@ interface EditPageProps {
}

const EditPage = ({ id, dismiss }: EditPageProps) => {
const { bookmark, setBookmark, saveBookmark } = useBookmark(id);
const { bookmark, setBookmark, saveBookmark, removeBookmark } = useBookmark(id);
const pageRef = useRef<HTMLElement | null>(null);
const [showDeleteAlert] = useIonAlert();

const handleTagDismiss = (newTags: string[] | null) => {
if (newTags !== null)
Expand All @@ -50,6 +52,25 @@ const EditPage = ({ id, dismiss }: EditPageProps) => {
dismiss(true);
};

const handleDeleteButton = async () => {
await showDeleteAlert({
header: "Delete Bookmark?",
message: "Are you sure you want to delete this bookmark? This is a permanent action.",
buttons: [{
text: 'Cancel',
role: 'cancel'
}, {
text: "Delete",
role: "destructive",
handler: async () => {
await removeBookmark();
await tapHeavy();
dismiss(true);
}
}]
})
};

const closeButton = (
<IonButton onClick={handleCloseButton} title="Cancel">
<IonIcon slot="icon-only" icon={closeOutline} />
Expand Down Expand Up @@ -124,7 +145,7 @@ const EditPage = ({ id, dismiss }: EditPageProps) => {
/>
</IonItem>

<IonItem lines="none" className="ion-margin-bottom">
<IonItem className="ion-margin-bottom">
<IonLabel>
Unread?
</IonLabel>
Expand All @@ -136,6 +157,20 @@ const EditPage = ({ id, dismiss }: EditPageProps) => {
/>
</IonItem>

<IonItem className="ion-margin-top" lines="none">
<IonLabel className="ion-margin-top ion-padding-top ion-text-center">
<IonButton
shape="round"
color="danger"
size="small"
onClick={handleDeleteButton}
>
<IonIcon slot="start" icon={trashOutline} />
Delete
</IonButton>
</IonLabel>
</IonItem>

<Footer />
</IonList>
</StandardPage>
Expand Down

0 comments on commit b0e0720

Please sign in to comment.