Skip to content

Commit

Permalink
Replace client.call() with the respective SDK methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Feb 7, 2024
1 parent ac8a9a9 commit eff7074
Show file tree
Hide file tree
Showing 32 changed files with 401 additions and 1,187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,27 @@ import type { PageLoad } from './$types';

export const load: PageLoad = async ({ params, url, route, depends }) => {
depends(Dependencies.USER_TARGETS);

const page = getPage(url);
const limit = getLimit(url, route, PAGE_LIMIT);
const offset = pageToOffset(page, limit);
const search = getSearch(url);
const query = getQuery(url);

const parsedQueries = queryParamToMap(query || '[]');
queries.set(parsedQueries);

const payload = {
queries: [
Query.limit(limit),
Query.offset(offset),
Query.orderDesc(''),
...parsedQueries.values()
]
};

if (search) {
payload['search'] = search;
}

// TODO: remove when the API is ready with data
// This allows us to mock w/ data and when search returns 0 results
const targets: { targets: Models.Target[]; total: number } = await sdk.forProject.client.call(
'GET',
new URL(`${sdk.forProject.client.config.endpoint}/users/${params.user}/targets`),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
},
payload
);
const targets = await sdk.forProject.users.listTargets(params.user, [
Query.limit(limit),
Query.offset(offset),
Query.orderDesc(''),
...parsedQueries.values()
]);

