Remove unused components and models for improved maintainability
- Deleted redundant components (`AddActionButton`, `AlertBox`, `AlertStack`, `BackButton`, `CancelButton`, and `CollapsableArea`) and related files. - Removed unused models (`Book`, `BookSerie`, `BookTables`, `Character`, and `Chapter`) to reduce codebase clutter. - Updated project structure and references to reflect these removals.
This commit is contained in:
@@ -1,27 +1,19 @@
|
||||
'use client'
|
||||
import {faMapMarkerAlt, faPlus, faShare, faToggleOn, faTrash} from '@fortawesome/free-solid-svg-icons';
|
||||
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
||||
import {MapPin, Plus, Share2, ToggleRight, Trash2} from 'lucide-react';
|
||||
import React, {ChangeEvent, forwardRef, useContext, useEffect, useImperativeHandle, useState} from 'react';
|
||||
import {SessionContext} from "@/context/SessionContext";
|
||||
import {AlertContext} from "@/context/AlertContext";
|
||||
import {BookContext} from "@/context/BookContext";
|
||||
import System from '@/lib/models/System';
|
||||
import {SessionContext, SessionContextProps} from "@/context/SessionContext";
|
||||
import {AlertContext, AlertContextProps} from "@/context/AlertContext";
|
||||
import {BookContext, BookContextProps} from "@/context/BookContext";
|
||||
import {apiDelete, apiGet, apiPatch, apiPost} from '@/lib/api/client';
|
||||
import InputField from "@/components/form/InputField";
|
||||
import TextInput from '@/components/form/TextInput';
|
||||
import TexteAreaInput from "@/components/form/TexteAreaInput";
|
||||
import {useTranslations} from "next-intl";
|
||||
import TextAreaInput from "@/components/form/TextAreaInput";
|
||||
import {useTranslations} from '@/lib/i18n';
|
||||
import {LangContext, LangContextProps} from "@/context/LangContext";
|
||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from "@/context/SyncQueueContext";
|
||||
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
|
||||
import {SyncedBook} from "@/lib/models/SyncedBook";
|
||||
import {SeriesContext, SeriesContextProps} from "@/context/SeriesContext";
|
||||
import {SeriesSyncContext, SeriesSyncContextProps} from "@/context/SeriesSyncContext";
|
||||
import {SyncedSeries} from "@/lib/models/SyncedSeries";
|
||||
import ToggleSwitch from "@/components/form/ToggleSwitch";
|
||||
import {SeriesLocationElement, SeriesLocationItem, SeriesLocationSubElement} from "@/lib/models/Series";
|
||||
import {SeriesLocationElement, SeriesLocationItem, SeriesLocationSubElement} from "@/lib/types/series";
|
||||
import SeriesImportSelector from "@/components/form/SeriesImportSelector";
|
||||
import * as tauri from '@/lib/tauri';
|
||||
import IconButton from "@/components/ui/IconButton";
|
||||
|
||||
interface SubElement {
|
||||
id: string;
|
||||
@@ -57,16 +49,11 @@ interface LocationComponentProps {
|
||||
export function LocationComponent(props: LocationComponentProps, ref: React.Ref<{ handleSave: () => Promise<void> }>) {
|
||||
const {showToggle = true, entityType = 'book', entityId} = props;
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext<LangContextProps>(LangContext);
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const {addToQueue} = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const {session} = useContext(SessionContext);
|
||||
const {successMessage, errorMessage} = useContext(AlertContext);
|
||||
const {book, setBook} = useContext(BookContext);
|
||||
const {seriesId, localSeries} = useContext<SeriesContextProps>(SeriesContext);
|
||||
const {localSyncedSeries} = useContext<SeriesSyncContextProps>(SeriesSyncContext);
|
||||
|
||||
const {lang}: LangContextProps = useContext<LangContextProps>(LangContext);
|
||||
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
|
||||
const {successMessage, errorMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
|
||||
const {book, setBook}: BookContextProps = useContext<BookContextProps>(BookContext);
|
||||
|
||||
const currentEntityId: string = entityId || book?.bookId || '';
|
||||
const isSeriesMode: boolean = entityType === 'series';
|
||||
const token: string = session.accessToken;
|
||||
@@ -90,17 +77,17 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
getAllLocations().then();
|
||||
}
|
||||
}, [currentEntityId]);
|
||||
|
||||
|
||||
useEffect((): void => {
|
||||
if (bookSeriesId && !isSeriesMode) {
|
||||
getSeriesLocations().then();
|
||||
}
|
||||
}, [bookSeriesId]);
|
||||
|
||||
|
||||
async function getSeriesLocations(): Promise<void> {
|
||||
if (!bookSeriesId) return;
|
||||
try {
|
||||
const response: SeriesLocationItem[] = await System.authGetQueryToServer<SeriesLocationItem[]>(
|
||||
const response: SeriesLocationItem[] = await apiGet<SeriesLocationItem[]>(
|
||||
'series/location/list',
|
||||
token,
|
||||
lang,
|
||||
@@ -111,7 +98,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
console.error('Error loading series locations:', e.message);
|
||||
errorMessage(e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,31 +106,19 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
async function handleToggleTool(enabled: boolean): Promise<void> {
|
||||
if (isSeriesMode) return;
|
||||
try {
|
||||
let response: boolean;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.updateBookToolSetting(currentEntityId, 'locations', enabled);
|
||||
} else {
|
||||
response = await System.authPatchToServer<boolean>('book/tool-setting', {
|
||||
bookId: currentEntityId,
|
||||
toolName: 'locations',
|
||||
enabled: enabled
|
||||
}, token, lang);
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('update_book_tool_setting', {data: {
|
||||
bookId: currentEntityId,
|
||||
toolName: 'locations',
|
||||
enabled: enabled
|
||||
}});
|
||||
}
|
||||
}
|
||||
const response: boolean = await apiPatch<boolean>('book/tool-setting', {
|
||||
bookId: currentEntityId,
|
||||
toolName: 'locations',
|
||||
enabled: enabled
|
||||
}, token, lang);
|
||||
if (response && setBook && book) {
|
||||
setToolEnabled(enabled);
|
||||
setBook({
|
||||
...book, tools: {
|
||||
characters: book.tools?.characters ?? false,
|
||||
worlds: book.tools?.worlds ?? false,
|
||||
spells: book.tools?.spells ?? false,
|
||||
locations: enabled
|
||||
locations: enabled,
|
||||
spells: book.tools?.spells ?? false
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -157,17 +132,12 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
async function getAllLocations(): Promise<void> {
|
||||
try {
|
||||
if (isSeriesMode) {
|
||||
let response: SeriesLocationItem[];
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
response = await tauri.getSeriesLocationList(currentEntityId) as SeriesLocationItem[];
|
||||
} else {
|
||||
response = await System.authGetQueryToServer<SeriesLocationItem[]>(
|
||||
'series/location/list',
|
||||
token,
|
||||
lang,
|
||||
{seriesid: currentEntityId}
|
||||
);
|
||||
}
|
||||
const response: SeriesLocationItem[] = await apiGet<SeriesLocationItem[]>(
|
||||
'series/location/list',
|
||||
token,
|
||||
lang,
|
||||
{seriesid: currentEntityId}
|
||||
);
|
||||
if (response) {
|
||||
const mappedLocations: LocationProps[] = response.map((loc: SeriesLocationItem): LocationProps => ({
|
||||
id: loc.id,
|
||||
@@ -186,14 +156,12 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
setSections(mappedLocations);
|
||||
}
|
||||
} else {
|
||||
let response: LocationListResponse;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.getAllLocations(currentEntityId, true) as LocationListResponse;
|
||||
} else {
|
||||
response = await System.authGetQueryToServer<LocationListResponse>(`location/all`, token, lang, {
|
||||
bookid: currentEntityId,
|
||||
});
|
||||
}
|
||||
const response: LocationListResponse = await apiGet<LocationListResponse>(
|
||||
'location/all',
|
||||
token,
|
||||
lang,
|
||||
{bookid: currentEntityId}
|
||||
);
|
||||
if (response) {
|
||||
setSections(response.locations);
|
||||
setToolEnabled(response.enabled);
|
||||
@@ -202,8 +170,8 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
...book, tools: {
|
||||
characters: book.tools?.characters ?? false,
|
||||
worlds: book.tools?.worlds ?? false,
|
||||
spells: book.tools?.spells ?? false,
|
||||
locations: response.enabled
|
||||
locations: response.enabled,
|
||||
spells: book.tools?.spells ?? false
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -217,7 +185,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleAddSection(): Promise<void> {
|
||||
if (!newSectionName.trim()) {
|
||||
errorMessage(t('locationComponent.errorSectionNameEmpty'))
|
||||
@@ -226,47 +194,29 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
try {
|
||||
let sectionId: string;
|
||||
if (isSeriesMode) {
|
||||
const addData = {
|
||||
seriesId: currentEntityId,
|
||||
name: newSectionName,
|
||||
};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
sectionId = await tauri.addSeriesLocationSection(addData);
|
||||
} else {
|
||||
sectionId = await System.authPostToServer<string>(
|
||||
'series/location/section/add',
|
||||
addData,
|
||||
token,
|
||||
lang
|
||||
);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('add_series_location_section', {data: addData});
|
||||
}
|
||||
}
|
||||
sectionId = await apiPost<string>(
|
||||
'series/location/section/add',
|
||||
{
|
||||
seriesId: currentEntityId,
|
||||
name: newSectionName,
|
||||
},
|
||||
token,
|
||||
lang
|
||||
);
|
||||
if (!sectionId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddSection'));
|
||||
return;
|
||||
}
|
||||
} else if (isCurrentlyOffline() || book?.localBook) {
|
||||
sectionId = await tauri.addLocationSection(newSectionName, currentEntityId);
|
||||
} else {
|
||||
sectionId = await System.authPostToServer<string>(`location/section/add`, {
|
||||
sectionId = await apiPost<string>('location/section/add', {
|
||||
bookId: currentEntityId,
|
||||
locationName: newSectionName,
|
||||
}, token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('add_location_section', {data: {
|
||||
bookId: currentEntityId,
|
||||
sectionId,
|
||||
locationName: newSectionName,
|
||||
}});
|
||||
if (!sectionId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddSection'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!sectionId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddSection'));
|
||||
return;
|
||||
}
|
||||
const newLocation: LocationProps = {
|
||||
id: sectionId,
|
||||
name: newSectionName,
|
||||
@@ -291,50 +241,30 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
try {
|
||||
let elementId: string;
|
||||
if (isSeriesMode) {
|
||||
const addData = {
|
||||
locationId: sectionId,
|
||||
name: newElementNames[sectionId],
|
||||
};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
elementId = await tauri.addSeriesLocationElement(addData);
|
||||
} else {
|
||||
elementId = await System.authPostToServer<string>(
|
||||
'series/location/element/add',
|
||||
addData,
|
||||
token,
|
||||
lang
|
||||
);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('add_series_location_element', {data: addData});
|
||||
}
|
||||
}
|
||||
elementId = await apiPost<string>(
|
||||
'series/location/element/add',
|
||||
{
|
||||
locationId: sectionId,
|
||||
name: newElementNames[sectionId],
|
||||
},
|
||||
token,
|
||||
lang
|
||||
);
|
||||
if (!elementId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddElement'));
|
||||
return;
|
||||
}
|
||||
} else if (isCurrentlyOffline() || book?.localBook) {
|
||||
elementId = await tauri.addLocationElement(sectionId, newElementNames[sectionId]);
|
||||
} else {
|
||||
elementId = await System.authPostToServer<string>(`location/element/add`, {
|
||||
bookId: currentEntityId,
|
||||
locationId: sectionId,
|
||||
elementName: newElementNames[sectionId],
|
||||
},
|
||||
token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('add_location_element', {data: {
|
||||
bookId: currentEntityId,
|
||||
locationId: sectionId,
|
||||
elementId,
|
||||
elementName: newElementNames[sectionId],
|
||||
}});
|
||||
elementId = await apiPost<string>('location/element/add', {
|
||||
bookId: currentEntityId,
|
||||
locationId: sectionId,
|
||||
elementName: newElementNames[sectionId],
|
||||
}, token, lang);
|
||||
if (!elementId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddElement'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!elementId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddElement'));
|
||||
return;
|
||||
}
|
||||
const updatedSections: LocationProps[] = [...sections];
|
||||
const sectionIndex: number = updatedSections.findIndex(
|
||||
(section: LocationProps): boolean => section.id === sectionId,
|
||||
@@ -355,7 +285,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function handleElementChange(
|
||||
sectionId: string,
|
||||
elementIndex: number,
|
||||
@@ -370,7 +300,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
updatedSections[sectionIndex].elements[elementIndex][field] = value;
|
||||
setSections(updatedSections);
|
||||
}
|
||||
|
||||
|
||||
async function handleAddSubElement(
|
||||
sectionId: string,
|
||||
elementIndex: number,
|
||||
@@ -384,49 +314,30 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
);
|
||||
try {
|
||||
let subElementId: string;
|
||||
const elementId = sections[sectionIndex].elements[elementIndex].id;
|
||||
if (isSeriesMode) {
|
||||
const addData = {
|
||||
elementId: elementId,
|
||||
name: newSubElementNames[elementIndex],
|
||||
};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
subElementId = await tauri.addSeriesLocationSubElement(addData);
|
||||
} else {
|
||||
subElementId = await System.authPostToServer<string>(
|
||||
'series/location/sub-element/add',
|
||||
addData,
|
||||
token,
|
||||
lang
|
||||
);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('add_series_location_sub_element', {data: addData});
|
||||
}
|
||||
}
|
||||
subElementId = await apiPost<string>(
|
||||
'series/location/sub-element/add',
|
||||
{
|
||||
elementId: sections[sectionIndex].elements[elementIndex].id,
|
||||
name: newSubElementNames[elementIndex],
|
||||
},
|
||||
token,
|
||||
lang
|
||||
);
|
||||
if (!subElementId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddSubElement'));
|
||||
return;
|
||||
}
|
||||
} else if (isCurrentlyOffline() || book?.localBook) {
|
||||
subElementId = await tauri.addLocationSubElement(elementId, newSubElementNames[elementIndex]);
|
||||
} else {
|
||||
subElementId = await System.authPostToServer<string>(`location/sub-element/add`, {
|
||||
elementId: elementId,
|
||||
subElementId = await apiPost<string>('location/sub-element/add', {
|
||||
elementId: sections[sectionIndex].elements[elementIndex].id,
|
||||
subElementName: newSubElementNames[elementIndex],
|
||||
}, token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('add_location_sub_element', {data: {
|
||||
elementId: elementId,
|
||||
subElementId,
|
||||
subElementName: newSubElementNames[elementIndex],
|
||||
}});
|
||||
if (!subElementId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddSubElement'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!subElementId) {
|
||||
errorMessage(t('locationComponent.errorUnknownAddSubElement'));
|
||||
return;
|
||||
}
|
||||
const updatedSections: LocationProps[] = [...sections];
|
||||
updatedSections[sectionIndex].elements[elementIndex].subElements.push({
|
||||
id: subElementId,
|
||||
@@ -460,45 +371,30 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
][field] = value;
|
||||
setSections(updatedSections);
|
||||
}
|
||||
|
||||
|
||||
async function handleRemoveElement(
|
||||
sectionId: string,
|
||||
elementIndex: number,
|
||||
): Promise<void> {
|
||||
try {
|
||||
let response: boolean;
|
||||
const elementId = sections.find((section: LocationProps): boolean => section.id === sectionId)
|
||||
const elementId: string | undefined = sections.find((section: LocationProps): boolean => section.id === sectionId)
|
||||
?.elements[elementIndex].id;
|
||||
const deletedAt: number = System.timeStampInSeconds();
|
||||
let success: boolean;
|
||||
if (isSeriesMode) {
|
||||
const deleteData = {elementId: elementId, deletedAt};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
response = await tauri.deleteSeriesLocationElement(deleteData.elementId!, deleteData.deletedAt);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>('series/location/element/delete', deleteData, token, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('delete_series_location_element', {data: deleteData});
|
||||
}
|
||||
}
|
||||
} else if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.deleteLocationElement(elementId!, currentEntityId, deletedAt);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>(`location/element/delete`, {
|
||||
elementId: elementId, bookId: currentEntityId, deletedAt,
|
||||
success = await apiDelete<boolean>('series/location/element/delete', {
|
||||
elementId: elementId
|
||||
}, token, lang);
|
||||
} else {
|
||||
success = await apiDelete<boolean>('location/element/delete', {
|
||||
elementId: elementId,
|
||||
}, token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('delete_location_element', {data: {
|
||||
elementId: elementId, bookId: currentEntityId, deletedAt,
|
||||
}});
|
||||
}
|
||||
}
|
||||
if (!response) {
|
||||
if (!success) {
|
||||
errorMessage(t('locationComponent.errorUnknownDeleteElement'));
|
||||
return;
|
||||
}
|
||||
const updatedSections: LocationProps[] = [...sections];
|
||||
const sectionIndex: number = updatedSections.findIndex((section: LocationProps): boolean => section.id === sectionId,);
|
||||
const sectionIndex: number = updatedSections.findIndex((section: LocationProps): boolean => section.id === sectionId);
|
||||
updatedSections[sectionIndex].elements.splice(elementIndex, 1);
|
||||
setSections(updatedSections);
|
||||
} catch (e: unknown) {
|
||||
@@ -516,39 +412,25 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
subElementIndex: number,
|
||||
): Promise<void> {
|
||||
try {
|
||||
let response: boolean;
|
||||
const subElementId = sections.find((section: LocationProps): boolean => section.id === sectionId)?.elements[elementIndex].subElements[subElementIndex].id;
|
||||
const deletedAt: number = System.timeStampInSeconds();
|
||||
const subElementId: string | undefined = sections.find((section: LocationProps): boolean => section.id === sectionId)
|
||||
?.elements[elementIndex].subElements[subElementIndex].id;
|
||||
let success: boolean;
|
||||
if (isSeriesMode) {
|
||||
const deleteData = {subElementId: subElementId, deletedAt};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
response = await tauri.deleteSeriesLocationSubElement(deleteData.subElementId!, deleteData.deletedAt);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>('series/location/sub-element/delete', deleteData, token, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('delete_series_location_sub_element', {data: deleteData});
|
||||
}
|
||||
}
|
||||
} else if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.deleteLocationSubElement(subElementId!, currentEntityId, deletedAt);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>(`location/sub-element/delete`, {
|
||||
subElementId: subElementId, bookId: currentEntityId, deletedAt,
|
||||
success = await apiDelete<boolean>('series/location/sub-element/delete', {
|
||||
subElementId: subElementId
|
||||
}, token, lang);
|
||||
} else {
|
||||
success = await apiDelete<boolean>('location/sub-element/delete', {
|
||||
subElementId: subElementId,
|
||||
}, token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('delete_location_sub_element', {data: {
|
||||
subElementId: subElementId, bookId: currentEntityId, deletedAt,
|
||||
}});
|
||||
}
|
||||
}
|
||||
if (!response) {
|
||||
if (!success) {
|
||||
errorMessage(t('locationComponent.errorUnknownDeleteSubElement'));
|
||||
return;
|
||||
}
|
||||
const updatedSections: LocationProps[] = [...sections];
|
||||
const sectionIndex: number = updatedSections.findIndex((section: LocationProps): boolean => section.id === sectionId,);
|
||||
updatedSections[sectionIndex].elements[elementIndex].subElements.splice(subElementIndex, 1,);
|
||||
const sectionIndex: number = updatedSections.findIndex((section: LocationProps): boolean => section.id === sectionId);
|
||||
updatedSections[sectionIndex].elements[elementIndex].subElements.splice(subElementIndex, 1);
|
||||
setSections(updatedSections);
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
@@ -561,36 +443,21 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
|
||||
async function handleRemoveSection(sectionId: string): Promise<void> {
|
||||
try {
|
||||
let response: boolean;
|
||||
const deletedAt: number = System.timeStampInSeconds();
|
||||
let success: boolean;
|
||||
if (isSeriesMode) {
|
||||
const deleteData = {locationId: sectionId, deletedAt};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
response = await tauri.deleteSeriesLocation(deleteData.locationId, deleteData.deletedAt);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>('series/location/delete', deleteData, token, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('delete_series_location', {data: deleteData});
|
||||
}
|
||||
}
|
||||
} else if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.deleteLocationSection(sectionId, currentEntityId, deletedAt);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>(`location/delete`, {
|
||||
locationId: sectionId, bookId: currentEntityId, deletedAt,
|
||||
success = await apiDelete<boolean>('series/location/delete', {
|
||||
locationId: sectionId
|
||||
}, token, lang);
|
||||
} else {
|
||||
success = await apiDelete<boolean>('location/delete', {
|
||||
locationId: sectionId,
|
||||
}, token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('delete_location_section', {data: {
|
||||
locationId: sectionId, bookId: currentEntityId, deletedAt,
|
||||
}});
|
||||
}
|
||||
}
|
||||
if (!response) {
|
||||
if (!success) {
|
||||
errorMessage(t('locationComponent.errorUnknownDeleteSection'));
|
||||
return;
|
||||
}
|
||||
const updatedSections: LocationProps[] = sections.filter((section: LocationProps): boolean => section.id !== sectionId,);
|
||||
const updatedSections: LocationProps[] = sections.filter((section: LocationProps): boolean => section.id !== sectionId);
|
||||
setSections(updatedSections);
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
@@ -603,20 +470,9 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
|
||||
async function handleSave(): Promise<void> {
|
||||
try {
|
||||
let response: boolean;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.updateLocations(sections) as boolean;
|
||||
} else {
|
||||
response = await System.authPostToServer<boolean>(`location/update`, {
|
||||
locations: sections,
|
||||
}, token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === currentEntityId)) {
|
||||
addToQueue('update_locations', {data: {
|
||||
locations: sections,
|
||||
}});
|
||||
}
|
||||
}
|
||||
const response: boolean = await apiPost<boolean>(`location/update`, {
|
||||
locations: sections,
|
||||
}, token, lang);
|
||||
if (!response) {
|
||||
errorMessage(t('locationComponent.errorUnknownSave'));
|
||||
return;
|
||||
@@ -630,23 +486,23 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleExportToSeries(section: LocationProps): Promise<void> {
|
||||
if (!bookSeriesId) return;
|
||||
|
||||
|
||||
try {
|
||||
const seriesLocationId: string = await System.authPostToServer<string>('series/location/section/add', {
|
||||
const seriesLocationId: string = await apiPost<string>('series/location/section/add', {
|
||||
seriesId: bookSeriesId,
|
||||
name: section.name,
|
||||
}, token, lang);
|
||||
|
||||
|
||||
if (seriesLocationId) {
|
||||
const updateResponse: boolean = await System.authPostToServer<boolean>('location/section/update', {
|
||||
const updateResponse: boolean = await apiPost<boolean>('location/section/update', {
|
||||
sectionId: section.id,
|
||||
sectionName: section.name,
|
||||
seriesLocationId: seriesLocationId,
|
||||
}, token, lang);
|
||||
|
||||
|
||||
if (updateResponse) {
|
||||
setSections(sections.map((s: LocationProps): LocationProps =>
|
||||
s.id === section.id ? {...s, seriesLocationId: seriesLocationId} : s
|
||||
@@ -661,42 +517,42 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleImportFromSeries(seriesLocationId: string): Promise<void> {
|
||||
const seriesLocation: SeriesLocationItem | undefined = seriesLocations.find((location: SeriesLocationItem): boolean => location.id === seriesLocationId);
|
||||
if (!seriesLocation) return;
|
||||
|
||||
|
||||
try {
|
||||
const sectionId: string = await System.authPostToServer<string>('location/section/add', {
|
||||
const sectionId: string = await apiPost<string>('location/section/add', {
|
||||
bookId: currentEntityId,
|
||||
locationName: seriesLocation.name,
|
||||
seriesLocationId: seriesLocationId,
|
||||
}, token, lang);
|
||||
|
||||
|
||||
if (!sectionId) {
|
||||
errorMessage(t('locationComponent.importError'));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const importedElements: Element[] = [];
|
||||
|
||||
|
||||
for (const seriesElement of seriesLocation.elements) {
|
||||
const elementId: string = await System.authPostToServer<string>('location/element/add', {
|
||||
const elementId: string = await apiPost<string>('location/element/add', {
|
||||
bookId: currentEntityId,
|
||||
locationId: sectionId,
|
||||
elementName: seriesElement.name,
|
||||
}, token, lang);
|
||||
|
||||
|
||||
if (!elementId) continue;
|
||||
|
||||
|
||||
const importedSubElements: SubElement[] = [];
|
||||
|
||||
|
||||
for (const seriesSubElement of seriesElement.subElements) {
|
||||
const subElementId: string = await System.authPostToServer<string>('location/sub-element/add', {
|
||||
const subElementId: string = await apiPost<string>('location/sub-element/add', {
|
||||
elementId: elementId,
|
||||
subElementName: seriesSubElement.name,
|
||||
}, token, lang);
|
||||
|
||||
|
||||
if (subElementId) {
|
||||
importedSubElements.push({
|
||||
id: subElementId,
|
||||
@@ -705,7 +561,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
importedElements.push({
|
||||
id: elementId,
|
||||
name: seriesElement.name,
|
||||
@@ -713,7 +569,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
subElements: importedSubElements,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const newLocation: LocationProps = {
|
||||
id: sectionId,
|
||||
name: seriesLocation.name,
|
||||
@@ -732,9 +588,9 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{showToggle && !isSeriesMode && (
|
||||
<div className="bg-secondary/20 rounded-xl p-5 shadow-inner border border-secondary/30">
|
||||
<div className="bg-secondary rounded-xl p-4">
|
||||
<InputField
|
||||
icon={faToggleOn}
|
||||
icon={ToggleRight}
|
||||
fieldName={t('locationComponent.enableTool')}
|
||||
input={
|
||||
<ToggleSwitch
|
||||
@@ -755,13 +611,15 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
<SeriesImportSelector
|
||||
availableItems={seriesLocations
|
||||
.filter((seriesLocation: SeriesLocationItem): boolean => !sections.some((section: LocationProps): boolean => section.seriesLocationId === seriesLocation.id))
|
||||
.map((seriesLocation: SeriesLocationItem) => ({id: seriesLocation.id, name: seriesLocation.name}))}
|
||||
.map((seriesLocation: SeriesLocationItem) => ({
|
||||
id: seriesLocation.id,
|
||||
name: seriesLocation.name
|
||||
}))}
|
||||
onImport={handleImportFromSeries}
|
||||
placeholder={t("seriesImport.selectElement")}
|
||||
label={t("seriesImport.importFromSeries")}
|
||||
/>
|
||||
)}
|
||||
<div className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-4 border border-secondary/50">
|
||||
<div className="grid grid-cols-1 gap-4 mb-4">
|
||||
<InputField
|
||||
input={
|
||||
@@ -771,45 +629,37 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
placeholder={t("locationComponent.newSectionPlaceholder")}
|
||||
/>
|
||||
}
|
||||
actionIcon={faPlus}
|
||||
actionIcon={Plus}
|
||||
actionLabel={t("locationComponent.addSectionLabel")}
|
||||
addButtonCallBack={handleAddSection}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{sections.length > 0 ? (
|
||||
sections.map((section: LocationProps) => (
|
||||
<div key={section.id}
|
||||
className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-4 border border-secondary/50">
|
||||
<div key={section.id} className="space-y-4">
|
||||
<h3 className="text-lg font-semibold text-text-primary mb-4 flex items-center">
|
||||
<FontAwesomeIcon icon={faMapMarkerAlt} className="mr-2 w-5 h-5"/>
|
||||
<MapPin className="mr-2 w-5 h-5" strokeWidth={1.75}/>
|
||||
{section.name}
|
||||
<span
|
||||
className="ml-2 text-sm bg-dark-background text-text-secondary py-0.5 px-2 rounded-full">
|
||||
className="ml-2 text-sm bg-secondary text-text-secondary py-0.5 px-2 rounded-full">
|
||||
{section.elements.length || 0}
|
||||
</span>
|
||||
<div className="ml-auto flex items-center gap-2">
|
||||
{!isSeriesMode && bookSeriesId && !section.seriesLocationId && (
|
||||
<button
|
||||
onClick={(): Promise<void> => handleExportToSeries(section)}
|
||||
title={t("locationComponent.exportToSeries")}
|
||||
className="bg-blue-500/90 text-text-primary rounded-full p-1.5 hover:bg-blue-500 transition-colors shadow-md"
|
||||
>
|
||||
<FontAwesomeIcon icon={faShare} className={'w-5 h-5'}/>
|
||||
</button>
|
||||
<IconButton icon={Share2} variant="ghost" size="sm" shape="square"
|
||||
tooltip={t("locationComponent.exportToSeries")}
|
||||
onClick={(): Promise<void> => handleExportToSeries(section)}/>
|
||||
)}
|
||||
<button onClick={(): Promise<void> => handleRemoveSection(section.id)}
|
||||
className="bg-dark-background text-text-primary rounded-full p-1.5 hover:bg-secondary transition-colors shadow-md">
|
||||
<FontAwesomeIcon icon={faTrash} className={'w-5 h-5'}/>
|
||||
</button>
|
||||
<IconButton icon={Trash2} variant="danger" size="sm" shape="square"
|
||||
onClick={(): Promise<void> => handleRemoveSection(section.id)}/>
|
||||
</div>
|
||||
</h3>
|
||||
<div className="space-y-4">
|
||||
{section.elements.length > 0 ? (
|
||||
section.elements.map((element, elementIndex) => (
|
||||
<div key={element.id}
|
||||
className="bg-dark-background rounded-lg p-3 border-l-4 border-primary">
|
||||
className="bg-secondary rounded-lg p-3 border-l-4 border-primary">
|
||||
<div className="mb-2">
|
||||
<InputField
|
||||
input={
|
||||
@@ -824,17 +674,17 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
removeButtonCallBack={(): Promise<void> => handleRemoveElement(section.id, elementIndex)}
|
||||
/>
|
||||
</div>
|
||||
<TexteAreaInput
|
||||
<TextAreaInput
|
||||
value={element.description}
|
||||
setValue={(e: React.ChangeEvent<HTMLTextAreaElement>): void => handleElementChange(section.id, elementIndex, 'description', e.target.value)}
|
||||
placeholder={t("locationComponent.elementDescriptionPlaceholder")}
|
||||
/>
|
||||
|
||||
<div className="mt-4 pt-4 border-t border-secondary/50">
|
||||
|
||||
<div className="mt-4 pt-4 border-t border-secondary">
|
||||
{element.subElements.length > 0 && (
|
||||
<h4 className="text-sm italic text-text-secondary mb-3">{t("locationComponent.subElementsHeading")}</h4>
|
||||
)}
|
||||
|
||||
|
||||
{element.subElements.map((subElement: SubElement, subElementIndex: number) => (
|
||||
<div key={subElement.id}
|
||||
className="bg-darkest-background rounded-lg p-3 mb-3">
|
||||
@@ -852,7 +702,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
removeButtonCallBack={(): Promise<void> => handleRemoveSubElement(section.id, elementIndex, subElementIndex)}
|
||||
/>
|
||||
</div>
|
||||
<TexteAreaInput
|
||||
<TextAreaInput
|
||||
value={subElement.description}
|
||||
setValue={(e) =>
|
||||
handleSubElementChange(section.id, elementIndex, subElementIndex, 'description', e.target.value)
|
||||
@@ -861,7 +711,7 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
|
||||
|
||||
<InputField
|
||||
input={
|
||||
<TextInput
|
||||
@@ -885,13 +735,16 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
{t("locationComponent.noElementAvailable")}
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
<InputField
|
||||
input={
|
||||
<TextInput
|
||||
value={newElementNames[section.id] || ''}
|
||||
setValue={(e: ChangeEvent<HTMLInputElement>) =>
|
||||
setNewElementNames({...newElementNames, [section.id]: e.target.value})
|
||||
setNewElementNames({
|
||||
...newElementNames,
|
||||
[section.id]: e.target.value
|
||||
})
|
||||
}
|
||||
placeholder={t("locationComponent.newElementPlaceholder")}
|
||||
/>
|
||||
@@ -902,9 +755,8 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<div
|
||||
className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-8 border border-secondary/50 text-center">
|
||||
<p className="text-text-secondary mb-4">{t("locationComponent.noSectionAvailable")}</p>
|
||||
<div className="text-center py-8">
|
||||
<p className="text-text-secondary">{t("locationComponent.noSectionAvailable")}</p>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
@@ -913,4 +765,4 @@ export function LocationComponent(props: LocationComponentProps, ref: React.Ref<
|
||||
);
|
||||
}
|
||||
|
||||
export default forwardRef(LocationComponent);
|
||||
export default forwardRef(LocationComponent);
|
||||
Reference in New Issue
Block a user