Skip to content

Commit

Permalink
finished modal createPost
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora01K committed Jan 6, 2024
1 parent f73c53e commit 2a5eb26
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 34 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@
"prettier-plugin-svelte": "^3.0.0",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tailwindcss": "3.3.6",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.4.2",
"vitest": "^0.32.2",
"tailwindcss": "3.3.6",
"vite-plugin-tailwind-purgecss": "0.1.4"
"vite-plugin-tailwind-purgecss": "0.1.4",
"vitest": "^0.32.2"
},
"type": "module",
"dependencies": {
"@floating-ui/dom": "1.5.3",
"@iconify/svelte": "^3.1.4",
"svelte-emoji-selector": "^1.0.1",
"svelte-i18n": "^4.0.0",
"svelte-local-storage-store": "^0.6.4",
"universal-cookie": "^6.1.1"
Expand Down
56 changes: 55 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 98 additions & 13 deletions src/components/ModalCreatePost.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,104 @@
<script lang="ts">
import { Modal, getModalStore } from "@skeletonlabs/skeleton";
import { serverURL, token } from '$lib/Store';
import Icon from '@iconify/svelte';
import { FileDropzone, getModalStore } from '@skeletonlabs/skeleton';
import { get } from 'svelte/store';
import { getToastStore } from '@skeletonlabs/skeleton';
import { createToast } from '$lib/Toasts';
import { t } from '../i18n';
const modalStore = getModalStore();
const toastStore = getToastStore();
const modalStore = getModalStore();
let textClick: boolean = true;
let imageClick: boolean = false;
function closeModal(){
modalStore.close();
};
let text: string = '';
function closeModal() {
modalStore.close();
}
function clickText() {
if (!textClick) {
textClick = true;
}
imageClick = false;
}
function clickImage() {
if (!imageClick) {
imageClick = true;
}
textClick = false;
}
async function sendPost() {
const url = get(serverURL) + '/posts';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + get(token)
},
body: JSON.stringify({
content: text
})
});
if (response.status == 201) {
modalStore.close();
} else {
toastStore.trigger(createToast('We were running into a problem! Sorry', 'error'));
}
}
</script>