const promisesById: Record<string, Promise<Models.Provider>> = {};
targets.targets.forEach((target) => {
if (target.providerId && !promisesById[target.providerId]) {
promisesById[target.providerId] = sdk.forProject.client.call(
'GET',
new URL(
`${sdk.forProject.client.config.endpoint}/messaging/providers/${target.providerId}`
),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
promisesById[target.providerId] = sdk.forProject.messaging.getProvider(
target.providerId
);
}
});
Expand All @@ -71,7 +44,6 @@ export const load: PageLoad = async ({ params, url, route, depends }) => {
return {
offset,
limit,
search,
query,
targets,
providersById
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,13 @@
const create = async () => {
try {
const payload = {
targetId: id ? id : ID.unique(),
await sdk.forProject.users.createTarget(
$page.params.user,
id ? id : ID.unique(),
providerType,
identifier
};
if (providerId) {
payload['providerId'] = providerId;
}
if (name) {
payload['name'] = name;
}
await sdk.forProject.client.call(
'POST',
new URL(
`${sdk.forProject.client.config.endpoint}/users/${$page.params.user}/targets`
),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
},
payload
identifier,
providerId ?? undefined,
name ?? undefined
);
show = false;
addNotification({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,9 @@
async function handleDelete() {
showDelete = false;
async function deleteTarget(id: string) {
await sdk.forProject.client.call(
'DELETE',
new URL(
`${sdk.forProject.client.config.endpoint}/users/${$page.params.user}/targets/${id}`
),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
);
}
const promises = selectedIds.map((id) => deleteTarget(id));
const promises = selectedIds.map((id) =>
sdk.forProject.users.deleteTarget($page.params.user, id)
);
try {
await Promise.all(promises);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,12 @@
let error = '';
onMount(async () => {
// TODO: Remove type cast when console SDK is updated
labels = [...($user as unknown as { labels: string[] }).labels];
labels = [...$user.labels];
});
async function updateLabels() {
try {
// TODO: Use SDK method when console SDK is updated
// await sdk.forProject.users.updateLabels($user.$id, labels);
const path = `/users/${$user.$id}/labels`;
await sdk.forProject.client.call(
'PUT',
new URL(sdk.forConsole.client.config.endpoint + path),
{
'content-type': 'application/json'
},
{
labels: labels
}
);
await sdk.forProject.users.updateLabels($user.$id, labels);
await invalidate(Dependencies.USER);
isDisabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ export const load: PageLoad = async ({ params }) => {
const period = isValueOfStringEnum(FunctionUsageRange, params.period)
? params.period
: FunctionUsageRange.ThirtyDays;
return sdk.forProject.functions.getFunctionUsage(
params.function,
period
);
return sdk.forProject.functions.getFunctionUsage(params.function, period);
} catch (e) {
throw error(e.code, e.message);
}
Expand Down
35 changes: 4 additions & 31 deletions src/routes/console/project-[project]/messaging/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
pageToOffset
} from '$lib/helpers/load';
import { sdk } from '$lib/stores/sdk';
import { Query, type Models } from '@appwrite.io/console';
import { Query } from '@appwrite.io/console';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ url, route }) => {
Expand All @@ -24,38 +24,11 @@ export const load: PageLoad = async ({ url, route }) => {
const parsedQueries = queryParamToMap(query || '[]');
queries.set(parsedQueries);

// TODO: remove when the API is ready with data
// This allows us to mock w/ data and when search returns 0 results
let messages: {
messages: ({ data: Record<string, string> } & Models.Message)[];
total: number;
} = { messages: [], total: 0 };
const params = {
queries: [
Query.limit(limit),
Query.offset(offset),
Query.orderDesc(''),
...parsedQueries.values()
]
};

if (search) {
params['search'] = search;
}

const response = await sdk.forProject.client.call(
'GET',
new URL(sdk.forProject.client.config.endpoint + '/messaging/messages'),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
},
params
const messages = await sdk.forProject.messaging.listMessages(
[Query.limit(limit), Query.offset(offset), Query.orderDesc(''), ...parsedQueries.values()],
search || undefined
);

messages = response;

return {
offset,
limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,16 @@ import Header from './header.svelte';
import { sdk } from '$lib/stores/sdk';
import { Dependencies } from '$lib/constants';
import { error } from '@sveltejs/kit';
import type { Models } from '@appwrite.io/console';

export const load: LayoutLoad = async ({ params, depends }) => {
depends(Dependencies.MESSAGING_MESSAGE);

try {
const response: Models.Message = await sdk.forProject.client.call(
'GET',
new URL(
`${sdk.forProject.client.config.endpoint}/messaging/messages/${params.message}`
),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
);
const message = await sdk.forProject.messaging.getMessage(params.message);

const topicsById = {};
const topicsPromise = Promise.allSettled(
response.topics.map((topicId) => {
return sdk.forProject.client.call(
'GET',
new URL(`${sdk.forProject.client.config.endpoint}/messaging/topics/${topicId}`),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
);
})
message.topics.map((topicId) => sdk.forProject.messaging.getTopic(topicId))
).then((results) => {
results.forEach((result) => {
if (result.status === 'fulfilled') {
Expand All @@ -44,18 +23,8 @@ export const load: LayoutLoad = async ({ params, depends }) => {
});

const targetsById = {};
const targetsPromise = sdk.forProject.client
.call(
'GET',
new URL(
`${sdk.forProject.client.config.endpoint}/messaging/messages/${params.message}/targets`
),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
)
const targetsPromise = sdk.forProject.messaging
.listTargets(params.message)
.then((response) => {
response.targets.forEach((target) => {
targetsById[target.$id] = target;
Expand All @@ -65,11 +34,11 @@ export const load: LayoutLoad = async ({ params, depends }) => {
await Promise.allSettled([topicsPromise, targetsPromise]);

return {
message,
topicsById,
targetsById,
header: Header,
breadcrumbs: Breadcrumbs,
message: response
breadcrumbs: Breadcrumbs
};
} catch (e) {
throw error(e.code, e.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
import { message } from './store';
import SMSPreview from './smsPreview.svelte';
import PushPreview from './pushPreview.svelte';
import {
messageParams,
operation,
providerType,
targetsById
} from '../wizard/store';
import { messageParams, operation, providerType, targetsById } from '../wizard/store';
import { topicsById } from '../store';
import { wizard } from '$lib/stores/wizard';
import Wizard from '../wizard.svelte';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@
const deleteMessage = async () => {
try {
await sdk.forProject.client.call(
'DELETE',
new URL(
`${sdk.forProject.client.config.endpoint}/messaging/messages/${$message.$id}`
),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
);
await sdk.forProject.messaging.delete($message.$id);
show = false;
addNotification({
type: 'success',
Expand Down
40 changes: 10 additions & 30 deletions src/routes/console/project-[project]/messaging/providers/+page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query, type Models } from '@appwrite.io/console';
import { Query } from '@appwrite.io/console';
import { sdk } from '$lib/stores/sdk';
import {
View,
Expand All @@ -25,41 +25,21 @@ export const load = async ({ depends, url, route }) => {
const parsedQueries = queryParamToMap(query || '[]');
queries.set(parsedQueries);

// TODO: get rid of demo data
let providers: { providers: Models.Provider[]; total: number } = { providers: [], total: 0 };
const params = {
queries: [
Query.limit(limit),
Query.offset(offset),
Query.orderDesc(''),
...parsedQueries.values()
]
};

if (search) {
params['search'] = search;
}

const response = await sdk.forProject.client.call(
'GET',
new URL(sdk.forProject.client.config.endpoint + '/messaging/providers'),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
},
params
);

providers = response;

return {
offset,
limit,
search,
query,
page,
view,
providers
providers: sdk.forProject.messaging.listProviders(
[
Query.limit(limit),
Query.offset(offset),
Query.orderDesc(''),
...parsedQueries.values()
],
search || undefined
)
};
};
Loading

0 comments on commit eff7074

Please sign in to comment.