Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Claims settings #78

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO system_config ("key", "value", "groupId") VALUES ('claims.showName', 'true', 'global');
3 changes: 3 additions & 0 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type Config = {
method: SuggestionMethod;
};
smtp: SMTPConfig;
claims: {
showName: boolean;
};
};

type Option = {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/components/admin/SettingsForm/Claims.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script lang="ts">
export let enabled: boolean;
</script>

<div class="flex flex-col space-y-2">
<h2 class="h2">Claims</h2>
<label class="unstyled flex flex-row space-x-2">
<input
id="claimsShowName"
name="claimsShowName"
class="checkbox"
type="checkbox"
bind:checked={enabled}
/>
<span>Show Name</span>
</label>
</div>
8 changes: 6 additions & 2 deletions src/lib/components/admin/SettingsForm/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import PublicSignup from "./PublicSignup.svelte";
import Suggestions from "./Suggestions.svelte";
import Smtp from "./SMTP.svelte";
import Claims from "./Claims.svelte";

export let config: Config;
$: form = $page.form;
Expand Down Expand Up @@ -33,7 +34,7 @@
};
}}
>
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div class="grid grid-cols-1 gap-4 md:grid-cols-3">
<div class="col-span-1">
<PublicSignup bind:enabled={config.enableSignup} />
</div>
Expand All @@ -43,8 +44,11 @@
bind:method={config.suggestions.method}
/>
</div>
<div class="col-span-1">
<Claims bind:enabled={config.claims.showName} />
</div>

<div class="col-span-1 md:col-span-2">
<div class="col-span-1 md:col-span-3">
<Smtp
bind:enabled={config.smtp.enable}
bind:host={config.smtp.host}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/components/wishlists/ItemCard/ClaimButtons.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export let item: FullItem;
export let user: PartialUser;
export let showName: boolean;

const dispatch = createEventDispatcher();
</script>
Expand All @@ -30,8 +31,10 @@
<span>Purchased</span>
</label>
</div>
{:else}
{:else if showName}
<span>Claimed by {item.pledgedBy?.name}</span>
{:else}
<span>Claimed</span>
{/if}
{:else}
<button class="variant-filled-secondary btn btn-sm md:btn" on:click={() => dispatch("claim")}>
Expand Down
2 changes: 2 additions & 0 deletions src/lib/components/wishlists/ItemCard/ItemCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

export let item: FullItem;
export let user: PartialUser & { userId: string };
export let showClaimedName: boolean;
export let showFor = false;

const modalStore = getModalStore();
Expand Down Expand Up @@ -162,6 +163,7 @@
<footer class="card-footer flex flex-row justify-between">
<ClaimButtons
{item}
showName={showClaimedName}
{user}
on:claim={() => handleClaim()}
on:unclaim={() => handleClaim(true)}
Expand Down
22 changes: 17 additions & 5 deletions src/lib/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export enum ConfigKey {
SMTP_USER = "smtp.user",
SMTP_PASS = "smtp.pass",
SMTP_FROM = "smtp.from",
SMTP_FROM_NAME = "smtp.fromName"
SMTP_FROM_NAME = "smtp.fromName",
CLAIMS_SHOW_NAME = "claims.showName"
}

type GroupConfig = Partial<Pick<Config, "suggestions">>;
type GroupConfig = Partial<Pick<Config, "suggestions" | "claims">>;

export const getConfig = async (groupId?: string): Promise<Config> => {
let configItems = await client.systemConfig.findMany({
Expand Down Expand Up @@ -69,10 +70,14 @@ export const getConfig = async (groupId?: string): Promise<Config> => {
method: (configMap[ConfigKey.SUGGESTIONS_METHOD] as SuggestionMethod) || "approval",
...groupConfig.suggestions
},
smtp: smtpConfig
smtp: smtpConfig,
claims: {
showName: configMap[ConfigKey.CLAIMS_SHOW_NAME] === "true",
...groupConfig.claims
}
};

return config as Config;
return config;
};

const getGroupConfig = async (groupId: string): Promise<GroupConfig> => {
Expand All @@ -92,6 +97,9 @@ const getGroupConfig = async (groupId: string): Promise<GroupConfig> => {
suggestions: {
enable: configMap[ConfigKey.SUGGESTIONS_ENABLE] === "true",
method: (configMap[ConfigKey.SUGGESTIONS_METHOD] as SuggestionMethod) || "approval"
},
claims: {
showName: configMap[ConfigKey.CLAIMS_SHOW_NAME] === "true"
}
};
}
Expand All @@ -107,6 +115,9 @@ const createDefaultConfig = async (): Promise<void> => {
},
smtp: {
enable: false
},
claims: {
showName: true
}
};