{#if $modalStore[0]}
<div>
<header>Write Post</header>
<div class="flex flex-row">
<button type="button" class="btn variant-filled-secondary" on:click={closeModal}>Close</button>
<button type="button" class="btn variant-filled-primary">Post</button>
</div>
</div>
{/if}
<div class="card w-[40vw]">
<header class="card-header flex justify-center items-center">
<div class="w-min flex justify-center outline-none border-none">
<button class="p-2" on:click={clickText}>{$t('modalCreatePost.header.Text')}</button>
<button class="p-2" on:click={clickImage}>{$t('modalCreatePost.header.Image')}</button>
</div>
</header>
{#if textClick == true}
<section>
<label class="label p-3">
<textarea
class="textarea resize-none"
rows="4"
bind:value={text}
placeholder={text === '' ? $t('modalCreatePost.textInput.Placeholder') : ''}
maxlength="256"
/>
</label>
</section>
{/if}
{#if imageClick == true}
<section class="p-3 flex flex-col">
<FileDropzone name="files" accept="image/*">
<svelte:fragment slot="message">
<div class="flex flex-col items-center">
<Icon class="w-12 h-12" icon="line-md:upload-loop"></Icon>
<p>{$t('modalCreatePost.fileInput.Message')}</p>
</div>
</svelte:fragment>
</FileDropzone>
<label class="label p-3">
<textarea
class="textarea h-auto resize-none"
placeholder={$t('modalCreatePost.textInputImage.Placeholder')}
maxlength="256"
/>
</label>
</section>
{/if}
<footer class="card-footer flex flex-row justify-end">
<button type="button" class="btn variant-filled-secondary mx-1" on:click={closeModal}
>{$t('modalCreatePost.button.close')}</button
>
<button type="button" class="btn variant-filled-primary mx-1" on:click={sendPost}
>{$t('modalCreatePost.button.post')}</button
>
</footer>
</div>
{/if}
10 changes: 5 additions & 5 deletions src/components/navbar.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<script lang="ts">
import { AppBar, getModalStore, Modal, type ModalSettings } from '@skeletonlabs/skeleton';
import { AppBar, getModalStore, type ModalSettings } from '@skeletonlabs/skeleton';
import Icon from '@iconify/svelte';
import { get } from 'svelte/store';
import { token } from '$lib/Store';
const loginToken = get(token);
const modalStore = getModalStore();
const modal: ModalSettings ={
const modal: ModalSettings = {
type: 'component',
component: 'modalCreatePost',
};
component: 'modalCreatePost'
};
function handleLogout() {
token.set('');
Expand All @@ -20,6 +20,7 @@
modalStore.trigger(modal);
}
</script>

<AppBar>
<svelte:fragment slot="lead">
<div style="gap: 1rem; display: flex; align-items: center;">
Expand Down Expand Up @@ -52,4 +53,3 @@
</div>
</svelte:fragment>
</AppBar>

2 changes: 1 addition & 1 deletion src/lib/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const registerUsername = writable('');
export const serverURL = writable('http://192.168.0.126:3000/api');

export const token = persisted('token', '');
export const refreshToken = persisted('refreshToken', '');
export const refreshToken = persisted('refreshToken', '');
7 changes: 3 additions & 4 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
import ModalCreatePost from '../components/ModalCreatePost.svelte';
initializeStores();
const modalRegistry: Record<string, ModalComponent> = {
modalCreatePost: {ref: ModalCreatePost}
}
modalCreatePost: { ref: ModalCreatePost }
};
</script>

<Modal components={modalRegistry}/>
<Modal components={modalRegistry} />
<AppShell>
<svelte:fragment slot="header"><Navbar /></svelte:fragment>
<slot />
Expand Down
7 changes: 2 additions & 5 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
import { get } from 'svelte/store';
import Login from '../components/Login.svelte';
import { token } from '$lib/Store';
import { getModalStore, type ModalSettings } from '@skeletonlabs/skeleton';
const loginToken = get(token);
</script>

<main class="">
<div class="grid grid-cols-6 grid-flow-col gap-4 grid-end-auto">
<div class="col-span-1">
</div>
<div class="card col-span-4">
</div>
<div class="col-span-1"></div>
<div class="card col-span-4"></div>
<div class="grid grid-rows-6 grid-flow-col gap-4">
<div class="card row-start-1">
{#if loginToken == ''}<Login />{/if}
Expand Down
18 changes: 16 additions & 2 deletions src/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ export default {
'register.button.cancel': 'Cancel',
'register.button.register': 'Register',
'post.postComment.placeholder': 'Leave a comment!',
'post.comments.header': 'Comments'
'post.comments.header': 'Comments',
'modalCreatePost.header.Text': 'Text',
'modalCreatePost.header.Image': 'Image',
'modalCreatePost.textInput.Placeholder': 'Write your post here',
'modalCreatePost.fileInput.Message': 'Upload image or drag-and-drop',
'modalCreatePost.textInputImage.Placeholder': 'Write your optional text here...',
'modalCreatePost.button.close': 'Close',
'modalCreatePost.button.post': 'Post'
},
de: {
'imprint.frontend.header': 'Impressum Frontend',
Expand Down Expand Up @@ -97,6 +104,13 @@ export default {
'register.button.cancel': 'Abbrechen',
'register.button.register': 'Registrieren',
'post.postComment.placeholder': 'Schreib einen Kommentar!',
'post.comments.header': 'Kommentare'
'post.comments.header': 'Kommentare',
'modalCreatePost.header.Text': 'Text',
'modalCreatePost.header.Image': 'Bild',
'modalCreatePost.textInput.Placeholder': 'Schreibe deinen Beitrag hier',
'modalCreatePost.fileInput.Message': 'Lade ein Bild hoch oder drag-and-drop',
'modalCreatePost.textInputImage.Placeholder': 'Schreibe deinen optionalen Text hier...',
'modalCreatePost.button.close': 'Schließen',
'modalCreatePost.button.post': 'Posten'
}
};

0 comments on commit 2a5eb26

Please sign in to comment.