-
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
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import * as proto from "@bufbuild/protobuf";
import { Button, type ButtonProps, DropdownMenu } from "@radix-ui/themes";
import { WriteWorkRecordRequestSchema } from "@yamori/proto/yamori/worker/v1/write_work_record_request_pb.js";
import { WriteWorkRecordResponseSchema } from "@yamori/proto/yamori/worker/v1/write_work_record_response_pb.js";
import { type DateSchema } from "@yamori/proto/yamori/type/v1/date_pb.js";
import { type WorkerSchema } from "@yamori/proto/yamori/worker/v1/worker_pb.js";
import { type WorkspaceSchema } from "@yamori/proto/yamori/workspace/v1/workspace_pb.js";
import { type FC } from "react";
import { useMethodMutation } from "../../../contexts/Service.tsx";
import { useToast } from "../../../contexts/Toast.tsx";
import { IllegalMessageError } from "../../../errors/IllegalMessageError.ts";
export interface ActionTarget {
worker: proto.MessageShape<typeof WorkerSchema>;
dates: proto.MessageShape<typeof DateSchema>[];
}
export interface MenuProps
extends Pick<ButtonProps, "size" | "variant" | "color" | "className" | "style"> {
workspaceID: proto.MessageShape<typeof WorkspaceSchema>["id"];
targets: readonly ActionTarget[];
onWorkRecordMutated?(): void;
}
export const Menu: FC<MenuProps> = ({
targets,
workspaceID,
onWorkRecordMutated,
...rest
}) => {
const toast = useToast();
const isWorkRecordWritable =
targets.length > 0 && targets.every((target) => target.worker.writeWorkRecordKey);
const writeRecord = useMethodMutation({
service: "yamori.worker.v1.WorkerService",
method: "WriteWorkRecord",
request: {
schema: WriteWorkRecordRequestSchema,
},
response: {
schema: WriteWorkRecordResponseSchema,
},
mapResponse(resp) {
if (resp.result.case === "ok") {
return resp.result.value.workRecords;
}
if (typeof resp.result.case === "undefined") {
throw new IllegalMessageError(resp);
}
throw resp.result.value;
},
options: {
onError() {
toast.open({
severity: "danger",
title: "勤怠記録の更新に失敗しました",
});
},
},
});
return (
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button {...rest} loading={writeRecord.isPending}>
操作
<DropdownMenu.TriggerIcon />
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Label>記録書き込み</DropdownMenu.Label>
<DropdownMenu.Item
disabled={!isWorkRecordWritable}
onSelect={async () => {
try {
for (const target of targets) {
await writeRecord.mutateAsync({
workspaceId: workspaceID,
workerId: target.worker.id,
writeWorkRecordKey: target.worker.writeWorkRecordKey,
workRecord: {
dates: target.dates,
record: {
case: "workingDay",
value: {
hasWorkerWorked: true,
},
},
},
});
}
toast.open({
severity: "success",
title: "勤怠記録を更新しました",
});
} finally {
onWorkRecordMutated?.();
}
}}
>
出勤
</DropdownMenu.Item>
<DropdownMenu.Item
disabled={!isWorkRecordWritable}
onSelect={async () => {
try {
for (const target of targets) {
await writeRecord.mutateAsync({
workspaceId: workspaceID,
workerId: target.worker.id,
writeWorkRecordKey: target.worker.writeWorkRecordKey,
workRecord: {
dates: target.dates,
record: {
case: "dayOff",
value: {},
},
},
});
}
toast.open({
severity: "success",
title: "勤怠記録を更新しました",
});
} finally {
onWorkRecordMutated?.();
}
}}
>
休日
</DropdownMenu.Item>
<DropdownMenu.Item
disabled={!isWorkRecordWritable}
onSelect={async () => {
try {
for (const target of targets) {
await writeRecord.mutateAsync({
workspaceId: workspaceID,
workerId: target.worker.id,
writeWorkRecordKey: target.worker.writeWorkRecordKey,
workRecord: {
dates: target.dates,
record: {
case: "workingDay",
value: {
hasWorkerWorked: false,
},
},
},
});
}
toast.open({
severity: "success",
title: "勤怠記録を更新しました",
});
} finally {
onWorkRecordMutated?.();
}
}}
>
欠勤
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
);
};