Skip to content

Commit

Permalink
hot-key for creation, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
querwurzel committed Nov 15, 2023
1 parent a289e77 commit 88aea38
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 68 deletions.
4 changes: 1 addition & 3 deletions frontend/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import {PasteListView} from './model/PasteListView';
import {PasteView} from './model/PasteView';
import {PasteSearchView} from './model/PasteSearchView';

const HOST_DEVELOPMENT = 'localhost:3000';

const apiBaseUrl = () => {
switch (window.location.host) {
case HOST_DEVELOPMENT:
case 'localhost:3000': // development
return 'http://localhost:8080';
default:
return window.location.origin;
Expand Down
61 changes: 41 additions & 20 deletions frontend/src/components/CreatePaste/CreatePaste.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, createSignal, JSX, Show} from "solid-js";
import {Component, createSignal, JSX, Show, onMount, onCleanup} from "solid-js";
import {createStore} from "solid-js/store";
import {PasteCreateCmd} from "../../api/model/PasteCreateCmd";
import {encrypt} from "../../crypto/CryptoUtil";
Expand Down Expand Up @@ -35,33 +35,54 @@ const CreatePaste: Component<CreatePasteProps> = ({onCreatePaste, initialPaste})

const [lastPasteUrl, setLastPasteUrl] = createSignal<string>();

onMount(() => {
window.addEventListener("keydown", globalSubmitPaste);
})

onCleanup(() => {
window.removeEventListener("keydown", globalSubmitPaste);
})

let creationForm: HTMLFormElement
let submitInput: HTMLInputElement

const updateFormField = (fieldName: keyof FormModel) => (event: Event) => {
const inputElement = event.currentTarget as HTMLInputElement;
function globalSubmitPaste(e: KeyboardEvent) {
if (e.altKey || e.shiftKey) {
return;
}

setForm({
[fieldName]: inputElement.value
});
if ((e.ctrlKey && e.code === 'Enter') || (e.metaKey && e.code === 'Enter')) {
creationForm.requestSubmit();
}
}

function updateFormField(fieldName: keyof FormModel): (event: Event) => void {
return (event: Event) => {
const inputElement = event.currentTarget as HTMLInputElement;

setForm({
[fieldName]: inputElement.value
});
}
};

const resetCreateForm = () => {
creationForm?.reset();
function resetCreatePaste() {
setForm({
title: null,
password: null,
content: null,
expiry: null,
exposure: null
} as FormModel)
setLastPasteUrl();
submitInput.style.backgroundColor = null;
}

const resetStore = () => {
setLastPasteUrl();
setForm({
title: null,
password: null,
content: null,
expiry: null,
exposure: null
} as FormModel)
function resetCreateForm() {
creationForm?.reset();
}

const createPaste = (e: Event) => {
function createPaste(e: Event) {
e.preventDefault();

const data: PasteCreateCmd = {
Expand All @@ -79,14 +100,13 @@ const CreatePaste: Component<CreatePasteProps> = ({onCreatePaste, initialPaste})
onCreatePaste(data)
.then(url => {
resetCreateForm();
resetStore();
setLastPasteUrl(url);
})
.catch(e => submitInput.style.backgroundColor = 'red');
}

return (
<form ref={creationForm} onSubmit={createPaste} onReset={resetStore} autocomplete="off" class={styles.createForm}>
<form ref={creationForm} onSubmit={createPaste} onReset={resetCreatePaste} autocomplete="off" class={styles.createForm}>
<fieldset>
<div>
<label for="expiry">Expires in: </label>
Expand Down Expand Up @@ -147,6 +167,7 @@ const CreatePaste: Component<CreatePasteProps> = ({onCreatePaste, initialPaste})
rows="20"
cols="50"
placeholder="Paste here"
name="content"
onInput={updateFormField("content")}>{form.content}</textarea>
<span>{form.content?.length || 0} / 4096</span>
</div>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/ReadPaste/ReadPaste.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const ReadPaste: Component<ReadPasteProps> = ({paste, onClonePaste, onDeletePast

const [clearText, setClearText] = createSignal<string>();

createEffect(on(clearText, () => linkifyContent()));

let keyInput: HTMLInputElement;
let contentElement: HTMLPreElement;

createEffect(on(clearText, () => linkifyContent()));

onMount(() => {
window.addEventListener("keydown", globalSelectContent);
})
Expand Down
46 changes: 22 additions & 24 deletions frontend/src/components/RecentPastes/RecentPastes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,10 @@ const RecentPastes: () => JSX.Element = () => {

const [pastes, { mutate, refetch }] = createResource(ApiClient.findAll);

const appContext = AppContext;

let refetchSchedule;

function manualRefetch() {
restartSchedule();
refetch();
}

function startSchedule() {
refetchSchedule = window.setInterval(refetch, 60_000);
}

function stopSchedule() {
window.clearInterval(refetchSchedule);
}

function restartSchedule() {
stopSchedule();
startSchedule();
}

onMount(() => {
startSchedule();

appContext.onPasteCreated((paste) => {
AppContext.onPasteCreated((paste) => {
const newItem: PasteListView = {
id: paste.id,
title: paste.title,
Expand All @@ -50,14 +28,34 @@ const RecentPastes: () => JSX.Element = () => {
mutate(prev => [newItem].concat(prev))
});

appContext.onPasteDeleted((paste) => {
AppContext.onPasteDeleted((paste) => {
restartSchedule();
mutate(prev => prev.filter(item => item.id !== paste.id));
});
})

onCleanup(() => stopSchedule());

let refetchSchedule;

function manualRefetch() {
restartSchedule();
refetch();
}

function startSchedule() {
refetchSchedule = window.setInterval(refetch, 60_000);
}

function stopSchedule() {
window.clearInterval(refetchSchedule);
}

function restartSchedule() {
stopSchedule();
startSchedule();
}

return (
<div class={styles.recentPastes}>

Expand Down
15 changes: 6 additions & 9 deletions frontend/src/components/SearchPastes/SearchPastes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,25 @@ const SearchPastes: Component<SearchPastesProps> = ({term, pastes, onSearchEnter

let searchInput: HTMLInputElement;

const submitOnClick = (e: Event) => {
function submitOnClick(e: Event) {
e.preventDefault();

if (searchInput.value?.length >= 3) {
onSearchEnter(searchInput.value);
}
}

const submitOnEnter = (e: Event) => {
e.preventDefault();

if (searchInput.value?.length >= 3) {
if (e instanceof KeyboardEvent && e.key === "Enter") {
onSearchEnter(searchInput.value);
}
function submitOnEnter(e: Event) {
if (e instanceof KeyboardEvent && e.key === "Enter") {
submitOnClick(e);
}
}

return (
<>
<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 ref={searchInput} onKeyUp={submitOnEnter} name="term" 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
.searchForm input[type=search] {
display: inline-block;
width: 20rem;
margin: 0;
margin: .5rem;
}

.searchForm input[type=submit],
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/pages/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import CreatePaste from '../components/CreatePaste/CreatePaste';

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

const appContext = AppContext;

const navigate = useNavigate();

const onCreatePaste = (cmd: PasteCreateCmd): Promise<string> => {
Expand All @@ -22,7 +20,7 @@ const Create: () => JSX.Element = () => {
.catch(_ => {});

if (paste.isPublic) {
appContext.pushPasteCreated(paste);
AppContext.pushPasteCreated(paste);
}

if (!paste.isOneTime) {
Expand All @@ -34,7 +32,7 @@ const Create: () => JSX.Element = () => {
}

return (
<CreatePaste initialPaste={appContext.popPasteCloned()} onCreatePaste={onCreatePaste} />
<CreatePaste initialPaste={AppContext.popPasteCloned()} onCreatePaste={onCreatePaste} />
)
}

Expand Down
8 changes: 3 additions & 5 deletions frontend/src/pages/Read.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ const NotFound = lazy(() => import('./404'));

const Read: Component = (): JSX.Element => {

const appContext = AppContext;

const navigate = useNavigate();

const params = useParams<{id: string}>();

const [paste] = createResource(() => params.id, (id) => appContext.popPasteCreated() || ApiClient.findOne(id));
const [paste] = createResource(() => params.id, (id) => AppContext.popPasteCreated() || ApiClient.findOne(id));

const clonePaste = () => {
appContext.pushPasteCloned({
AppContext.pushPasteCloned({
title: paste().title,
content: paste().content
})
Expand All @@ -27,7 +25,7 @@ const Read: Component = (): JSX.Element => {
const deletePaste = () => {
ApiClient.deletePaste(paste().id)
.then(_ => {
appContext.pushPasteDeleted(paste());
AppContext.pushPasteDeleted(paste());
navigate('/')
})
.catch(() => {})
Expand Down

0 comments on commit 88aea38

Please sign in to comment.