Expand All @@ -115,7 +126,6 @@ const createDefaultConfig = async (): Promise<void> => {

export const writeConfig = async (config: Partial<Config>, groupId = GLOBAL) => {
const configMap: Record<string, string | null | undefined> = {};
if (config.enableSignup) configMap[ConfigKey.SIGNUP_ENABLE] = config?.enableSignup?.toString();

if (config.smtp) {
configMap[ConfigKey.SMTP_ENABLE] = config?.smtp?.enable.toString();
Expand All @@ -127,8 +137,10 @@ export const writeConfig = async (config: Partial<Config>, groupId = GLOBAL) =>
configMap[ConfigKey.SMTP_FROM_NAME] = config?.smtp?.fromName;
}

configMap[ConfigKey.SIGNUP_ENABLE] = config?.enableSignup?.toString();
configMap[ConfigKey.SUGGESTIONS_ENABLE] = config?.suggestions?.enable.toString();
configMap[ConfigKey.SUGGESTIONS_METHOD] = config?.suggestions?.method;
configMap[ConfigKey.CLAIMS_SHOW_NAME] = config?.claims?.showName.toString();

for (const [key, value] of Object.entries(configMap)) {
await client.systemConfig.upsert({
Expand Down
3 changes: 2 additions & 1 deletion src/lib/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ export const settingSchema = z.object({
smtpUser: z.string().optional(),
smtpPass: z.string().optional(),
smtpFrom: z.string().optional(),
smtpFromName: z.string().optional()
smtpFromName: z.string().optional(),
claimsShowName: z.coerce.boolean().default(false)
});
6 changes: 5 additions & 1 deletion src/routes/admin/groups/[groupId]/settings/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const actions: Actions = {
const formData = Object.fromEntries(await request.formData());
const groupSettingSchema = settingSchema.pick({
suggestionMethod: true,
enableSuggestions: true
enableSuggestions: true,
claimsShowName: true
});

const configData = groupSettingSchema.safeParse(formData);
Expand All @@ -63,6 +64,9 @@ export const actions: Actions = {
suggestions: {
enable: configData.data.enableSuggestions,
method: configData.data.suggestionMethod
},
claims: {
showName: configData.data.claimsShowName
}
};
await writeConfig(newConfig, params.groupId);
Expand Down
16 changes: 12 additions & 4 deletions src/routes/admin/groups/[groupId]/settings/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { enhance } from "$app/forms";
import Claims from "$lib/components/admin/SettingsForm/Claims.svelte";
import Suggestions from "$lib/components/admin/SettingsForm/Suggestions.svelte";
import type { PageData } from "./$types";

Expand All @@ -18,10 +19,17 @@
};
}}
>
<Suggestions
bind:enabled={data.config.suggestions.enable}
bind:method={data.config.suggestions.method}
/>
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div class="col-span-1">
<Suggestions
bind:enabled={data.config.suggestions.enable}
bind:method={data.config.suggestions.method}
/>
</div>
<div class="col-span-1">
<Claims bind:enabled={data.config.claims.showName} />
</div>
</div>

<button class="variant-filled-primary btn mt-2" type="submit">
{#if saved}
Expand Down
6 changes: 4 additions & 2 deletions src/routes/admin/settings/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const actions: Actions = {
},
settings: async ({ request }) => {
const formData = Object.fromEntries(await request.formData());

const configData = settingSchema.safeParse(formData);

if (!configData.success) {
Expand Down Expand Up @@ -79,7 +78,10 @@ const generateConfig = (configData: z.infer<typeof settingSchema>) => {
enable: configData.enableSuggestions,
method: configData.suggestionMethod
},
smtp: smtpConfig
smtp: smtpConfig,
claims: {
showName: configData.claimsShowName
}
};

return newConfig;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/claims/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
out:send|local={{ key: item.id }}
animate:flip={{ duration: 200 }}
>
<ItemCard {item} showFor user={data.user} />
<ItemCard {item} showClaimedName showFor user={data.user} />
</div>
{/each}
{#each items.filter((item) => item.purchased) as item (item.id)}
Expand All @@ -51,7 +51,7 @@
out:send|local={{ key: item.id }}
animate:flip={{ duration: 200 }}
>
<ItemCard {item} showFor user={data.user} />
<ItemCard {item} showClaimedName showFor user={data.user} />
</div>
{/each}
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/wishlists/[username]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const load: PageServerLoad = async ({ locals, params, depends, url }) =>
},
items: wishlistItems.filter((item) => item.approved),
approvals: wishlistItems.filter((item) => !item.approved),
suggestionsEnabled: config.suggestions.enable
suggestionsEnabled: config.suggestions.enable,
showClaimedName: config.claims.showName
};
};
4 changes: 2 additions & 2 deletions src/routes/wishlists/[username]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
out:send|local={{ key: item.id }}
animate:flip={{ duration: 200 }}
>
<ItemCard {item} user={data.user} />
<ItemCard {item} showClaimedName={data.showClaimedName} user={data.user} />
</div>
{/each}
</div>
Expand Down Expand Up @@ -106,7 +106,7 @@
out:send|local={{ key: item.id }}
animate:flip={{ duration: 200 }}
>
<ItemCard {item} user={data.user} />
<ItemCard {item} showClaimedName={data.showClaimedName} user={data.user} />
</div>
{/each}
</div>
Expand Down
Loading