Skip to content

Commit

Permalink
Split project creation from add-to-project form
Browse files Browse the repository at this point in the history
Fixes #653
  • Loading branch information
allanlasser committed Oct 16, 2024
1 parent 51a5236 commit f5660db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
3 changes: 3 additions & 0 deletions src/lib/components/forms/EditProject.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Edit project metadata
function onSubmit({ submitter }) {
submitter.disabled = true;
return ({ result, update }) => {
if (result.type === "success") {
dispatch("success", result);
}
update(result);
dispatch("close");
};
Expand Down
70 changes: 24 additions & 46 deletions src/lib/components/forms/Projects.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@ because it needs to load all of a user's projects
and we don't want to do that everywhere.
-->
<script lang="ts">
import type { Writable } from "svelte/store";
import type { Document, Project, User } from "$lib/api/types";
import type { Document, Project } from "$lib/api/types";
import { enhance } from "$app/forms";
import { invalidate } from "$app/navigation";
import { createEventDispatcher, getContext, onMount } from "svelte";
import { createEventDispatcher, onMount } from "svelte";
import { _ } from "svelte-i18n";
import Button from "../common/Button.svelte";
import Field from "../inputs/Field.svelte";
import Flex from "../common/Flex.svelte";
import Switch from "../inputs/Switch.svelte";
import Text from "../inputs/Text.svelte";
import TextArea from "../inputs/TextArea.svelte";
import { getForUser, add, remove } from "$lib/api/projects";
import { getCsrfToken } from "$lib/utils/api";
import { intersection } from "@/util/array.js";
import { getCurrentUser } from "@/lib/utils/permissions";
import { PlusCircle16 } from "svelte-octicons";
import EditProject from "./EditProject.svelte";
import Portal from "../layouts/Portal.svelte";
import Modal from "../layouts/Modal.svelte";
export let documents: Document[] = [];
export let projects: Project[] = [];
const dispatch = createEventDispatcher();
const me = getCurrentUser();
let createProjectOpen = false;
let common: Set<number>;
$: common = new Set(
Expand Down Expand Up @@ -66,15 +65,8 @@ and we don't want to do that everywhere.
await invalidateDocs(documents);
}
function onSubmit({ submitter }) {
submitter.disabled = true;
return async ({ result, update }) => {
if (result.type === "success") {
projects = [...projects, result.data.project];
update(result);
}
submitter.disabled = false;
};
function onCreateSuccess(event: CustomEvent<{ data: { project: Project } }>) {
projects = [...projects, event.detail.data.project];
}
function sort(projects: Project[]) {
Expand All @@ -85,33 +77,9 @@ and we don't want to do that everywhere.
</script>

<div class="container">
<form method="post" action="/projects/" use:enhance={onSubmit}>
<h2>{$_("projects.create")}</h2>
<Field title={$_("projects.fields.title")} required inline>
<Text name="title" placeholder={$_("projects.fields.title")} required />
</Field>

<details>
<summary>{$_("common.more")} &hellip;</summary>
<Flex direction="column">
<Field title={$_("projects.fields.description")}>
<TextArea name="description" />
</Field>
<Field title={$_("projects.fields.private")} inline>
<Switch name="private" />
</Field>
<Field title={$_("projects.fields.pinned")} inline>
<Switch name="pinned" />
</Field>
</Flex>
</details>
<Flex>
<Button mode="primary" type="submit">
{$_("common.add")} +
</Button>
</Flex>
</form>

<Button ghost mode="primary" on:click={() => (createProjectOpen = true)}>
<PlusCircle16 /> Create Project
</Button>
{#if projects.length}
<hr class="divider" />
<Flex direction="column" class="projects">
Expand Down Expand Up @@ -139,13 +107,23 @@ and we don't want to do that everywhere.
<Button on:click={() => dispatch("close")}>{$_("dialog.done")}</Button>
</Flex>
</div>
{#if createProjectOpen}
<Portal>
<Modal on:close={() => (createProjectOpen = false)}>
<h1 slot="title">Create Project</h1>
<EditProject
on:close={() => (createProjectOpen = false)}
on:success={onCreateSuccess}
/>
</Modal>
</Portal>
{/if}

<style>
form,
.container {
display: flex;
flex-direction: column;
gap: 1rem;
gap: 0.5rem;
width: 100%;
}
Expand Down

0 comments on commit f5660db

Please sign in to comment.