|
| 1 | +import BackLink from "@/components/Common/BackLink"; |
| 2 | +import { useUsers } from "@litespace/headless/users"; |
| 3 | +import { useSendAdMessage } from "@litespace/headless/student"; |
| 4 | +import { IUser } from "@litespace/types"; |
| 5 | +import { Button } from "@litespace/ui/Button"; |
| 6 | +import { DateInput } from "@litespace/ui/DateInput"; |
| 7 | +import { useFormatMessage } from "@litespace/ui/hooks/intl"; |
| 8 | +import { Typography } from "@litespace/ui/Typography"; |
| 9 | +import dayjs from "dayjs"; |
| 10 | +import { useCallback, useState } from "react"; |
| 11 | +import { useOnError } from "@/hooks/error"; |
| 12 | +import { useToast } from "@litespace/ui/Toast"; |
| 13 | + |
| 14 | +const AdMessage: React.FC = () => { |
| 15 | + const intl = useFormatMessage(); |
| 16 | + const toast = useToast(); |
| 17 | + |
| 18 | + const [from, setFrom] = useState( |
| 19 | + dayjs().subtract(1, "day").format("YYYY-MM-DD") |
| 20 | + ); |
| 21 | + const [to, setTo] = useState(dayjs().format("YYYY-MM-DD")); |
| 22 | + |
| 23 | + const findStudents = useUsers({ |
| 24 | + role: IUser.Role.Student, |
| 25 | + createdAt: { |
| 26 | + gte: from, |
| 27 | + lte: to, |
| 28 | + }, |
| 29 | + full: true, |
| 30 | + }); |
| 31 | + |
| 32 | + const onError = useOnError({ |
| 33 | + type: "mutation", |
| 34 | + handler: (error) => toast.error({ title: intl(error.messageId) }), |
| 35 | + }); |
| 36 | + |
| 37 | + const sendAdMessageMutation = useSendAdMessage({ |
| 38 | + onSuccess: () => toast.success({ title: intl("labels.done") }), |
| 39 | + onError, |
| 40 | + }); |
| 41 | + |
| 42 | + const send = useCallback(() => { |
| 43 | + sendAdMessageMutation.mutate({ |
| 44 | + payload: { |
| 45 | + createdAt: { |
| 46 | + gte: from, |
| 47 | + lte: to, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }); |
| 51 | + }, [sendAdMessageMutation, from, to]); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="w-full flex flex-col gap-6 max-w-screen-2xl mx-auto p-6"> |
| 55 | + <BackLink /> |
| 56 | + |
| 57 | + <div className="flex flex-row gap-2 max-w-[350px] items-center"> |
| 58 | + <Typography tag="label">{intl("placeholders.from")}</Typography> |
| 59 | + <DateInput value={from} onChange={setFrom} /> |
| 60 | + </div> |
| 61 | + |
| 62 | + <div className="flex flex-row gap-2 max-w-[350px] items-center"> |
| 63 | + <Typography tag="label">{intl("placeholders.to")}</Typography> |
| 64 | + <DateInput value={to} onChange={setTo} /> |
| 65 | + </div> |
| 66 | + |
| 67 | + <div className="flex flex-col gap-1 max-h-[300px] overflow-auto"> |
| 68 | + {findStudents.query.data?.list.map((student) => |
| 69 | + student.phone ? ( |
| 70 | + <Typography tag="span"> |
| 71 | + {student.id} - {student.name} |
| 72 | + </Typography> |
| 73 | + ) : null |
| 74 | + )} |
| 75 | + </div> |
| 76 | + |
| 77 | + <div className="flex flex-row gap-2 max-w-[350px] items-center"> |
| 78 | + <Button |
| 79 | + variant="primary" |
| 80 | + size="large" |
| 81 | + className="flex-1" |
| 82 | + onClick={send} |
| 83 | + loading={sendAdMessageMutation.isPending} |
| 84 | + disabled={sendAdMessageMutation.isPending} |
| 85 | + > |
| 86 | + {intl("labels.send")} |
| 87 | + </Button> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +export default AdMessage; |
0 commit comments