Skip to content

Commit

Permalink
feat: enable refreshInterval of SWR
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Aug 16, 2024
1 parent d1a8c0a commit 718c235
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion client/features/tasks/TaskList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import styles from './taskList.module.css';

export const TaskList = () => {
const { setAlert } = useAlert();
const { data: tasks, mutate: mutateTasks } = useAspidaSWR(apiClient.private.tasks);
const { data: tasks, mutate: mutateTasks } = useAspidaSWR(apiClient.private.tasks, {
refreshInterval: 5000,
});
const { lastMsg } = usePickedLastMsg(['taskCreated', 'taskUpdated', 'taskDeleted']);
const fileRef = useRef<HTMLInputElement | null>(null);
const [label, setLabel] = useState('');
Expand Down
2 changes: 1 addition & 1 deletion server/api/private/tasks/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineController(() => ({
status: 200,
body: await taskQuery
.listByAuthorId(prismaClient, user.id, query?.limit)
.then((tasks) => Promise.all(tasks.map(toTaskDto))),
.then((tasks) => tasks.map(toTaskDto)),
}),
post: {
validators: { body: taskValidator.taskCreate },
Expand Down
2 changes: 1 addition & 1 deletion server/api/private/tasks/di/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default defineController({ listByAuthorId: taskQuery.listByAuthorId }, (d
status: 200,
body: await taskQuery.findManyWithDI
.inject(deps)(prismaClient, user.id)
.then((tasks) => Promise.all(tasks.map(toTaskDto))),
.then((tasks) => tasks.map(toTaskDto)),
}),
}));
6 changes: 2 additions & 4 deletions server/domain/task/service/toTaskDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { brandedId } from 'service/brandedId';
import { s3 } from 'service/s3Client';
import type { TaskEntity } from '../model/taskType';

export const toTaskDto = async (task: TaskEntity): Promise<TaskDto> => ({
export const toTaskDto = (task: TaskEntity): TaskDto => ({
...task,
id: brandedId.task.dto.parse(task.id),
image: task.imageKey
? { s3Key: task.imageKey, url: await s3.getSignedUrl(task.imageKey) }
: undefined,
image: task.imageKey ? { s3Key: task.imageKey, url: s3.keyToUrl(task.imageKey) } : undefined,
author: { ...task.author, id: brandedId.user.dto.parse(task.author.id) },
});
6 changes: 3 additions & 3 deletions server/domain/task/useCase/taskUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const taskUseCase = {

await taskCommand.save(tx, created);

const dto = await toTaskDto(created.task);
const dto = toTaskDto(created.task);
taskEvent.created(user, dto);

return dto;
Expand All @@ -28,7 +28,7 @@ export const taskUseCase = {

await taskCommand.save(tx, updated);

const dto = await toTaskDto(updated.task);
const dto = toTaskDto(updated.task);
taskEvent.updated(user, dto);

return dto;
Expand All @@ -40,7 +40,7 @@ export const taskUseCase = {

await taskCommand.delete(tx, deleted);

const dto = await toTaskDto(deleted.task);
const dto = toTaskDto(deleted.task);
taskEvent.deleted(user, dto);

return dto;
Expand Down
4 changes: 4 additions & 0 deletions server/service/envValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const COGNITO_USER_POOL_CLIENT_ID = z
.parse(process.env.NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID);
const S3_ENDPOINT = z.string().parse(process.env.S3_ENDPOINT ?? '');
const S3_BUCKET = z.string().parse(process.env.S3_BUCKET ?? '');
const S3_PUBLIC_ENDPOINT =
z.string().url().optional().parse(process.env.S3_PUBLIC_ENDPOINT) ??
`${S3_ENDPOINT}/${S3_BUCKET}`;
const S3_ACCESS_KEY = z.string().parse(process.env.S3_ACCESS_KEY ?? '');
const S3_SECRET_KEY = z.string().parse(process.env.S3_SECRET_KEY ?? '');
const S3_REGION = z.string().parse(process.env.S3_REGION ?? '');
Expand All @@ -25,6 +28,7 @@ export {
S3_ACCESS_KEY,
S3_BUCKET,
S3_ENDPOINT,
S3_PUBLIC_ENDPOINT,
S3_REGION,
S3_SECRET_KEY,
SERVER_PORT,
Expand Down
10 changes: 9 additions & 1 deletion server/service/s3Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import {
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import type { MultipartFile } from '@fastify/multipart';
import { S3_ACCESS_KEY, S3_BUCKET, S3_ENDPOINT, S3_REGION, S3_SECRET_KEY } from './envValues';
import {
S3_ACCESS_KEY,
S3_BUCKET,
S3_ENDPOINT,
S3_PUBLIC_ENDPOINT,
S3_REGION,
S3_SECRET_KEY,
} from './envValues';

export type S3PutParams = { key: string; data: MultipartFile };

Expand All @@ -23,6 +30,7 @@ export const s3Client = new S3Client({
});

export const s3 = {
keyToUrl: (key: string): string => `${S3_PUBLIC_ENDPOINT}/${key}`,
getSignedUrl: async (key: string): Promise<string> => {
const command = new GetObjectCommand({ Bucket: S3_BUCKET, Key: key });

Expand Down

0 comments on commit 718c235

Please sign in to comment.