-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathfeedback.ts
140 lines (131 loc) · 4.3 KB
/
feedback.ts
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
138
139
140
import { browser } from '$app/environment';
import { VARS } from '$lib/system';
import { writable } from 'svelte/store';
import type { SvelteComponent } from 'svelte';
import FeedbackGeneral from '$lib/components/feedback/feedbackGeneral.svelte';
import FeedbackNps from '$lib/components/feedback/feedbackNPS.svelte';
export type Feedback = {
elapsed: number;
visualized: number;
notification: boolean;
type: 'nps' | 'general';
show: boolean;
};
export type FeedbackData = {
message: string;
name?: string;
email?: string;
value?: number;
};
export type FeedbackOption = {
type: Feedback['type'];
title: string;
desc: string;
component: typeof SvelteComponent<unknown>;
};
export const feedbackOptions: FeedbackOption[] = [
{
type: 'general',
title: 'Help us improve Appwrite',
desc: 'Appwrite evolves with your input. Share your thoughts and help us improve Appwrite.',
component: FeedbackGeneral
},
{
type: 'nps',
title: 'Help us improve Appwrite',
desc: 'Appwrite evolves with your input. Share your thoughts and help us improve Appwrite. If you would like to be contacted regarding your feedback, please share your contact details below.',
component: FeedbackNps
}
];
export const selectedFeedback = writable<FeedbackOption>();
function createFeedbackDataStore() {
const { set, subscribe, update } = writable<FeedbackData>({
message: '',
name: '',
email: '',
value: null
});
return {
set,
subscribe,
update,
reset: () => {
update((feedbackData) => {
feedbackData.message = '';
feedbackData.name = '';
feedbackData.email = '';
feedbackData.value = null;
return feedbackData;
});
}
};
}
export const feedbackData = createFeedbackDataStore();
function createFeedbackStore() {
const { subscribe, update } = writable<Feedback>({
elapsed: browser ? parseInt(localStorage.getItem('feedbackElapsed')) ?? 0 : 0,
visualized: browser ? parseInt(localStorage.getItem('feedbackVisualized')) ?? 0 : 0,
notification: false,
type: 'nps', //TODO: change to general before release
show: false
});
return {
subscribe,
update,
toggleFeedback: () => {
update((feedback) => {
feedback.show = !feedback.show;
return feedback;
});
},
toggleNotification: () =>
update((feedback) => {
feedback.notification = !feedback.notification;
return feedback;
}),
switchType: (feedType: Feedback['type']) =>
update((feedback) => {
feedback.type = feedType;
return feedback;
}),
addVisualization: () =>
update((feedback) => {
feedback.visualized += 1;
localStorage.setItem('feedbackVisualized', feedback.visualized.toString());
return feedback;
}),
increaseElapsed: (time: number) => {
update((feedback) => {
feedback.elapsed += time;
localStorage.setItem('feedbackElapsed', feedback.elapsed.toString());
return feedback;
});
},
submitFeedback: async (
subject: string,
message: string,
firstname?: string,
email?: string,
value?: number
) => {
if (!VARS.GROWTH_ENDPOINT) return;
const response = await fetch(`${VARS.GROWTH_ENDPOINT}/feedback`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
subject,
message,
email,
firstname: firstname ? firstname : undefined,
customFields: value ? [{ id: '40655', value }] : undefined
})
});
if (response.status >= 400) {
throw new Error('Failed to submit feedback');
}
}
};
}
export const feedback = createFeedbackStore();