|
1 | 1 | import { Drawing, TagDrawingSets } from '../types' |
2 | | -import { tagDrawingSets as fixtureTagDrawingSets } from '../__fixtures__' |
| 2 | +import { |
| 3 | + tagDrawingSets as fixtureTagDrawingSets, |
| 4 | + tags as fixtureTags, |
| 5 | +} from '../__fixtures__' |
3 | 6 |
|
4 | | -export function loadMyTagDrawingSets(): TagDrawingSets { |
5 | | - if (typeof window === 'undefined') return {} as TagDrawingSets |
6 | | - return JSON.parse(localStorage.getItem('tagDrawingSets') || '{}') as TagDrawingSets |
7 | | -} |
8 | | - |
9 | | -export function saveMyTagDrawingSets(tagDrawingSets: TagDrawingSets): void { |
10 | | - saveMyTagDrawingSetsString(JSON.stringify(tagDrawingSets || {} as TagDrawingSets)) |
11 | | -} |
12 | | - |
13 | | -export function saveMyTagDrawingSetsString(value: string): void { |
14 | | - localStorage.setItem('tagDrawingSets', value) |
| 7 | +export function getTagsForDrawing(drawing: Drawing): string[] { |
| 8 | + return fixtureTags.filter(tag => drawingHasTag(drawing, tag)) |
15 | 9 | } |
16 | 10 |
|
17 | | -export function drawingHasMyTag(drawing: Drawing, tag: string): boolean { |
18 | | - const tagDrawingSets: TagDrawingSets = loadMyTagDrawingSets() |
19 | | - const drawings: Drawing[] = tagDrawingSets[tag] ?? [] |
| 11 | +export function drawingHasTag(drawing: Drawing, tag: string): boolean { |
| 12 | + const drawings: Drawing[] = fixtureTagDrawingSets[tag] ?? [] |
20 | 13 | return drawings.some(o => o.id === drawing.id) |
21 | 14 | } |
22 | 15 |
|
23 | | -export function addMyTagToDrawing(tag: string, drawing: Drawing): void { |
24 | | - const tagDrawingSets: TagDrawingSets = loadMyTagDrawingSets() |
25 | | - const drawings: Drawing[] = tagDrawingSets[tag] ?? [] |
26 | | - if (!drawings.some(o => o.id === drawing.id)) { |
27 | | - drawings.push(drawing) |
28 | | - tagDrawingSets[tag] = drawings |
29 | | - saveMyTagDrawingSets(tagDrawingSets) |
30 | | - } |
31 | | -} |
32 | | - |
33 | | -export function removeMyTagFromDrawing(tag: string, drawing: Drawing): void { |
34 | | - const tagDrawingSets: TagDrawingSets = loadMyTagDrawingSets() |
35 | | - const drawings: Drawing[] | undefined = tagDrawingSets[tag] |
36 | | - if (!drawings) return |
37 | | - tagDrawingSets[tag] = drawings.filter(o => o.id !== drawing.id) |
38 | | - if (!tagDrawingSets[tag].length) delete tagDrawingSets[tag] |
39 | | - saveMyTagDrawingSets(tagDrawingSets) |
40 | | -} |
41 | | - |
42 | | -export function getMyTags(): string[] { |
43 | | - const tagDrawingSets: TagDrawingSets = loadMyTagDrawingSets() |
44 | | - return Object.keys(tagDrawingSets) |
45 | | -} |
46 | | - |
47 | | -export function getMyTagsForDrawing(drawing: Drawing): string[] { |
48 | | - return getMyTags().filter(tag => drawingHasMyTag(drawing, tag)) |
49 | | -} |
50 | | - |
51 | | -export function getFixtureTagsForDrawing(drawing: Drawing): string[] { |
52 | | - return getFixtureTags().filter(tag => drawingHasFixtureTag(drawing, tag)) |
53 | | -} |
54 | | - |
55 | | -export function getFixtureTags(): string[] { |
56 | | - return Object.keys(fixtureTagDrawingSets) |
| 16 | +export function loadMyTagDrawingSets(): TagDrawingSets { |
| 17 | + if (typeof window === 'undefined') return {} as TagDrawingSets |
| 18 | + return JSON.parse(localStorage.getItem('tagDrawingSets') || '{}') as TagDrawingSets |
57 | 19 | } |
58 | 20 |
|
59 | | -export function drawingHasFixtureTag(drawing: Drawing, tag: string): boolean { |
60 | | - const drawings: Drawing[] = fixtureTagDrawingSets[tag] ?? [] |
61 | | - return drawings.some(o => o.id === drawing.id) |
| 21 | +export function duplicateTagDrawingSets(original: TagDrawingSets): TagDrawingSets { |
| 22 | + return Object.keys(original).reduce<TagDrawingSets>((accumulator, tag) => { |
| 23 | + accumulator[tag] = [...original[tag]] |
| 24 | + return accumulator |
| 25 | + }, {}) |
62 | 26 | } |
0 commit comments