-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.svelte
137 lines (120 loc) · 2.85 KB
/
index.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<script context="module" type="ts">
import TaskItem from "./../components/TaskItem.svelte";
import {
GET_TASKS,
ADD_TASK,
SUB_GET_TASKS,
DEL_TASK,
UPDATE_TASK,
} from "./../modules/queries";
import { operationStore, subscription, setClient } from "@urql/svelte";
import { auth, googleProvider } from "../modules/firebase";
import Profile from "./../components/Profile.svelte";
import client from "../modules/urql";
export async function preload() {
const dgTask = await client.query(GET_TASKS).toPromise();
return { dgTask: dgTask.data?.queryTask };
}
interface User {
displayName: string;
photoURL: string;
uid: string;
email: string;
}
</script>
<script type="ts">
export let dgTask: any[];
let user: User;
// Form Text
let text = "some task";
auth.onAuthStateChanged((u: any) => {
user = u;
});
if ((process as any).browser) {
setClient(client);
const getTasks = operationStore(SUB_GET_TASKS);
subscription(getTasks).subscribe((r: any) => {
dgTask = r.data ? r.data?.queryTask : [];
});
}
async function add() {
await client
.mutation(ADD_TASK, {
task: {
title: text,
completed: false,
user: { email: user.email },
},
})
.toPromise()
.then((r: any) => {
if (r.error) {
console.log(r.error);
}
});
text = "";
}
async function remove(event: any) {
const { id } = event.detail;
await client
.mutation(DEL_TASK, {
id: [id],
})
.toPromise()
.then((r: any) => {
if (r.error) {
console.log(r.error);
}
});
}
async function update(event: any) {
const { id, newStatus } = event.detail;
await client
.mutation(UPDATE_TASK, {
id: id,
completed: newStatus,
})
.toPromise()
.then((r: any) => {
if (r.error) {
console.log(r.error);
}
});
}
const onKeyPress = (e: any) => {
if (e.charCode === 13) add();
};
</script>
<svelte:head>
<title>Dgraph Sapper URQL</title>
</svelte:head>
<h1>Dgraph Sapper URQL</h1>
<section>
{#if user}
<Profile
displayName={user.displayName}
photoURL={user.photoURL}
uid={user.uid}
/>
<button on:click={() => auth.signOut()}>Logout</button>
<ul>
{#each dgTask as task (task.id)}
<li>
<TaskItem
id={task.id}
text={task.title}
completed={task.completed}
on:remove={remove}
on:toggle={update}
/>
</li>
{/each}
</ul>
<input bind:value={text} on:keypress={onKeyPress} />
<button on:click={add}>Add Task</button>
{:else}
<button on:click={() => auth.signInWithPopup(googleProvider)}
>Signin with Google</button
>
{/if}
</section>