basic functionality

This commit is contained in:
Dovi Cowan 2023-10-16 20:37:07 +01:00
commit 1402cf392e
Signed by: dcowan
GPG key ID: C110D77558FBC227
26 changed files with 5240 additions and 0 deletions

1
.env Normal file
View file

@ -0,0 +1 @@
VITE_APPWRITE_DB_ID=tehillim-split

24
.gitignore vendored Normal file
View file

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

3
.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
}

47
README.md Normal file
View file

@ -0,0 +1,47 @@
# Svelte + Vite
This template should help get you started developing with Svelte in Vite.
## Recommended IDE Setup
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
## Need an official Svelte framework?
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
## Technical considerations
**Why use this over SvelteKit?**
- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
**Why include `.vscode/extensions.json`?**
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
**Why enable `checkJs` in the JS template?**
It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.
**Why is HMR not preserving my local component state?**
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
```js
// store.js
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```

13
index.html Normal file
View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tehillim Split</title>
</head>
<body class="dark:bg-gray-700 dark:text-white">
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>

33
jsconfig.json Normal file
View file

@ -0,0 +1,33 @@
{
"compilerOptions": {
"moduleResolution": "Node",
"target": "ESNext",
"module": "ESNext",
/**
* svelte-preprocess cannot figure out whether you have
* a value or a type, so tell TypeScript to enforce using
* `import type` instead of `import` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
* To have warnings / errors of the Svelte compiler at the
* correct position, enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable this if you'd like to use dynamic types.
*/
"checkJs": true
},
/**
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}

2963
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

35
package.json Normal file
View file

@ -0,0 +1,35 @@
{
"name": "tehillim-split",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^2.0.0",
"autoprefixer": "^10.4.16",
"postcss": "^8.4.31",
"prettier": "^3.0.3",
"prettier-plugin-svelte": "^3.0.3",
"prettier-plugin-tailwindcss": "^0.5.5",
"serve": "^14.2.1",
"svelte": "^3.54.0",
"tailwindcss": "^3.3.3",
"vite": "^4.0.0",
"yarn": "^1.22.19"
},
"dependencies": {
"appwrite": "^13.0.0",
"svelte-navigator": "^3.2.2",
"svelte-routing": "^2.5.0"
},
"prettier": {
"plugins": [
"prettier-plugin-tailwindcss",
"prettier-plugin-svelte"
]
}
}

6
postcss.config.js Normal file
View file

@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}

30
src/App.svelte Normal file
View file

@ -0,0 +1,30 @@
<script>
import { Route, Router } from "svelte-navigator";
import Home from "./routes/Home.svelte";
import Login from "./routes/Login.svelte";
// import { account } from "./lib/appwrite";
import Register from "./routes/Register.svelte";
import EmailVerify from "./routes/EmailVerify.svelte";
import ListView from "./routes/Lists/ListView.svelte";
import ListCreate from "./routes/Lists/ListCreate.svelte";
import ListMembers from "./routes/Lists/ListMembers.svelte";
export let url = "";
</script>
<Router {url}>
<Route path="/register/emailVerify" component={EmailVerify} />
<Route path="/register" component={Register} />
<Route path="/login" component={Login} />
<Route path="/list/:id/members" component={ListMembers} />
<Route path="/list/:id" component={ListView} />
<Route path="/lists/create" component={ListCreate} />
<Route path="/" component={Home} />
</Router>
<!-- <style>
p.error {
color: red;
}
</style> -->

3
src/app.css Normal file
View file

@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

83
src/app.css.old Normal file
View file

@ -0,0 +1,83 @@
:root {
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
font-size: 16px;
line-height: 24px;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
.card {
padding: 2em;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
button,input {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
transition: border-color 0.25s;
}
button {
cursor: pointer;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

10
src/lib/Header.svelte Normal file
View file

@ -0,0 +1,10 @@
<script>
import { logOut, loggedInUser } from "./appwrite";
</script>
<div class="">Welcome, {loggedInUser.name} <a href="#" class="text-blue-400" on:click={logOut}>Log out</a></div>
{#if !loggedInUser.emailVerification}
<div class="">You have not verified your email address yet</div>
{/if}

35
src/lib/appwrite.js Normal file
View file

@ -0,0 +1,35 @@
import { Account, Client, Databases, Teams } from 'appwrite';
import { navigate } from 'svelte-navigator';
const appwrite = new Client();
appwrite
.setEndpoint("https://api.fnukapps.com/v1")
.setProject("6524683fc8d947fe9a07")
export const account = new Account(appwrite);
export const teams = new Teams(appwrite);
export const logOut = async() => {
await account.deleteSession("current");
window.location.reload();
}
export const database = new Databases(appwrite);
export let loggedInUser = null;
await account.get().then((r) => {
loggedInUser = r;
}).catch(() => {
loggedInUser = null;
})
export function checkloggedin() {
if (loggedInUser == null) {
window.location.href = "/login";
}
}
export { ID } from 'appwrite';

8
src/main.js Normal file
View file

@ -0,0 +1,8 @@
import './app.css'
import App from './App.svelte'
const app = new App({
target: document.getElementById('app'),
})
export default app

View file

@ -0,0 +1,3 @@
<script>
</script>

43
src/routes/Home.svelte Normal file
View file

@ -0,0 +1,43 @@
<script>
import { navigate } from "svelte-navigator/src/history";
import {
account,
checkloggedin,
database,
logOut,
loggedInUser,
} from "../lib/appwrite";
import { Link } from "svelte-navigator";
import Header from "../lib/Header.svelte";
// navigate("/login", {});
// console.log(loggedInUser);
checkloggedin();
const lists = database.listDocuments(
import.meta.env.VITE_APPWRITE_DB_ID,
"lists",
);
</script>
<main class="container m-5">
<Header />
<div class="my-5">
<h1 class="text-2xl">My Lists</h1>
{#await lists}
Loading...
{:then lists}
{#if lists.total == 0}
You have no lists yet! <Link to="/lists/create" class="text-blue-400"
>Create one now</Link
>
{/if}
{#each lists.documents as list}
<Link class="text-blue-400" to="/list/{list['$id']}">{list['title']}</Link>
{/each}
{/await}
</div>
</main>

View file

@ -0,0 +1,88 @@
<script>
import { Permission, Role } from "appwrite";
import Header from "../../lib/Header.svelte";
import {
ID,
account,
database,
loggedInUser,
teams,
} from "../../lib/appwrite";
let listname = "";
let requireLoggedIn = false;
function createList(e) {
e.preventDefault();
database
.createDocument("tehillim-split", "lists", ID.unique(), {
title: listname,
require_logged_in: requireLoggedIn,
owner_id: loggedInUser["$id"],
})
.then((r) => {
teams.create(r["$id"], listname).then((r) => {
let permissions = [
Permission.read(Role.user(loggedInUser["$id"])),
Permission.read(Role.team(r["$id"])),
Permission.write(Role.team("admins-" + r["$id"])),
];
if (requireLoggedIn == false) {
permissions.push(Permission.read(Role.any()));
}
teams.create("admins-" + r["$id"], listname + " Admins").then(() => {
console.log(r);
database.updateDocument(
"tehillim-split",
"lists",
r["$id"],
{},
permissions,
);
});
});
console.log(r);
});
}
</script>
<main class="container m-5">
<Header />
<div class="my-5">
<form on:submit={createList}>
<fieldset
class="container justify-center p-2 border border-black dark:border-current"
>
<legend class="text-2xl">Create new list</legend>
<input
class="dark:bg-gray-500 dark:placeholder-white border border-black dark:border-current rounded p-1"
type="text"
placeholder="List name"
bind:value={listname}
required
/> <br />
<input
class="w-5 h-5 border-solid border-white my-3"
name="requireLoggedIn"
id="requireLoggedIn"
type="checkbox"
bind:value={requireLoggedIn}
/>
<label for="requireLoggedIn"
>Require users to be logged in to sign up for this list?</label
><br />
<button
class="dark:bg-gray-600 hover:bg-gray-500 bg-gray-400 border p-1 mt-2 rounded"
type="submit">Create</button
>
</fieldset>
</form>
</div>
</main>

View file

@ -0,0 +1,43 @@
<script>
import { Link } from "svelte-navigator";
import Header from "../../lib/Header.svelte";
import { database, teams } from "../../lib/appwrite";
export let id;
let list = database.getDocument("tehillim-split", "lists", id);
let members = teams.listMemberships(id);
let adminMembers = teams.listMemberships("admins-" + id);
const memberIndex = (adminMembers, i) => {
console.log(adminMembers);
let index = adminMembers.memberships.findIndex((e) => e.userId == i.userId);
return index;
};
function inviteMember() {
prompt("Enter member email address")
}
</script>
<main class="container m-5">
<Header />
<div class="my-5">
{#await members}
Loading...
{:then members}
{#await adminMembers then adminMembers}
{#each members.memberships as member}
<div class="m-1">
{member.userName} - {member.userEmail}
{#if memberIndex(adminMembers, member) !== -1}
<span class="border p-1">List Admin</span>
{/if}
</div>
{/each}
<Link class="text-blue-400" to="#" on:click={inviteMember}>Invite member</Link>
{/await}
{/await}
</div>
</main>

View file

@ -0,0 +1,159 @@
<script>
import { ID, Permission, Query, Role } from "appwrite";
import Header from "../../lib/Header.svelte";
import { database, loggedInUser } from "../../lib/appwrite";
import { Link } from "svelte-navigator";
export let id;
let list = database.getDocument("tehillim-split", "lists", id);
let perakim = [];
let perakimPromise = database
.listDocuments("tehillim-split", "perakim", [Query.equal("list_id", [id])])
.then((r) => {
perakim = r.documents;
});
const perekIndex = (i) => {
let index = perakim.findIndex((e) => e.perek == i);
return index;
};
function takePerek(perek) {}
function takeNewPerek(perek) {
console.log(perek);
database
.createDocument(
"tehillim-split",
"perakim",
ID.unique(),
{
list_id: id,
perek: perek,
taken: true,
taken_by: loggedInUser["$id"],
},
[
Permission.read(Role.team(id)),
Permission.write(Role.user(loggedInUser["$id"])),
Permission.write(Role.team("admins-" + id)),
],
)
.then(() => {
perakimPromise = database
.listDocuments("tehillim-split", "perakim", [
Query.equal("list_id", [id]),
])
.then((r) => {
perakim = r.documents;
});
});
}
function completePerek(perek) {
let perekDBId = perakim[perekIndex(perek)]["$id"];
database
.updateDocument("tehillim-split", "perakim", perekDBId, {
completed: true,
})
.then(() => {
perakimPromise = database
.listDocuments("tehillim-split", "perakim", [
Query.equal("list_id", [id]),
])
.then((r) => {
perakim = r.documents;
});
});
}
function uncompletePerek(perek) {
let perekDBId = perakim[perekIndex(perek)]["$id"];
database
.updateDocument("tehillim-split", "perakim", perekDBId, {
completed: false,
})
.then(() => {
perakimPromise = database
.listDocuments("tehillim-split", "perakim", [
Query.equal("list_id", [id]),
])
.then((r) => {
perakim = r.documents;
});
});
}
function untakePerek(perek) {
let perekDBId = perakim[perekIndex(perek)]["$id"];
database.deleteDocument("tehillim-split", "perakim", perekDBId).then(() => {
perakimPromise = database
.listDocuments("tehillim-split", "perakim", [
Query.equal("list_id", [id]),
])
.then((r) => {
perakim = r.documents;
});
});
}
</script>
<main class="container m-5">
<Header />
<div class="my-5">
{#await list}
Loading...
{:then list}
<h1 class="text-2xl">List {list.title}</h1>
<br />
<Link class="text-blue-400" to="/list/{id}/members">Members</Link><br>
{#await perakimPromise then perakimResolved}
{#each Array.from(Array(150 + 1).keys()).slice(1) as i}
{#if perekIndex(i) !== -1}
{#if perakim[perekIndex(i)].taken == false}
Perek {i}
<button
on:click={() => takePerek(i)}
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
>Take</button
>
{:else if perakim[perekIndex(i)].taken_by == loggedInUser["$id"] && perakim[perekIndex(i)].completed !== true}
Perek {i}
<button
on:click={() => untakePerek(i)}
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
>Untake</button
>
<button
on:click={() => completePerek(i)}
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
>Complete</button
>
{:else if perakim[perekIndex(i)].taken_by == loggedInUser["$id"] && perakim[perekIndex(i)].completed == true}
Perek {i}
<button
on:click={() => uncompletePerek(i)}
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
>Uncomplete</button
>
{:else}
Perek {i} - Taken
{/if}
{:else}
Perek {i}
<button
on:click={() => takeNewPerek(i)}
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
>Take</button
>
{/if}<br />
{/each}
{/await}
{/await}
</div>
</main>

85
src/routes/Login.svelte Normal file
View file

@ -0,0 +1,85 @@
<script>
import { navigate } from "svelte-navigator/src/history";
import { account, loggedInUser } from "../lib/appwrite";
import { useFocus } from "svelte-navigator";
const registerFocus = useFocus();
if (loggedInUser !== null) {
console.log(loggedInUser);
// location.href = "/";
navigate("/", {});
}
let email = "";
let password = "";
let error;
let submitButton;
function login(e) {
e.preventDefault();
submitButton.setAttribute("disabled", true);
error.innerText = "";
account
.createEmailSession(email, password)
.then((r) => {
// location.href = "/";
navigate("/", {});
})
.catch((e) => {
submitButton.removeAttribute("disabled");
error.innerText = e;
});
}
</script>
<form on:submit={login}>
<fieldset
class="container justify-center md:m-10 m-2 p-2 border border-black dark:border-current"
>
<legend>Log into Tehillim Split</legend>
<div class="my-2">
<input
class="dark:bg-gray-500 dark:placeholder-white border border-black dark:border-current rounded p-1"
placeholder="Email address"
type="email"
name="email"
id="email"
bind:value={email}
required
use:registerFocus
/><br />
</div>
<div class="my-2">
<input
class="dark:bg-gray-500 dark:placeholder-white border border-black dark:border-current rounded p-1"
placeholder="Password"
type="password"
name="password"
id="password"
bind:value={password}
required
/><br />
</div>
<button
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
type="submit"
bind:this={submitButton}>Log In</button
>
<button
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
type="button"
on:click={() => navigate("/register", {})}>Go to Register</button
><br />
<p class="text-red-600" bind:this={error}></p>
</fieldset>
</form>
<style>
</style>

View file

@ -0,0 +1,77 @@
<script>
import { navigate } from "svelte-navigator/src/history";
import { ID, account } from "../lib/appwrite";
let name = "";
let email = "";
let password = "";
let error;
async function register(e) {
e.preventDefault();
error.innerText = "";
await account
.create(ID.unique(), email, password)
.catch((e) => (error.innerText = e));
await account.createEmailSession(email, password);
await account.updateName(name).catch((e) => (error.innerText = e));
account
.createVerification("http://localhost:5173/register/emailVerify")
.then((r) => {
navigate("/");
})
.catch((e) => (error.innerText = e));
}
</script>
<form on:submit={register}>
<fieldset
class="container justify-center md:m-10 m-2 p-2 border border-black dark:border-current"
>
<legend>Register for Tehillim Split</legend>
<div class="my-2">
<input
placeholder="Name"
class="dark:bg-gray-500 dark:placeholder-white border border-black dark:border-current rounded p-1"
type="text"
name="name"
bind:value={name}
/>
</div>
<div class="my-2">
<input
placeholder="Email address"
class="dark:bg-gray-500 dark:placeholder-white border border-black dark:border-current rounded p-1"
type="email"
name="email"
bind:value={email}
/>
</div>
<div class="my-2">
<input
placeholder="Password"
class="dark:bg-gray-500 dark:placeholder-white border border-black dark:border-current rounded p-1"
type="password"
name="password"
bind:value={password}
/>
</div>
<button
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
type="submit">Register</button
>
<button
class="dark:bg-gray-600 bg-gray-400 border p-1 mt-2 rounded"
type="button"
on:click={() => navigate("/login")}>Go to Log In</button
><br />
<p class="text-red-600" bind:this={error}></p>
</fieldset>
</form>

2
src/vite-env.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />

12
tailwind.config.js Normal file
View file

@ -0,0 +1,12 @@
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{svelte,js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}

11
vite.config.js Normal file
View file

@ -0,0 +1,11 @@
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
build: {
target: "firefox100",
},
optimizeDeps: { exclude: ["svelte-navigator"] },
});

1423
yarn.lock Normal file

File diff suppressed because it is too large Load diff