Skip to content

Commit

Permalink
Better feedback on form submission.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Jun 23, 2024
1 parent 72d3c9b commit 54c83a2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
16 changes: 10 additions & 6 deletions src/lib/FormDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import Header from './Header.svelte';
import Paragraph from './Paragraph.svelte';
import Actions from './Actions.svelte';
import Oops from './Oops.svelte';
import Loading from './Loading.svelte';
export let button: string;
export let showTip: string;
export let submitTip: string;
export let submit: string;
export let header: string;
export let explanation: string;
export let action: () => void;
export let action: () => Promise<boolean>;
export let valid: () => boolean;
let show = false;
let submitting = false;
</script>

<Button tip={showTip} action={() => (show = true)}>{button}</Button>
Expand All @@ -25,9 +26,11 @@
<Header>{header}</Header>
<Paragraph>{explanation}</Paragraph>
<Form
action={() => {
action();
show = false;
action={async () => {
submitting = true;
const result = await action();
submitting = false;
if (result) show = false;
}}
>
<slot />
Expand All @@ -36,10 +39,11 @@
tip={submitTip}
end
submit
active={valid()}
active={!submitting && valid()}
action={() => {}}>{submit}</Button
>
</Actions>
{#if submitting}<Loading />{/if}
</Form>
</Dialog>
{/if}
24 changes: 11 additions & 13 deletions src/lib/NewOrganization.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@
let submitting = false;
async function create() {
if ($user === null || $user.email === undefined) return;
if ($user === null || $user.email === undefined) {
addError(errors, "You're not logged in.");
return false;
}
submitting = false;
try {
const id = await $db.createOrganization(orgName, name, invite, $user.id, $user.email);
const id = await $db.createOrganization(orgName, name, invite, $user.id, $user.email);
if (typeof id === 'string') goto(`/org/${id}`);
else
addError(
errors,
"Couldn't create a new organization with this invite code",
id ?? undefined
);
} catch (e) {
console.log(e);
addError(errors, "Couldn't create a new organization", undefined);
if (typeof id === 'string') {
goto(`/org/${id}`);
return true;
} else {
addError(errors, "Couldn't create a new organization with this invite code", id ?? undefined);
return false;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/lib/SuggestionView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,18 @@
header="Comment"
explanation="What do you want to say?"
valid={() => newComment.length > 0}
action={() => {
const result = $db.addComment(
action={async () => {
const result = await $db.addComment(
$org.getID(),
$user.id,
newComment,
'suggestions',
suggestion.id,
comments.data.map((c) => c.id)
);
if (result) return false;
newComment = '';
return result;
return true;
}}
>
<textarea
Expand Down
6 changes: 4 additions & 2 deletions src/routes/org/[orgid]/process/[processid]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,15 @@
header="Set a new concern"
explanation="Set a new concern to group processes."
valid={() => newConcern.length > 0 && $org.getConcerns().indexOf(newConcern) === -1}
action={() => {
queryOrError(
action={async () => {
const error = await queryOrError(
errors,
$db.updateProcessConcern(process, newConcern, $user.id),
"Couldn't update concern."
);
if (error) return false;
newConcern = '';
return true;
}}
>
<Field label="new concern" bind:text={newConcern} />
Expand Down
9 changes: 7 additions & 2 deletions src/routes/org/[orgid]/processes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@
let title = '';
async function newProcess() {
const { error, id } = await $db.addProcess($organization.getID(), title);
if (error) addError(errors, "Couldn't add new process", error);
else goto(`/org/${$organization.getID()}/process/${id}`);
if (error) {
addError(errors, "Couldn't add new process", error);
return false;
} else {
goto(`/org/${$organization.getID()}/process/${id}`);
return true;
}
}
</script>

Expand Down
19 changes: 15 additions & 4 deletions src/routes/org/[orgid]/roles/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,27 @@
async function createRole() {
const { data, error } = await $db.createRole($org.getID(), newRole);
if (data) goto(`/org/${$org.getID()}/role/${data.id}`);
else if (error) addError(errors, "We couldn't create the new role.", error);
if (data) {
goto(`/org/${$org.getID()}/role/${data.id}`);
return true;
} else if (error) {
addError(errors, "We couldn't create the new role.", error);
return false;
} else return false;
}
let newTeam = '';
async function createTeam() {
const { data, error } = await $db.createTeam($org.getID(), newTeam);
if (error) addError(errors, "We couldn't create the new team.", error);
else if (data) goto(`/org/${$org.getID()}/team/${data.id}`);
if (error) {
addError(errors, "We couldn't create the new team.", error);
return false;
} else if (data) {
goto(`/org/${$org.getID()}/team/${data.id}`);
return true;
}
return false;
}
</script>

Expand Down

0 comments on commit 54c83a2

Please sign in to comment.