summaryrefslogtreecommitdiff
path: root/front/src/Pages/RangedWeapon.tsx
blob: 5b66fad9b0d43f196d35ccb50ea9f0294c909e1c (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { useContext, useEffect, useState } from "react";
import { AllowedLanguages, LanguageContext } from "../Locales/Context";
import { BackendURL } from "../Config";
import axios from "axios";
import { RangedWeapon, RangedWeaponRequest } from "../Models/RangedWeapon";
import { Description } from "../Models/Description";
import { useNavigate, useParams } from "react-router";
import { GetLocalizedString } from "../Locales/Locales";
import "./Weapons.css"
import { AuthContext } from "../Authentication/ContextProvider";
import { useCookies } from 'react-cookie';

const RangedWeaponsURL = `${BackendURL}/weapons/ranged`;

export function RangedWeaponPage() {
    var user = useContext(AuthContext);
    const { id } = useParams();
    const lang = useContext(LanguageContext);
    const [weapon, setWeapon] = useState<RangedWeapon | null>(null);
    const [description, setDescription] = useState<Description | null>(null);

    useState(() => {
        getWeapon(id).then(data => setWeapon(data));
        getWeaponDescription(id, lang).then(data => setDescription(data));
    });

    if (weapon == null) {
        return <h1> Not found </h1>; // <Navigate to="/404" replace />;
    }

    function editButton() {
        if (!user || user.Role != "editor") {
            return (<p></p>);
        } else {
            return ( <a href={`${id}/edit`}> Edit </a> );
        }
    }

    return (
        <div className="WeaponPage">
            <span> <h1> {weapon?.Name} </h1> </span>
            <h2> { GetLocalizedString(weapon.WeaponType, lang) } </h2>
            <table> 
                <tbody>
                    <tr> 
                        <td> { GetLocalizedString("Accuracy", lang) } </td>
                        <td> { weapon.Accuracy } </td>
                    </tr>
                    <tr>
                        <td> { GetLocalizedString("Concealability", lang) } </td>
                        <td> { GetLocalizedString(weapon.Concealability, lang) } </td>
                    </tr>
                    <tr>
                        <td> { GetLocalizedString("Avaliability", lang) } </td>
                        <td> { GetLocalizedString(weapon.Avaliability, lang) } </td>
                    </tr>
                    <tr>
                        <td> { GetLocalizedString("Damage/Ammunition", lang) } </td>
                        <td> { weapon.Damage }({ weapon.Ammunition }) </td>
                    </tr>
                    <tr>
                        <td> { GetLocalizedString("Number Of Shots", lang) } </td>
                        <td> { weapon.NumberOfShots } </td>
                    </tr>
                    <tr>
                        <td> { GetLocalizedString("Rate Of Fire", lang) } </td>
                        <td> { weapon.RateOfFire } </td>
                    </tr>
                    <tr>
                        <td> { GetLocalizedString("Reliability", lang) } </td>
                        <td> { GetLocalizedString(weapon.Reliability, lang) } </td>
                    </tr>
                    <tr>
                        <td> { GetLocalizedString("Origin", lang) } </td>
                        <td> { GetLocalizedString(weapon.Origin, lang) } </td>
                    </tr>
                </tbody>
            </table>

            <p>
                {description?.Contents}
            </p>
            { editButton() }
        </div>
    );
}
async function getWeaponDescription(id: string | undefined, lanuage: AllowedLanguages): Promise<Description | null> {
    if (id == undefined) return null;
    try {
        const {data, status} = await axios.get<Description>(
            `${RangedWeaponsURL}/${id}/description?lang=${lanuage}`,
            {
                headers: { Accept: "application/json" }
            }
        );
        
        if (status != 200) return null; 

        return data;

    } catch (err) {
        console.log(`Failed to get ranged weapon: ${err}`);
        return null;
    }
}

const defaultNewRangedWeapon: RangedWeapon = {
    Id: "",
    Name: "",
    WeaponType: "Light pistol",
    Accuracy: 0,
    Concealability: "Pocket",
    Avaliability: "Common",
    Damage: "1d6",
    Ammunition: "9mm",
    NumberOfShots: 0,
    RateOfFire: 0,
    Reliability: "Standard",
    Origin: "Corebook",
    CreatedAt: 0,
    UpdatedAt: 0,
}

export function EditRangedWeaponPage() {
    var user = useContext(AuthContext);
    const [isLoading, setIsLoading] = useState(true);
    const { id } = useParams();
    const lang = useContext(LanguageContext);
    var [weapon, setWeapon] = useState<RangedWeapon | null>(null);
    const [description, setDescription] = useState<Description | null>(null);
    const [authCookie, ] = useCookies(['X-AUTH-TOKEN']);

    const [Name, setName] = useState<string>();
    const [WeaponType, setWeaponType] = useState<string>();
    const [Accuracy, setAccuracy] = useState<number>();
    const [Concealability, setConcealability] = useState<string>();
    const [Avaliability, setAvaliability] = useState<string>();
    const [Damage, setDamage] = useState<string>();
    const [Ammunition, setAmmunition] = useState<string>();
    const [NumberOfShots, setNumberOfShots] = useState<number>();
    const [RateOfFire, setRateOfFire] = useState<number>();
    const [Reliability, setReliability] = useState<string>();
    const [Origin, setOrigin] = useState<string>();
    const navigate = useNavigate();

    useState(() => {
        if (id == 'new') {
            setWeapon(defaultNewRangedWeapon);
            return
        }
        getWeapon(id).then(data => setWeapon(data));
        getWeaponDescription(id, lang).then(data => setDescription(data));
    });

    useEffect(() => {
        if (user !== null) {
            setIsLoading(false);
        }
    }, [user]);

    useEffect(() => {
        if (weapon) {
            setName(weapon.Name);
            setWeaponType(weapon.WeaponType);
            setAccuracy(weapon.Accuracy);
            setConcealability(weapon.Concealability);
            setAvaliability(weapon.Avaliability);
            setDamage(weapon.Damage);
            setAmmunition(weapon.Ammunition);
            setNumberOfShots(weapon.NumberOfShots);
            setRateOfFire(weapon.RateOfFire);
            setReliability(weapon.Reliability);
            setOrigin(weapon.Origin);
        }
    }, [weapon]);

    function getUpdatedWeapon(): RangedWeaponRequest {
        var outp: RangedWeaponRequest = {
            Id: id || "",
            Name: Name || "",
            WeaponType: WeaponType || "",
            Accuracy: Accuracy || 0,
            Concealability: Concealability || "",
            Avaliability: Avaliability || "",
            Damage: Damage || "",
            Ammunition: Ammunition || "",
            NumberOfShots: NumberOfShots || 0,
            RateOfFire: RateOfFire || 0,
            Reliability: Reliability || "",
            Origin: Origin || "",
        };
        return outp;
    }

    if (isLoading) {
        return <div>Loading...</div>;
    }

    if (!user || user.Role != "editor") {
        throw new Response("Forbidden", { status: 403 });
    }

    if (weapon == null) {
        return <h1> Not found </h1>; // <Navigate to="/404" replace />;
    }

    return (
        <div className="WeaponPage">
            <span> 
            <input value={Name} onChange={(val) => {
                setName(val.target.value);
            }} /> 
            </span>
            <select 
            value={WeaponType}
            onChange={ (val) => {
                const newWeaponType = val.target.value;
                setWeaponType(newWeaponType);
            }}> 
                <option value="Light pistol"> Light pistol </option>
                <option value="Medium pistol"> Medium pistol </option>
                <option value="Heavy pistol"> Heavy pistol </option>
                <option value="Very heavy pistol"> Very heavy pistol </option>

                <option value="Light submachinegun"> Light submachinegun </option>
                <option value="Medium submachinegun"> Medium submachinegun </option>
                <option value="Heavy submachinegun"> Heavy submachinegun </option>

                <option value="Assault rifle"> Assault rifle </option>
                <option value="Heavy weapon"> Heavy weapon </option>
                <option value="Exotic"> Exotic </option>

            </select>
            <table> 
                <tbody>
                    <tr> 
                        <td> { GetLocalizedString("Accuracy", lang) } </td>
                        <td> 
                            <input value={Accuracy} type="number" onChange={(val) => {
                                setAccuracy(+val.target.value);
                            }} /> 
                        </td>
                    </tr>

                    <tr>
                        <td> { GetLocalizedString("Concealability", lang) } </td>
                        <td> 
                            <select 
                                value={Concealability}
                                onChange={ (val) => {
                                    const newConcealability = val.target.value;
                                    setConcealability(newConcealability);
                                }}> 
                                <option value="Pocket"> Pocket </option>
                                <option value="Jacket"> Jacket </option>
                                <option value="Long"> Long </option>
                                <option value="Nothing"> Nothing </option>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td> { GetLocalizedString("Avaliability", lang) } </td>
                        <td>
                            <select 
                                value={Avaliability}
                                onChange={ (val) => {
                                    const newAvaliability = val.target.value;
                                    setAvaliability(newAvaliability);
                                }}> 
                                <option value="Excellent"> Excellent </option>
                                <option value="Common"> Common </option>
                                <option value="Poor"> Poor </option>
                                <option value="Rare"> Rare </option>
                            </select>
                        </td>
                    </tr>
                    
                    <tr>
                        <td> { GetLocalizedString("Damage/Ammunition", lang) } </td>
                        <td>
                        <input 
                        className="DmgInput"
                        value={Damage}
                        onChange={val => {
                            setDamage(val.target.value);
                        }}/> (
                            <input 
                            className="DmgInput"
                            value={Ammunition}
                            onChange={val => {
                                setAmmunition(val.target.value);
                            }}/>
                        ) 
                        </td>
                    </tr>

                    <tr>
                        <td> { GetLocalizedString("Number Of Shots", lang) } </td>
                        <td> 
                            <input value={NumberOfShots} type="number" onChange={(val) => {
                                setNumberOfShots(+val.target.value);
                            }} /> 
                        </td>
                    </tr>

                    <tr>
                        <td> { GetLocalizedString("Rate Of Fire", lang) } </td>
                        <td> 
                            <input value={RateOfFire} type="number" onChange={(val) => {
                                setRateOfFire(+val.target.value);
                            }} /> 
                        </td>
                    </tr>

                    <tr>
                        <td> { GetLocalizedString("Reliability", lang) } </td>
                        <td> 
                            <select 
                                value={Reliability}
                                onChange={ (val) => {
                                    const newReliability = val.target.value;
                                    setReliability(newReliability);
                                }}> 
                                <option value="Very reliable"> Very reliable </option>
                                <option value="Standard"> Standard </option>
                                <option value="Unreliable"> Unreliable </option>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td> { GetLocalizedString("Origin", lang) } </td>
                        <td> 
                            <select 
                                value={Origin}
                                onChange={ (val) => {
                                    const newOrigin = val.target.value;
                                    setOrigin(newOrigin);
                                }}> 
                                <option value="Corebook"> Corebook </option>
                            </select>
                        </td>
                    </tr>
                </tbody>
            </table>

            <button onClick={ () => {
                const updated = getUpdatedWeapon();
                if (id == "new") {
                    CreateRangedWeapon(updated, authCookie['X-AUTH-TOKEN']);
                } else {
                    UpdateRangedWeapon(id, updated, authCookie['X-AUTH-TOKEN']);
                }
                navigate('/weapons');
                document.location.reload();
            }}> Update </button> 
            <button onClick={ () => {
                if (id == "new") return;
                DeleteRangedWeapon(id, authCookie['X-AUTH-TOKEN']);
                navigate('/weapons');
                document.location.reload();
            }}> Delete </button> 

            <p>
                {description?.Contents}
            </p>
        </div>
    );
}

async function getWeapon(id: string | undefined): Promise<RangedWeapon | null>  {
    if (id == undefined) return null;
    try {
        const {data, status} = await axios.get<RangedWeapon>(
            `${RangedWeaponsURL}/${id}`,
            {
                headers: { Accept: "application/json" }
            }
        );
        
        if (status != 200) return null; 

        return data;

    } catch (err) {
        console.log(`Failed to get ranged weapon: ${err}`);
        return null;
    }
}

async function UpdateRangedWeapon(id: string | undefined, model: RangedWeaponRequest, token: string) {
    if (id == undefined) return null;
    const uri = `${RangedWeaponsURL}/${id}`;

    await axios.put(uri, model, {
        headers: {
            Authorization: `Bearer ${token}`
        }
    })
    .then(resp => {
        console.log(resp);
    })
    .catch(err => {
        console.log(`Failed to update the model: ${err}`)
    });
}

function generateId(Name: string): string {
    return Name.toLowerCase().replaceAll(' ', '_');
}

async function CreateRangedWeapon(model: RangedWeaponRequest, token: string) {
    if (model.Name.length == 0) {
        throw new Error('The model name should not be empty');
    }
    model.Id = generateId(model.Name);
    if (model.Id == "new") {
        throw new Error("Forbidden name");
    }

    await axios.post(RangedWeaponsURL, model, {
        headers: {
            Authorization: `Bearer ${token}`
        }
    })
    .then(resp => {
        console.log(resp);
    })
    .catch(err => {
        console.log(`Failed to create the model: ${err}`)
    });
}

async function DeleteRangedWeapon(id: string | undefined, token: string) {
    if (id == undefined || id == "new" || id.length < 1) {
        throw new Error("Invalid ID");
    }

    const uri = `${RangedWeaponsURL}/${id}`;
    await axios.delete(uri, {
        headers: {
            Authorization: `Bearer ${token}`
        }
    })
    .then(resp => {
        console.log(resp);
    })
    .catch(err => {
        console.log(`Failed to create the model: ${err}`)
    });
}