Skip to content

Commit

Permalink
ts code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wilke committed Nov 13, 2023
1 parent b325139 commit 0b769b5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 40 deletions.
1 change: 1 addition & 0 deletions frontend/src/AppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IAppContext {
popPasteCloned: () => PasteClone | undefined
pushPasteCreated: (paste: PasteView) => void
popPasteCreated: () => PasteView | undefined
pushPasteDeleted: (paste: PasteView) => void
onPasteCreated: (callback: (paste: PasteView) => void) => void
onPasteDeleted: (callback: (paste: PasteView) => void) => void
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/CreatePaste/CreatePaste.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ type FormModel = {
content: string
expiry?: 'ONE_HOUR' | 'ONE_DAY' | 'ONE_WEEK' | 'ONE_MONTH' | 'THREE_MONTHS' | 'ONE_YEAR' | 'NEVER'
exposure?: 'PUBLIC' | 'UNLISTED' | 'ONCE'
password: string
password?: string
}

const CreatePaste: Component<CreatePasteProps> = ({onCreatePaste, initialPaste}): JSX.Element => {

const [form, setForm] = createStore<FormModel>({
title: initialPaste?.title || null,
content: initialPaste?.content || null,
password: null,
expiry: null,
exposure: null
exposure: null,
password: null,
});

const [lastPasteUrl, setLastPasteUrl] = createSignal<string>();
Expand All @@ -51,7 +51,7 @@ const CreatePaste: Component<CreatePasteProps> = ({onCreatePaste, initialPaste})
}

const resetStore = () => {
setLastPasteUrl(null);
setLastPasteUrl();
setForm({
title: null,
password: null,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ReadPaste/ReadPaste.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, createEffect, createSignal, JSX, lazy, on, Show} from 'solid-js';
import {Component, createEffect, createSignal, JSX, on, Show} from 'solid-js';
import linkifyElement from 'linkify-element';
import {PasteView} from '../../api/model/PasteView';
import {decrypt} from '../../crypto/CryptoUtil';
Expand Down
29 changes: 4 additions & 25 deletions frontend/src/components/SearchPastes/SearchPastes.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
import {Component, createResource, createSignal, JSX, Show} from 'solid-js';
import {Component, JSX, Show} from 'solid-js';
import {A} from '@solidjs/router';
import ApiClient from '../../api/client';
import {PasteSearchView} from '../../api/model/PasteSearchView';
import {toDateTimeString} from '../../datetime/DateTimeUtil';
import styles from "./searchPastes.module.css";

type SearchPastesProps = {
term: String
pastes: PasteSearchView
pastes: Array<PasteSearchView>
onSearchEnter: (term: String) => void
}

const SearchPastes: Component<SearchPastesProps> = ({term, pastes, onSearchEnter}): JSX.Element => {

const [search, setSearch] = createSignal<string>();

const [results, { refetch }] = createResource(() => search(), () => searchTerm());

let searchInput: HTMLInputElement;


const searchTerm = (): Promise<Array<PasteListView>> => {
if (search() && search().length >= 3) {
return ApiClient.searchAll(search());
}

return Promise.resolve([]);
}

const resetSearchForm = () => {
setSearch(null);
refetch();
}


const submitOnClick = (e: Event) => {
e.preventDefault();
if (searchInput.value?.length >= 3) {
Expand All @@ -55,7 +35,7 @@ const SearchPastes: Component<SearchPastesProps> = ({term, pastes, onSearchEnter
<>
<form autocomplete="off" class={styles.searchForm} onSubmit={submitOnClick}>
<fieldset>
<input ref={searchInput} onKeyUp={submitOnEnter} value={term} type="search" required minlength="3" maxlength="25" placeholder="Search for pastes" autofocus />
<input onKeyUp={submitOnEnter} value={term} type="search" required minlength="3" maxlength="25" placeholder="Search for pastes" autofocus />
<input type="submit" value="Search"/>
<input type="reset" value="Reset"/>
</fieldset>
Expand All @@ -64,7 +44,7 @@ const SearchPastes: Component<SearchPastesProps> = ({term, pastes, onSearchEnter
<Show when={term}>
<Show when={pastes.length} keyed fallback={<p>Nothing found</p>}>

<ol styles={styles.searchResults}>
<ol class={styles.searchResults}>
<For each={pastes}>{item =>
<li class={styles.item}>
<p><A href={'/paste/' + item.id}>{item.title || 'Untitled' }</A></p>
Expand All @@ -81,7 +61,6 @@ const SearchPastes: Component<SearchPastesProps> = ({term, pastes, onSearchEnter

</Show>
</Show>

</>
)
}
Expand Down
17 changes: 7 additions & 10 deletions frontend/src/pages/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import {JSX, createResource, Switch, Match} from 'solid-js';
import {A, useLocation, useSearchParams} from '@solidjs/router';
import {useSearchParams} from '@solidjs/router';
import ApiClient from '../api/client';
import {toDateTimeString} from '../datetime/DateTimeUtil';
import SearchPastes from '../components/SearchPastes/SearchPastes';
import Spinner from '../components/Spinner/Spinner';

const Search: () => JSX.Element = () => {

const location = useLocation();

const [searchTerm, setSearchTerm] = useSearchParams();

const effectiveTerm = () => {
return (searchTerm.q && searchTerm.q.length >= 3) ? searchTerm.q : null;
}
const [searchTerm, setSearchTerm] = useSearchParams<{q: string}>();

const [pastes] = createResource(
effectiveTerm,
(term) => ApiClient.searchAll(term),
{initialValue: []}
{ initialValue: [] }
);

function effectiveTerm(): string | null {
return (searchTerm.q && searchTerm.q.length >= 3) ? searchTerm.q : null;
}

function onSearchEnter(term: String) {
setSearchTerm({q: term})
}
Expand Down

0 comments on commit 0b769b5

Please sign in to comment.