Skip to content
Discussion options

You must be logged in to vote

Something like this. Not sure currying is a good practice, docs say use derived with multiple stores.

<script lang="ts">
	import { readable, derived } from 'svelte/store';

	const requiredUserLevels: Record<string, Record<string, number> | undefined | undefined> = {
		view: {
			page: 999,
		},
	};

	const user = readable({ name: 'admin', userLevel: 999 });

	const can = derived(user, ($user) => (v: string, p: string) => {
		const requiredLevel = requiredUserLevels[v]?.[p];
		if (!requiredLevel) return false;
		return $user.userLevel >= requiredLevel;
	});
</script>

<p>
	{$can('view', 'page')}
	{$can('edit', 'page')}
	{$can('view', 'dashboard')}
	{$can('edit', 'dashboard')}
</p>

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@unikitty37
Comment options

Answer selected by unikitty37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants