gitscope
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/users/components/dialogs/mutation/sections/expiration-method/expiration-method.strategy.ts · v0.3.2
Source
1 d377c4c Dawsh 2y ago import { useFormContext } from "react-hook-form";
2 d377c4c Dawsh 2y ago import { ExpireStrategy } from "@marzneshin/features/users";
3 d377c4c Dawsh 2y ago
4 d377c4c Dawsh 2y ago class ExpirationMethodErrorClear {
5 d377c4c Dawsh 2y ago protected clearErrors(form: ReturnType<typeof useFormContext>) {
6 d377c4c Dawsh 2y ago form.clearErrors("expire_date");
7 d377c4c Dawsh 2y ago form.clearErrors("usage_duration");
8 d377c4c Dawsh 2y ago form.clearErrors("activation_deadline");
9 d377c4c Dawsh 2y ago }
10 d377c4c Dawsh 2y ago }
11 d377c4c Dawsh 2y ago
12 d377c4c Dawsh 2y ago abstract class ExpirationMethodStrategy {
13 d377c4c Dawsh 2y ago abstract apply(form: ReturnType<typeof useFormContext>): void;
14 d377c4c Dawsh 2y ago }
15 d377c4c Dawsh 2y ago
16 d377c4c Dawsh 2y ago export class FirstUseStrategy
17 d377c4c Dawsh 2y ago extends ExpirationMethodErrorClear
18 d377c4c Dawsh 2y ago implements ExpirationMethodStrategy {
19 d377c4c Dawsh 2y ago
20 d377c4c Dawsh 2y ago apply(form: ReturnType<typeof useFormContext>) {
21 d377c4c Dawsh 2y ago form.setValue("expire_strategy", "start_on_first_use");
22 d377c4c Dawsh 2y ago form.setValue("expire", undefined);
23 d377c4c Dawsh 2y ago this.clearErrors(form);
24 d377c4c Dawsh 2y ago }
25 d377c4c Dawsh 2y ago }
26 d377c4c Dawsh 2y ago
27 d377c4c Dawsh 2y ago export class NeverStrategy
28 d377c4c Dawsh 2y ago extends ExpirationMethodErrorClear
29 d377c4c Dawsh 2y ago implements ExpirationMethodStrategy {
30 d377c4c Dawsh 2y ago
31 d377c4c Dawsh 2y ago apply(form: ReturnType<typeof useFormContext>) {
32 d377c4c Dawsh 2y ago form.setValue("expire_strategy", 'never');
33 d377c4c Dawsh 2y ago form.setValue("expire_date", undefined);
34 d377c4c Dawsh 2y ago form.setValue("activation_deadline", undefined);
35 d377c4c Dawsh 2y ago form.setValue("usage_duration", undefined);
36 d377c4c Dawsh 2y ago this.clearErrors(form);
37 d377c4c Dawsh 2y ago }
38 d377c4c Dawsh 2y ago }
39 d377c4c Dawsh 2y ago
40 d377c4c Dawsh 2y ago export class FixedStrategy
41 d377c4c Dawsh 2y ago extends ExpirationMethodErrorClear
42 d377c4c Dawsh 2y ago implements ExpirationMethodStrategy {
43 d377c4c Dawsh 2y ago
44 d377c4c Dawsh 2y ago apply(form: ReturnType<typeof useFormContext>) {
45 d377c4c Dawsh 2y ago form.setValue("expire_strategy", 'fixed_date');
46 d377c4c Dawsh 2y ago form.setValue("on_hold_expire_duration", 0);
47 d377c4c Dawsh 2y ago form.setValue("activation_deadline", null);
48 d377c4c Dawsh 2y ago this.clearErrors(form);
49 d377c4c Dawsh 2y ago }
50 d377c4c Dawsh 2y ago }
51 d377c4c Dawsh 2y ago
52 d377c4c Dawsh 2y ago export const strategies: { [key in ExpireStrategy]: ExpirationMethodStrategy } = {
53 d377c4c Dawsh 2y ago start_on_first_use: new FirstUseStrategy(),
54 d377c4c Dawsh 2y ago never: new NeverStrategy(),
55 d377c4c Dawsh 2y ago fixed_date: new FixedStrategy(),
56 d377c4c Dawsh 2y ago };